drbd_worker.c revision a6b32bc3cebd3fb6848c526763733b9dbc389c02
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/wait.h>
30b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner#include <linux/mm.h>
31b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner#include <linux/memcontrol.h>
32b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner#include <linux/mm_inline.h>
33b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner#include <linux/slab.h>
34b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner#include <linux/random.h>
35b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner#include <linux/string.h>
36b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner#include <linux/scatterlist.h>
37b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
38b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner#include "drbd_int.h"
39a3603a6e3b4f2f0fb5529821134424e2eeec88fdAndreas Gruenbacher#include "drbd_protocol.h"
40b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner#include "drbd_req.h"
41b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
4200d56944ff086f895e9ad184a7785ca1eece4a3bPhilipp Reisnerstatic int w_make_ov_request(struct drbd_work *w, int cancel);
43b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
44b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
45c5a91619793d444e5103ec5841045bf878718398Andreas Gruenbacher/* endio handlers:
46c5a91619793d444e5103ec5841045bf878718398Andreas Gruenbacher *   drbd_md_io_complete (defined here)
47fcefa62e4c26e70c70b9e8252a4bc9b9031a4182Andreas Gruenbacher *   drbd_request_endio (defined here)
48fcefa62e4c26e70c70b9e8252a4bc9b9031a4182Andreas Gruenbacher *   drbd_peer_request_endio (defined here)
49c5a91619793d444e5103ec5841045bf878718398Andreas Gruenbacher *   bm_async_io_complete (defined in drbd_bitmap.c)
50c5a91619793d444e5103ec5841045bf878718398Andreas Gruenbacher *
51b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * For all these callbacks, note the following:
52b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * The callbacks will be called in irq context by the IDE drivers,
53b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * and in Softirqs/Tasklets/BH context by the SCSI drivers.
54b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * Try to get the locking right :)
55b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner *
56b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner */
57b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
58b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
59b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner/* About the global_state_lock
60b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner   Each state transition on an device holds a read lock. In case we have
6195f8efd08bcce65df994049a292b94e56c7ada67Andreas Gruenbacher   to evaluate the resync after dependencies, we grab a write lock, because
62b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner   we need stable states on all devices for that.  */
63b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisnerrwlock_t global_state_lock;
64b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
65b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner/* used for synchronous meta data and bitmap IO
66b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * submitted by drbd_md_sync_page_io()
67b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner */
68b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisnervoid drbd_md_io_complete(struct bio *bio, int error)
69b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
70b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	struct drbd_md_io *md_io;
71b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	struct drbd_device *device;
72b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
73b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	md_io = (struct drbd_md_io *)bio->bi_private;
74b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	device = container_of(md_io, struct drbd_device, md_io);
75cdfda633d235028e9b27381dedb65416409e8729Philipp Reisner
76b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	md_io->error = error;
77b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
780cfac5dd904ec8b376beb27f6ad265b12d71bf9ePhilipp Reisner	/* We grabbed an extra reference in _drbd_md_sync_page_io() to be able
790cfac5dd904ec8b376beb27f6ad265b12d71bf9ePhilipp Reisner	 * to timeout on the lower level device, and eventually detach from it.
800cfac5dd904ec8b376beb27f6ad265b12d71bf9ePhilipp Reisner	 * If this io completion runs after that timeout expired, this
810cfac5dd904ec8b376beb27f6ad265b12d71bf9ePhilipp Reisner	 * drbd_md_put_buffer() may allow us to finally try and re-attach.
820cfac5dd904ec8b376beb27f6ad265b12d71bf9ePhilipp Reisner	 * During normal operation, this only puts that extra reference
830cfac5dd904ec8b376beb27f6ad265b12d71bf9ePhilipp Reisner	 * down to 1 again.
840cfac5dd904ec8b376beb27f6ad265b12d71bf9ePhilipp Reisner	 * Make sure we first drop the reference, and only then signal
850cfac5dd904ec8b376beb27f6ad265b12d71bf9ePhilipp Reisner	 * completion, or we may (in drbd_al_read_log()) cycle so fast into the
860cfac5dd904ec8b376beb27f6ad265b12d71bf9ePhilipp Reisner	 * next drbd_md_sync_page_io(), that we trigger the
87b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	 * ASSERT(atomic_read(&device->md_io_in_use) == 1) there.
880cfac5dd904ec8b376beb27f6ad265b12d71bf9ePhilipp Reisner	 */
89b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	drbd_md_put_buffer(device);
90cdfda633d235028e9b27381dedb65416409e8729Philipp Reisner	md_io->done = 1;
91b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	wake_up(&device->misc_wait);
92cdfda633d235028e9b27381dedb65416409e8729Philipp Reisner	bio_put(bio);
93b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	if (device->ldev) /* special case: drbd_md_read() during drbd_adm_attach() */
94b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		put_ldev(device);
95b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
96b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
97b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner/* reads on behalf of the partner,
98b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * "submitted" by the receiver
99b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner */
100a186e47856c7877fa684d75901436c1d35ec35e0Rashika Kheriastatic void drbd_endio_read_sec_final(struct drbd_peer_request *peer_req) __releases(local)
101b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
102b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	unsigned long flags = 0;
103b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	struct drbd_device *device = peer_req->w.device;
104b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
105a6b32bc3cebd3fb6848c526763733b9dbc389c02Andreas Gruenbacher	spin_lock_irqsave(&first_peer_device(device)->connection->req_lock, flags);
106b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	device->read_cnt += peer_req->i.size >> 9;
107db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher	list_del(&peer_req->w.list);
108b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	if (list_empty(&device->read_ee))
109b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		wake_up(&device->ee_wait);
110db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher	if (test_bit(__EE_WAS_ERROR, &peer_req->flags))
111b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		__drbd_chk_io_error(device, DRBD_READ_ERROR);
112a6b32bc3cebd3fb6848c526763733b9dbc389c02Andreas Gruenbacher	spin_unlock_irqrestore(&first_peer_device(device)->connection->req_lock, flags);
113b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
114a6b32bc3cebd3fb6848c526763733b9dbc389c02Andreas Gruenbacher	drbd_queue_work(&first_peer_device(device)->connection->sender_work, &peer_req->w);
115b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	put_ldev(device);
116b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
117b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
118b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner/* writes on behalf of the partner, or resync writes,
11945bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg * "submitted" by the receiver, final stage.  */
120db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacherstatic void drbd_endio_write_sec_final(struct drbd_peer_request *peer_req) __releases(local)
121b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
122b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	unsigned long flags = 0;
123b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	struct drbd_device *device = peer_req->w.device;
124181286ad22bf9bfb85de625e8501285de5261b35Lars Ellenberg	struct drbd_interval i;
125b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	int do_wake;
126579b57ed730819970a3542b4bbcc2d4176f25c72Andreas Gruenbacher	u64 block_id;
127b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	int do_al_complete_io;
128b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
129db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher	/* after we moved peer_req to done_ee,
130b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	 * we may no longer access it,
131b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	 * it may be freed/reused already!
132b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	 * (as soon as we release the req_lock) */
133181286ad22bf9bfb85de625e8501285de5261b35Lars Ellenberg	i = peer_req->i;
134db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher	do_al_complete_io = peer_req->flags & EE_CALL_AL_COMPLETE_IO;
135db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher	block_id = peer_req->block_id;
136b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
137a6b32bc3cebd3fb6848c526763733b9dbc389c02Andreas Gruenbacher	spin_lock_irqsave(&first_peer_device(device)->connection->req_lock, flags);
138b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	device->writ_cnt += peer_req->i.size >> 9;
139b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	list_move_tail(&peer_req->w.list, &device->done_ee);
140b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
141bb3bfe96144a4535d47ccfea444bc1ef8e02f4e3Andreas Gruenbacher	/*
1425e4722645afb27ee749ea65988544450f08f78baAndreas Gruenbacher	 * Do not remove from the write_requests tree here: we did not send the
143bb3bfe96144a4535d47ccfea444bc1ef8e02f4e3Andreas Gruenbacher	 * Ack yet and did not wake possibly waiting conflicting requests.
144bb3bfe96144a4535d47ccfea444bc1ef8e02f4e3Andreas Gruenbacher	 * Removed from the tree from "drbd_process_done_ee" within the
145bb3bfe96144a4535d47ccfea444bc1ef8e02f4e3Andreas Gruenbacher	 * appropriate w.cb (e_end_block/e_end_resync_block) or from
146bb3bfe96144a4535d47ccfea444bc1ef8e02f4e3Andreas Gruenbacher	 * _drbd_clear_done_ee.
147bb3bfe96144a4535d47ccfea444bc1ef8e02f4e3Andreas Gruenbacher	 */
148b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
149b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	do_wake = list_empty(block_id == ID_SYNCER ? &device->sync_ee : &device->active_ee);
150b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
151db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher	if (test_bit(__EE_WAS_ERROR, &peer_req->flags))
152b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		__drbd_chk_io_error(device, DRBD_WRITE_ERROR);
153a6b32bc3cebd3fb6848c526763733b9dbc389c02Andreas Gruenbacher	spin_unlock_irqrestore(&first_peer_device(device)->connection->req_lock, flags);
154b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
155579b57ed730819970a3542b4bbcc2d4176f25c72Andreas Gruenbacher	if (block_id == ID_SYNCER)
156b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		drbd_rs_complete_io(device, i.sector);
157b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
158b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (do_wake)
159b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		wake_up(&device->ee_wait);
160b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
161b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (do_al_complete_io)
162b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		drbd_al_complete_io(device, &i);
163b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
164a6b32bc3cebd3fb6848c526763733b9dbc389c02Andreas Gruenbacher	wake_asender(first_peer_device(device)->connection);
165b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	put_ldev(device);
16645bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg}
167b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
16845bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg/* writes on behalf of the partner, or resync writes,
16945bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg * "submitted" by the receiver.
17045bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg */
171fcefa62e4c26e70c70b9e8252a4bc9b9031a4182Andreas Gruenbachervoid drbd_peer_request_endio(struct bio *bio, int error)
17245bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg{
173db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher	struct drbd_peer_request *peer_req = bio->bi_private;
174b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	struct drbd_device *device = peer_req->w.device;
17545bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	int uptodate = bio_flagged(bio, BIO_UPTODATE);
17645bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	int is_write = bio_data_dir(bio) == WRITE;
17745bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg
178071942727824bab03b1a3f6b6eeb5b269697b333Lars Ellenberg	if (error && __ratelimit(&drbd_ratelimit_state))
17945bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg		dev_warn(DEV, "%s: error=%d s=%llus\n",
18045bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg				is_write ? "write" : "read", error,
181db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher				(unsigned long long)peer_req->i.sector);
18245bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	if (!error && !uptodate) {
183071942727824bab03b1a3f6b6eeb5b269697b333Lars Ellenberg		if (__ratelimit(&drbd_ratelimit_state))
184071942727824bab03b1a3f6b6eeb5b269697b333Lars Ellenberg			dev_warn(DEV, "%s: setting error to -EIO s=%llus\n",
185071942727824bab03b1a3f6b6eeb5b269697b333Lars Ellenberg					is_write ? "write" : "read",
186db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher					(unsigned long long)peer_req->i.sector);
18745bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg		/* strange behavior of some lower level drivers...
18845bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg		 * fail the request by clearing the uptodate flag,
18945bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg		 * but do not return any error?! */
19045bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg		error = -EIO;
19145bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	}
19245bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg
19345bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	if (error)
194db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher		set_bit(__EE_WAS_ERROR, &peer_req->flags);
19545bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg
19645bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	bio_put(bio); /* no need for the bio anymore */
197db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher	if (atomic_dec_and_test(&peer_req->pending_bios)) {
19845bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg		if (is_write)
199db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher			drbd_endio_write_sec_final(peer_req);
20045bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg		else
201db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher			drbd_endio_read_sec_final(peer_req);
20245bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	}
203b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
204b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
205b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner/* read, readA or write requests on R_PRIMARY coming from drbd_make_request
206b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner */
207fcefa62e4c26e70c70b9e8252a4bc9b9031a4182Andreas Gruenbachervoid drbd_request_endio(struct bio *bio, int error)
208b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
209a115413de13ae6beb0cbfc198afe385a261ab284Lars Ellenberg	unsigned long flags;
210b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	struct drbd_request *req = bio->bi_private;
211b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	struct drbd_device *device = req->w.device;
212a115413de13ae6beb0cbfc198afe385a261ab284Lars Ellenberg	struct bio_and_error m;
213b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	enum drbd_req_event what;
214b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	int uptodate = bio_flagged(bio, BIO_UPTODATE);
215b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
216b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (!error && !uptodate) {
217b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		dev_warn(DEV, "p %s: setting error to -EIO\n",
218b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			 bio_data_dir(bio) == WRITE ? "write" : "read");
219b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		/* strange behavior of some lower level drivers...
220b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		 * fail the request by clearing the uptodate flag,
221b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		 * but do not return any error?! */
222b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		error = -EIO;
223b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
224b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
2251b6dd252e6c631322372c018ed546a108d9869d3Philipp Reisner
2261b6dd252e6c631322372c018ed546a108d9869d3Philipp Reisner	/* If this request was aborted locally before,
2271b6dd252e6c631322372c018ed546a108d9869d3Philipp Reisner	 * but now was completed "successfully",
2281b6dd252e6c631322372c018ed546a108d9869d3Philipp Reisner	 * chances are that this caused arbitrary data corruption.
2291b6dd252e6c631322372c018ed546a108d9869d3Philipp Reisner	 *
2301b6dd252e6c631322372c018ed546a108d9869d3Philipp Reisner	 * "aborting" requests, or force-detaching the disk, is intended for
2311b6dd252e6c631322372c018ed546a108d9869d3Philipp Reisner	 * completely blocked/hung local backing devices which do no longer
2321b6dd252e6c631322372c018ed546a108d9869d3Philipp Reisner	 * complete requests at all, not even do error completions.  In this
2331b6dd252e6c631322372c018ed546a108d9869d3Philipp Reisner	 * situation, usually a hard-reset and failover is the only way out.
2341b6dd252e6c631322372c018ed546a108d9869d3Philipp Reisner	 *
2351b6dd252e6c631322372c018ed546a108d9869d3Philipp Reisner	 * By "aborting", basically faking a local error-completion,
2361b6dd252e6c631322372c018ed546a108d9869d3Philipp Reisner	 * we allow for a more graceful swichover by cleanly migrating services.
2371b6dd252e6c631322372c018ed546a108d9869d3Philipp Reisner	 * Still the affected node has to be rebooted "soon".
2381b6dd252e6c631322372c018ed546a108d9869d3Philipp Reisner	 *
2391b6dd252e6c631322372c018ed546a108d9869d3Philipp Reisner	 * By completing these requests, we allow the upper layers to re-use
2401b6dd252e6c631322372c018ed546a108d9869d3Philipp Reisner	 * the associated data pages.
2411b6dd252e6c631322372c018ed546a108d9869d3Philipp Reisner	 *
2421b6dd252e6c631322372c018ed546a108d9869d3Philipp Reisner	 * If later the local backing device "recovers", and now DMAs some data
2431b6dd252e6c631322372c018ed546a108d9869d3Philipp Reisner	 * from disk into the original request pages, in the best case it will
2441b6dd252e6c631322372c018ed546a108d9869d3Philipp Reisner	 * just put random data into unused pages; but typically it will corrupt
2451b6dd252e6c631322372c018ed546a108d9869d3Philipp Reisner	 * meanwhile completely unrelated data, causing all sorts of damage.
2461b6dd252e6c631322372c018ed546a108d9869d3Philipp Reisner	 *
2471b6dd252e6c631322372c018ed546a108d9869d3Philipp Reisner	 * Which means delayed successful completion,
2481b6dd252e6c631322372c018ed546a108d9869d3Philipp Reisner	 * especially for READ requests,
2491b6dd252e6c631322372c018ed546a108d9869d3Philipp Reisner	 * is a reason to panic().
2501b6dd252e6c631322372c018ed546a108d9869d3Philipp Reisner	 *
2511b6dd252e6c631322372c018ed546a108d9869d3Philipp Reisner	 * We assume that a delayed *error* completion is OK,
2521b6dd252e6c631322372c018ed546a108d9869d3Philipp Reisner	 * though we still will complain noisily about it.
2531b6dd252e6c631322372c018ed546a108d9869d3Philipp Reisner	 */
2541b6dd252e6c631322372c018ed546a108d9869d3Philipp Reisner	if (unlikely(req->rq_state & RQ_LOCAL_ABORTED)) {
2551b6dd252e6c631322372c018ed546a108d9869d3Philipp Reisner		if (__ratelimit(&drbd_ratelimit_state))
2561b6dd252e6c631322372c018ed546a108d9869d3Philipp Reisner			dev_emerg(DEV, "delayed completion of aborted local request; disk-timeout may be too aggressive\n");
2571b6dd252e6c631322372c018ed546a108d9869d3Philipp Reisner
2581b6dd252e6c631322372c018ed546a108d9869d3Philipp Reisner		if (!error)
2591b6dd252e6c631322372c018ed546a108d9869d3Philipp Reisner			panic("possible random memory corruption caused by delayed completion of aborted local request\n");
2601b6dd252e6c631322372c018ed546a108d9869d3Philipp Reisner	}
2611b6dd252e6c631322372c018ed546a108d9869d3Philipp Reisner
262b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	/* to avoid recursion in __req_mod */
263b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (unlikely(error)) {
264b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		what = (bio_data_dir(bio) == WRITE)
2658554df1c6d3bb7686b39ed775772f507fa857c19Andreas Gruenbacher			? WRITE_COMPLETED_WITH_ERROR
2665c3c7e64bbcb60db39d0d55c8fab39ef41d41c30Lars Ellenberg			: (bio_rw(bio) == READ)
2678554df1c6d3bb7686b39ed775772f507fa857c19Andreas Gruenbacher			  ? READ_COMPLETED_WITH_ERROR
2688554df1c6d3bb7686b39ed775772f507fa857c19Andreas Gruenbacher			  : READ_AHEAD_COMPLETED_WITH_ERROR;
269b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	} else
2708554df1c6d3bb7686b39ed775772f507fa857c19Andreas Gruenbacher		what = COMPLETED_OK;
271b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
272b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	bio_put(req->private_bio);
273b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	req->private_bio = ERR_PTR(error);
274b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
275a115413de13ae6beb0cbfc198afe385a261ab284Lars Ellenberg	/* not req_mod(), we need irqsave here! */
276a6b32bc3cebd3fb6848c526763733b9dbc389c02Andreas Gruenbacher	spin_lock_irqsave(&first_peer_device(device)->connection->req_lock, flags);
277a115413de13ae6beb0cbfc198afe385a261ab284Lars Ellenberg	__req_mod(req, what, &m);
278a6b32bc3cebd3fb6848c526763733b9dbc389c02Andreas Gruenbacher	spin_unlock_irqrestore(&first_peer_device(device)->connection->req_lock, flags);
279b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	put_ldev(device);
280a115413de13ae6beb0cbfc198afe385a261ab284Lars Ellenberg
281a115413de13ae6beb0cbfc198afe385a261ab284Lars Ellenberg	if (m.bio)
282b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		complete_master_bio(device, &m);
283b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
284b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
285b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbachervoid drbd_csum_ee(struct drbd_device *device, struct crypto_hash *tfm,
286db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher		  struct drbd_peer_request *peer_req, void *digest)
28745bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg{
28845bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	struct hash_desc desc;
28945bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	struct scatterlist sg;
290db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher	struct page *page = peer_req->pages;
29145bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	struct page *tmp;
29245bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	unsigned len;
29345bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg
29445bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	desc.tfm = tfm;
29545bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	desc.flags = 0;
29645bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg
29745bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	sg_init_table(&sg, 1);
29845bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	crypto_hash_init(&desc);
29945bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg
30045bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	while ((tmp = page_chain_next(page))) {
30145bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg		/* all but the last page will be fully used */
30245bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg		sg_set_page(&sg, page, PAGE_SIZE, 0);
30345bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg		crypto_hash_update(&desc, &sg, sg.length);
30445bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg		page = tmp;
30545bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	}
30645bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	/* and now the last, possibly only partially used page */
307db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher	len = peer_req->i.size & (PAGE_SIZE - 1);
30845bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	sg_set_page(&sg, page, len ?: PAGE_SIZE, 0);
30945bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	crypto_hash_update(&desc, &sg, sg.length);
31045bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	crypto_hash_final(&desc, digest);
31145bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg}
31245bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg
313b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbachervoid drbd_csum_bio(struct drbd_device *device, struct crypto_hash *tfm, struct bio *bio, void *digest)
314b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
315b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	struct hash_desc desc;
316b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	struct scatterlist sg;
3177988613b0e5b2638caf6cd493cc78e9595eba19cKent Overstreet	struct bio_vec bvec;
3187988613b0e5b2638caf6cd493cc78e9595eba19cKent Overstreet	struct bvec_iter iter;
319b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
320b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	desc.tfm = tfm;
321b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	desc.flags = 0;
322b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
323b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	sg_init_table(&sg, 1);
324b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	crypto_hash_init(&desc);
325b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
3267988613b0e5b2638caf6cd493cc78e9595eba19cKent Overstreet	bio_for_each_segment(bvec, bio, iter) {
3277988613b0e5b2638caf6cd493cc78e9595eba19cKent Overstreet		sg_set_page(&sg, bvec.bv_page, bvec.bv_len, bvec.bv_offset);
328b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		crypto_hash_update(&desc, &sg, sg.length);
329b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
330b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	crypto_hash_final(&desc, digest);
331b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
332b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
3339676c760979371701ea5a6f8adb7ce8125c22c7dLars Ellenberg/* MAYBE merge common code with w_e_end_ov_req */
33499920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacherstatic int w_e_send_csum(struct drbd_work *w, int cancel)
335b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
33600d56944ff086f895e9ad184a7785ca1eece4a3bPhilipp Reisner	struct drbd_peer_request *peer_req = container_of(w, struct drbd_peer_request, w);
337b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	struct drbd_device *device = w->device;
338b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	int digest_size;
339b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	void *digest;
34099920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacher	int err = 0;
341b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
34253ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenberg	if (unlikely(cancel))
34353ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenberg		goto out;
344b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
3459676c760979371701ea5a6f8adb7ce8125c22c7dLars Ellenberg	if (unlikely((peer_req->flags & EE_WAS_ERROR) != 0))
34653ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenberg		goto out;
347b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
348a6b32bc3cebd3fb6848c526763733b9dbc389c02Andreas Gruenbacher	digest_size = crypto_hash_digestsize(first_peer_device(device)->connection->csums_tfm);
34953ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenberg	digest = kmalloc(digest_size, GFP_NOIO);
35053ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenberg	if (digest) {
351db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher		sector_t sector = peer_req->i.sector;
352db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher		unsigned int size = peer_req->i.size;
353a6b32bc3cebd3fb6848c526763733b9dbc389c02Andreas Gruenbacher		drbd_csum_ee(device, first_peer_device(device)->connection->csums_tfm, peer_req, digest);
3549676c760979371701ea5a6f8adb7ce8125c22c7dLars Ellenberg		/* Free peer_req and pages before send.
35553ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenberg		 * In case we block on congestion, we could otherwise run into
35653ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenberg		 * some distributed deadlock, if the other side blocks on
35753ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenberg		 * congestion as well, because our receiver blocks in
358c37c8ecfee685fa42de8fd418ad8ca1e66408bd8Andreas Gruenbacher		 * drbd_alloc_pages due to pp_in_use > max_buffers. */
359b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		drbd_free_peer_req(device, peer_req);
360db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher		peer_req = NULL;
361b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		inc_rs_pending(device);
362b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		err = drbd_send_drequest_csum(device, sector, size,
363db1b0b724e56f34608b76197191ef0577a1ddd45Andreas Gruenbacher					      digest, digest_size,
364db1b0b724e56f34608b76197191ef0577a1ddd45Andreas Gruenbacher					      P_CSUM_RS_REQUEST);
36553ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenberg		kfree(digest);
36653ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenberg	} else {
36753ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenberg		dev_err(DEV, "kmalloc() of digest failed.\n");
36899920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacher		err = -ENOMEM;
36953ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenberg	}
370b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
37153ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenbergout:
372db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher	if (peer_req)
373b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		drbd_free_peer_req(device, peer_req);
374b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
37599920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacher	if (unlikely(err))
376b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		dev_err(DEV, "drbd_send_drequest(..., csum) failed\n");
37799920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacher	return err;
378b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
379b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
380b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner#define GFP_TRY	(__GFP_HIGHMEM | __GFP_NOWARN)
381b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
382b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacherstatic int read_for_csum(struct drbd_device *device, sector_t sector, int size)
383b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
384db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher	struct drbd_peer_request *peer_req;
385b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
386b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	if (!get_ldev(device))
38780a40e439e5a3f30b0a6210a1add6d7c33392e54Lars Ellenberg		return -EIO;
388b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
389b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	if (drbd_rs_should_slow_down(device, sector))
3900f0601f4ea2f53cfd8bcae060fb03d9bbde070ecLars Ellenberg		goto defer;
3910f0601f4ea2f53cfd8bcae060fb03d9bbde070ecLars Ellenberg
392b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	/* GFP_TRY, because if there is no memory available right now, this may
393b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	 * be rescheduled for later. It is "only" background resync, after all. */
394b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	peer_req = drbd_alloc_peer_req(device, ID_SYNCER /* unused */, sector,
3950db55363cb1e6cfe2bedecb7e47c05f8992c612eAndreas Gruenbacher				       size, GFP_TRY);
396db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher	if (!peer_req)
39780a40e439e5a3f30b0a6210a1add6d7c33392e54Lars Ellenberg		goto defer;
398b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
399db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher	peer_req->w.cb = w_e_send_csum;
400a6b32bc3cebd3fb6848c526763733b9dbc389c02Andreas Gruenbacher	spin_lock_irq(&first_peer_device(device)->connection->req_lock);
401b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	list_add(&peer_req->w.list, &device->read_ee);
402a6b32bc3cebd3fb6848c526763733b9dbc389c02Andreas Gruenbacher	spin_unlock_irq(&first_peer_device(device)->connection->req_lock);
403b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
404b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	atomic_add(size >> 9, &device->rs_sect_ev);
405b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	if (drbd_submit_peer_request(device, peer_req, READ, DRBD_FAULT_RS_RD) == 0)
40680a40e439e5a3f30b0a6210a1add6d7c33392e54Lars Ellenberg		return 0;
407b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
40810f6d9926cd17afff9dc03c967706419798b4929Lars Ellenberg	/* If it failed because of ENOMEM, retry should help.  If it failed
40910f6d9926cd17afff9dc03c967706419798b4929Lars Ellenberg	 * because bio_add_page failed (probably broken lower level driver),
41010f6d9926cd17afff9dc03c967706419798b4929Lars Ellenberg	 * retry may or may not help.
41110f6d9926cd17afff9dc03c967706419798b4929Lars Ellenberg	 * If it does not, you may need to force disconnect. */
412a6b32bc3cebd3fb6848c526763733b9dbc389c02Andreas Gruenbacher	spin_lock_irq(&first_peer_device(device)->connection->req_lock);
413db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher	list_del(&peer_req->w.list);
414a6b32bc3cebd3fb6848c526763733b9dbc389c02Andreas Gruenbacher	spin_unlock_irq(&first_peer_device(device)->connection->req_lock);
41522cc37a943832c948808884604ec6f5ff2594c1dLars Ellenberg
416b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	drbd_free_peer_req(device, peer_req);
41780a40e439e5a3f30b0a6210a1add6d7c33392e54Lars Ellenbergdefer:
418b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	put_ldev(device);
41980a40e439e5a3f30b0a6210a1add6d7c33392e54Lars Ellenberg	return -EAGAIN;
420b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
421b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
42299920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacherint w_resync_timer(struct drbd_work *w, int cancel)
423b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
424b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	struct drbd_device *device = w->device;
425b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	switch (device->state.conn) {
42663106d3c6c769b6219bd04edde513b12abae3f61Philipp Reisner	case C_VERIFY_S:
42700d56944ff086f895e9ad184a7785ca1eece4a3bPhilipp Reisner		w_make_ov_request(w, cancel);
42863106d3c6c769b6219bd04edde513b12abae3f61Philipp Reisner		break;
42963106d3c6c769b6219bd04edde513b12abae3f61Philipp Reisner	case C_SYNC_TARGET:
43000d56944ff086f895e9ad184a7785ca1eece4a3bPhilipp Reisner		w_make_resync_request(w, cancel);
43163106d3c6c769b6219bd04edde513b12abae3f61Philipp Reisner		break;
432b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
433b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
43499920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacher	return 0;
435794abb753e29e85949b3719dbc2ab6a98711a47ePhilipp Reisner}
436794abb753e29e85949b3719dbc2ab6a98711a47ePhilipp Reisner
437794abb753e29e85949b3719dbc2ab6a98711a47ePhilipp Reisnervoid resync_timer_fn(unsigned long data)
438794abb753e29e85949b3719dbc2ab6a98711a47ePhilipp Reisner{
439b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	struct drbd_device *device = (struct drbd_device *) data;
440794abb753e29e85949b3719dbc2ab6a98711a47ePhilipp Reisner
441b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	if (list_empty(&device->resync_work.list))
442a6b32bc3cebd3fb6848c526763733b9dbc389c02Andreas Gruenbacher		drbd_queue_work(&first_peer_device(device)->connection->sender_work, &device->resync_work);
443b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
444b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
445778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisnerstatic void fifo_set(struct fifo_buffer *fb, int value)
446778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner{
447778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	int i;
448778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner
449778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	for (i = 0; i < fb->size; i++)
450f10f262349762c96ab247b6108af3a30b52b6f5aPhilipp Reisner		fb->values[i] = value;
451778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner}
452778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner
453778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisnerstatic int fifo_push(struct fifo_buffer *fb, int value)
454778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner{
455778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	int ov;
456778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner
457778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	ov = fb->values[fb->head_index];
458778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	fb->values[fb->head_index++] = value;
459778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner
460778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	if (fb->head_index >= fb->size)
461778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner		fb->head_index = 0;
462778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner
463778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	return ov;
464778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner}
465778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner
466778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisnerstatic void fifo_add_val(struct fifo_buffer *fb, int value)
467778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner{
468778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	int i;
469778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner
470778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	for (i = 0; i < fb->size; i++)
471778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner		fb->values[i] += value;
472778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner}
473778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner
4749958c857c760eec76f4fdf288b6f33a1c3b41833Philipp Reisnerstruct fifo_buffer *fifo_alloc(int fifo_size)
4759958c857c760eec76f4fdf288b6f33a1c3b41833Philipp Reisner{
4769958c857c760eec76f4fdf288b6f33a1c3b41833Philipp Reisner	struct fifo_buffer *fb;
4779958c857c760eec76f4fdf288b6f33a1c3b41833Philipp Reisner
4788747d30af97232f9ff4cde78b8d259cc715a9b7aLars Ellenberg	fb = kzalloc(sizeof(struct fifo_buffer) + sizeof(int) * fifo_size, GFP_NOIO);
4799958c857c760eec76f4fdf288b6f33a1c3b41833Philipp Reisner	if (!fb)
4809958c857c760eec76f4fdf288b6f33a1c3b41833Philipp Reisner		return NULL;
4819958c857c760eec76f4fdf288b6f33a1c3b41833Philipp Reisner
4829958c857c760eec76f4fdf288b6f33a1c3b41833Philipp Reisner	fb->head_index = 0;
4839958c857c760eec76f4fdf288b6f33a1c3b41833Philipp Reisner	fb->size = fifo_size;
4849958c857c760eec76f4fdf288b6f33a1c3b41833Philipp Reisner	fb->total = 0;
4859958c857c760eec76f4fdf288b6f33a1c3b41833Philipp Reisner
4869958c857c760eec76f4fdf288b6f33a1c3b41833Philipp Reisner	return fb;
4879958c857c760eec76f4fdf288b6f33a1c3b41833Philipp Reisner}
4889958c857c760eec76f4fdf288b6f33a1c3b41833Philipp Reisner
489b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacherstatic int drbd_rs_controller(struct drbd_device *device)
490778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner{
491daeda1cca91d58bb6c8e45f6734f021bab9c28b7Philipp Reisner	struct disk_conf *dc;
492778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	unsigned int sect_in;  /* Number of sectors that came in since the last turn */
493778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	unsigned int want;     /* The number of sectors we want in the proxy */
494778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	int req_sect; /* Number of sectors to request in this turn */
495778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	int correction; /* Number of sectors more we need in the proxy*/
496778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	int cps; /* correction per invocation of drbd_rs_controller() */
497778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	int steps; /* Number of time steps to plan ahead */
498778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	int curr_corr;
499778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	int max_sect;
500813472ced7fac734157fe5be1137ce2bac942902Philipp Reisner	struct fifo_buffer *plan;
501778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner
502b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	sect_in = atomic_xchg(&device->rs_sect_in, 0); /* Number of sectors that came in */
503b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	device->rs_in_flight -= sect_in;
504778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner
505b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	dc = rcu_dereference(device->ldev->disk_conf);
506b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	plan = rcu_dereference(device->rs_plan_s);
507778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner
508813472ced7fac734157fe5be1137ce2bac942902Philipp Reisner	steps = plan->size; /* (dc->c_plan_ahead * 10 * SLEEP_TIME) / HZ; */
509778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner
510b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	if (device->rs_in_flight + sect_in == 0) { /* At start of resync */
511daeda1cca91d58bb6c8e45f6734f021bab9c28b7Philipp Reisner		want = ((dc->resync_rate * 2 * SLEEP_TIME) / HZ) * steps;
512778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	} else { /* normal path */
513daeda1cca91d58bb6c8e45f6734f021bab9c28b7Philipp Reisner		want = dc->c_fill_target ? dc->c_fill_target :
514daeda1cca91d58bb6c8e45f6734f021bab9c28b7Philipp Reisner			sect_in * dc->c_delay_target * HZ / (SLEEP_TIME * 10);
515778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	}
516778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner
517b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	correction = want - device->rs_in_flight - plan->total;
518778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner
519778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	/* Plan ahead */
520778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	cps = correction / steps;
521813472ced7fac734157fe5be1137ce2bac942902Philipp Reisner	fifo_add_val(plan, cps);
522813472ced7fac734157fe5be1137ce2bac942902Philipp Reisner	plan->total += cps * steps;
523778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner
524778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	/* What we do in this step */
525813472ced7fac734157fe5be1137ce2bac942902Philipp Reisner	curr_corr = fifo_push(plan, 0);
526813472ced7fac734157fe5be1137ce2bac942902Philipp Reisner	plan->total -= curr_corr;
527778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner
528778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	req_sect = sect_in + curr_corr;
529778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	if (req_sect < 0)
530778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner		req_sect = 0;
531778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner
532daeda1cca91d58bb6c8e45f6734f021bab9c28b7Philipp Reisner	max_sect = (dc->c_max_rate * 2 * SLEEP_TIME) / HZ;
533778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	if (req_sect > max_sect)
534778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner		req_sect = max_sect;
535778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner
536778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	/*
537778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	dev_warn(DEV, "si=%u if=%d wa=%u co=%d st=%d cps=%d pl=%d cc=%d rs=%d\n",
538b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		 sect_in, device->rs_in_flight, want, correction,
539b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		 steps, cps, device->rs_planed, curr_corr, req_sect);
540778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	*/
541778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner
542778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	return req_sect;
543778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner}
544778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner
545b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacherstatic int drbd_rs_number_requests(struct drbd_device *device)
546e65f440d474d7d6a6fd8a2c844e851d8c96ed9c5Lars Ellenberg{
547e65f440d474d7d6a6fd8a2c844e851d8c96ed9c5Lars Ellenberg	int number;
548813472ced7fac734157fe5be1137ce2bac942902Philipp Reisner
549813472ced7fac734157fe5be1137ce2bac942902Philipp Reisner	rcu_read_lock();
550b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	if (rcu_dereference(device->rs_plan_s)->size) {
551b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		number = drbd_rs_controller(device) >> (BM_BLOCK_SHIFT - 9);
552b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		device->c_sync_rate = number * HZ * (BM_BLOCK_SIZE / 1024) / SLEEP_TIME;
553e65f440d474d7d6a6fd8a2c844e851d8c96ed9c5Lars Ellenberg	} else {
554b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		device->c_sync_rate = rcu_dereference(device->ldev->disk_conf)->resync_rate;
555b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		number = SLEEP_TIME * device->c_sync_rate  / ((BM_BLOCK_SIZE / 1024) * HZ);
556e65f440d474d7d6a6fd8a2c844e851d8c96ed9c5Lars Ellenberg	}
557813472ced7fac734157fe5be1137ce2bac942902Philipp Reisner	rcu_read_unlock();
558e65f440d474d7d6a6fd8a2c844e851d8c96ed9c5Lars Ellenberg
559e65f440d474d7d6a6fd8a2c844e851d8c96ed9c5Lars Ellenberg	/* ignore the amount of pending requests, the resync controller should
560e65f440d474d7d6a6fd8a2c844e851d8c96ed9c5Lars Ellenberg	 * throttle down to incoming reply rate soon enough anyways. */
561e65f440d474d7d6a6fd8a2c844e851d8c96ed9c5Lars Ellenberg	return number;
562e65f440d474d7d6a6fd8a2c844e851d8c96ed9c5Lars Ellenberg}
563e65f440d474d7d6a6fd8a2c844e851d8c96ed9c5Lars Ellenberg
56499920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacherint w_make_resync_request(struct drbd_work *w, int cancel)
565b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
566b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	struct drbd_device *device = w->device;
567b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	unsigned long bit;
568b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	sector_t sector;
569b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	const sector_t capacity = drbd_get_capacity(device->this_bdev);
5701816a2b47afae838e53a177d5d166cc7be97d6b5Lars Ellenberg	int max_bio_size;
571e65f440d474d7d6a6fd8a2c844e851d8c96ed9c5Lars Ellenberg	int number, rollback_i, size;
572b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	int align, queued, sndbuf;
5730f0601f4ea2f53cfd8bcae060fb03d9bbde070ecLars Ellenberg	int i = 0;
574b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
575b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (unlikely(cancel))
57699920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacher		return 0;
577b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
578b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	if (device->rs_total == 0) {
579af85e8e83d160f72a10e4467852646ac08614260Lars Ellenberg		/* empty resync? */
580b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		drbd_resync_finished(device);
58199920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacher		return 0;
582af85e8e83d160f72a10e4467852646ac08614260Lars Ellenberg	}
583af85e8e83d160f72a10e4467852646ac08614260Lars Ellenberg
584b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	if (!get_ldev(device)) {
585b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		/* Since we only need to access device->rsync a
586b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		   get_ldev_if_state(device,D_FAILED) would be sufficient, but
587b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		   to continue resync with a broken disk makes no sense at
588b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		   all */
589b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		dev_err(DEV, "Disk broke down during resync!\n");
59099920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacher		return 0;
591b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
592b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
593b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	max_bio_size = queue_max_hw_sectors(device->rq_queue) << 9;
594b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	number = drbd_rs_number_requests(device);
595e65f440d474d7d6a6fd8a2c844e851d8c96ed9c5Lars Ellenberg	if (number == 0)
5960f0601f4ea2f53cfd8bcae060fb03d9bbde070ecLars Ellenberg		goto requeue;
597b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
598b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	for (i = 0; i < number; i++) {
599b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		/* Stop generating RS requests, when half of the send buffer is filled */
600a6b32bc3cebd3fb6848c526763733b9dbc389c02Andreas Gruenbacher		mutex_lock(&first_peer_device(device)->connection->data.mutex);
601a6b32bc3cebd3fb6848c526763733b9dbc389c02Andreas Gruenbacher		if (first_peer_device(device)->connection->data.socket) {
602a6b32bc3cebd3fb6848c526763733b9dbc389c02Andreas Gruenbacher			queued = first_peer_device(device)->connection->data.socket->sk->sk_wmem_queued;
603a6b32bc3cebd3fb6848c526763733b9dbc389c02Andreas Gruenbacher			sndbuf = first_peer_device(device)->connection->data.socket->sk->sk_sndbuf;
604b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		} else {
605b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			queued = 1;
606b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			sndbuf = 0;
607b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
608a6b32bc3cebd3fb6848c526763733b9dbc389c02Andreas Gruenbacher		mutex_unlock(&first_peer_device(device)->connection->data.mutex);
609b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (queued > sndbuf / 2)
610b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			goto requeue;
611b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
612b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisnernext_sector:
613b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		size = BM_BLOCK_SIZE;
614b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		bit  = drbd_bm_find_next(device, device->bm_resync_fo);
615b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
6164b0715f09655e76ca24c35a9e25e7c464c2f7346Lars Ellenberg		if (bit == DRBD_END_OF_BITMAP) {
617b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher			device->bm_resync_fo = drbd_bm_bits(device);
618b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher			put_ldev(device);
61999920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacher			return 0;
620b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
621b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
622b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		sector = BM_BIT_TO_SECT(bit);
623b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
624b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		if (drbd_rs_should_slow_down(device, sector) ||
625b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		    drbd_try_rs_begin_io(device, sector)) {
626b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher			device->bm_resync_fo = bit;
627b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			goto requeue;
628b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
629b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		device->bm_resync_fo = bit + 1;
630b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
631b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		if (unlikely(drbd_bm_test_bit(device, bit) == 0)) {
632b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher			drbd_rs_complete_io(device, sector);
633b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			goto next_sector;
634b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
635b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
6361816a2b47afae838e53a177d5d166cc7be97d6b5Lars Ellenberg#if DRBD_MAX_BIO_SIZE > BM_BLOCK_SIZE
637b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		/* try to find some adjacent bits.
638b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		 * we stop if we have already the maximum req size.
639b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		 *
640b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		 * Additionally always align bigger requests, in order to
641b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		 * be prepared for all stripe sizes of software RAIDs.
642b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		 */
643b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		align = 1;
644d207450cf2731c6a2afa8c78fb31c7206cd35ebaPhilipp Reisner		rollback_i = i;
645b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		for (;;) {
6461816a2b47afae838e53a177d5d166cc7be97d6b5Lars Ellenberg			if (size + BM_BLOCK_SIZE > max_bio_size)
647b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner				break;
648b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
649b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			/* Be always aligned */
650b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			if (sector & ((1<<(align+3))-1))
651b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner				break;
652b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
653b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			/* do not cross extent boundaries */
654b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			if (((bit+1) & BM_BLOCKS_PER_BM_EXT_MASK) == 0)
655b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner				break;
656b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			/* now, is it actually dirty, after all?
657b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			 * caution, drbd_bm_test_bit is tri-state for some
658b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			 * obscure reason; ( b == 0 ) would get the out-of-band
659b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			 * only accidentally right because of the "oddly sized"
660b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			 * adjustment below */
661b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher			if (drbd_bm_test_bit(device, bit+1) != 1)
662b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner				break;
663b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			bit++;
664b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			size += BM_BLOCK_SIZE;
665b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			if ((BM_BLOCK_SIZE << align) <= size)
666b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner				align++;
667b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			i++;
668b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
669b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		/* if we merged some,
670b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		 * reset the offset to start the next drbd_bm_find_next from */
671b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (size > BM_BLOCK_SIZE)
672b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher			device->bm_resync_fo = bit + 1;
673b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner#endif
674b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
675b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		/* adjust very last sectors, in case we are oddly sized */
676b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (sector + (size>>9) > capacity)
677b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			size = (capacity-sector)<<9;
678a6b32bc3cebd3fb6848c526763733b9dbc389c02Andreas Gruenbacher		if (first_peer_device(device)->connection->agreed_pro_version >= 89 &&
679a6b32bc3cebd3fb6848c526763733b9dbc389c02Andreas Gruenbacher		    first_peer_device(device)->connection->csums_tfm) {
680b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher			switch (read_for_csum(device, sector, size)) {
68180a40e439e5a3f30b0a6210a1add6d7c33392e54Lars Ellenberg			case -EIO: /* Disk failure */
682b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher				put_ldev(device);
68399920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacher				return -EIO;
68480a40e439e5a3f30b0a6210a1add6d7c33392e54Lars Ellenberg			case -EAGAIN: /* allocation failed, or ldev busy */
685b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher				drbd_rs_complete_io(device, sector);
686b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher				device->bm_resync_fo = BM_SECT_TO_BIT(sector);
687d207450cf2731c6a2afa8c78fb31c7206cd35ebaPhilipp Reisner				i = rollback_i;
688b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner				goto requeue;
68980a40e439e5a3f30b0a6210a1add6d7c33392e54Lars Ellenberg			case 0:
69080a40e439e5a3f30b0a6210a1add6d7c33392e54Lars Ellenberg				/* everything ok */
69180a40e439e5a3f30b0a6210a1add6d7c33392e54Lars Ellenberg				break;
69280a40e439e5a3f30b0a6210a1add6d7c33392e54Lars Ellenberg			default:
69380a40e439e5a3f30b0a6210a1add6d7c33392e54Lars Ellenberg				BUG();
694b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			}
695b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		} else {
69699920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacher			int err;
69799920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacher
698b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher			inc_rs_pending(device);
699b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher			err = drbd_send_drequest(device, P_RS_DATA_REQUEST,
70099920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacher						 sector, size, ID_SYNCER);
70199920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacher			if (err) {
702b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner				dev_err(DEV, "drbd_send_drequest() failed, aborting...\n");
703b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher				dec_rs_pending(device);
704b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher				put_ldev(device);
70599920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacher				return err;
706b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			}
707b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
708b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
709b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
710b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	if (device->bm_resync_fo >= drbd_bm_bits(device)) {
711b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		/* last syncer _request_ was sent,
712b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		 * but the P_RS_DATA_REPLY not yet received.  sync will end (and
713b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		 * next sync group will resume), as soon as we receive the last
714b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		 * resync data block, and the last bit is cleared.
715b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		 * until then resync "work" is "inactive" ...
716b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		 */
717b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		put_ldev(device);
71899920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacher		return 0;
719b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
720b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
721b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner requeue:
722b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	device->rs_in_flight += (i << (BM_BLOCK_SHIFT - 9));
723b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	mod_timer(&device->resync_timer, jiffies + SLEEP_TIME);
724b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	put_ldev(device);
72599920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacher	return 0;
726b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
727b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
72800d56944ff086f895e9ad184a7785ca1eece4a3bPhilipp Reisnerstatic int w_make_ov_request(struct drbd_work *w, int cancel)
729b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
730b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	struct drbd_device *device = w->device;
731b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	int number, i, size;
732b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	sector_t sector;
733b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	const sector_t capacity = drbd_get_capacity(device->this_bdev);
73458ffa580a748dd16b1e5ab260bea39cdbd1e94efLars Ellenberg	bool stop_sector_reached = false;
735b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
736b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (unlikely(cancel))
737b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		return 1;
738b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
739b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	number = drbd_rs_number_requests(device);
740b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
741b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	sector = device->ov_position;
742b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	for (i = 0; i < number; i++) {
74358ffa580a748dd16b1e5ab260bea39cdbd1e94efLars Ellenberg		if (sector >= capacity)
744b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			return 1;
74558ffa580a748dd16b1e5ab260bea39cdbd1e94efLars Ellenberg
74658ffa580a748dd16b1e5ab260bea39cdbd1e94efLars Ellenberg		/* We check for "finished" only in the reply path:
74758ffa580a748dd16b1e5ab260bea39cdbd1e94efLars Ellenberg		 * w_e_end_ov_reply().
74858ffa580a748dd16b1e5ab260bea39cdbd1e94efLars Ellenberg		 * We need to send at least one request out. */
74958ffa580a748dd16b1e5ab260bea39cdbd1e94efLars Ellenberg		stop_sector_reached = i > 0
750b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher			&& verify_can_do_stop_sector(device)
751b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher			&& sector >= device->ov_stop_sector;
75258ffa580a748dd16b1e5ab260bea39cdbd1e94efLars Ellenberg		if (stop_sector_reached)
75358ffa580a748dd16b1e5ab260bea39cdbd1e94efLars Ellenberg			break;
754b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
755b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		size = BM_BLOCK_SIZE;
756b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
757b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		if (drbd_rs_should_slow_down(device, sector) ||
758b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		    drbd_try_rs_begin_io(device, sector)) {
759b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher			device->ov_position = sector;
760b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			goto requeue;
761b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
762b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
763b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (sector + (size>>9) > capacity)
764b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			size = (capacity-sector)<<9;
765b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
766b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		inc_rs_pending(device);
767b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		if (drbd_send_ov_request(device, sector, size)) {
768b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher			dec_rs_pending(device);
769b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			return 0;
770b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
771b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		sector += BM_SECT_PER_BIT;
772b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
773b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	device->ov_position = sector;
774b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
775b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner requeue:
776b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	device->rs_in_flight += (i << (BM_BLOCK_SHIFT - 9));
77758ffa580a748dd16b1e5ab260bea39cdbd1e94efLars Ellenberg	if (i == 0 || !stop_sector_reached)
778b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		mod_timer(&device->resync_timer, jiffies + SLEEP_TIME);
779b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	return 1;
780b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
781b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
78299920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacherint w_ov_finished(struct drbd_work *w, int cancel)
783b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
784b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	struct drbd_device *device = w->device;
785b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	kfree(w);
786b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	ov_out_of_sync_print(device);
787b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	drbd_resync_finished(device);
788b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
78999920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacher	return 0;
790b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
791b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
79299920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacherstatic int w_resync_finished(struct drbd_work *w, int cancel)
793b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
794b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	struct drbd_device *device = w->device;
795b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	kfree(w);
796b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
797b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	drbd_resync_finished(device);
798b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
79999920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacher	return 0;
800b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
801b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
802b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacherstatic void ping_peer(struct drbd_device *device)
803af85e8e83d160f72a10e4467852646ac08614260Lars Ellenberg{
804a6b32bc3cebd3fb6848c526763733b9dbc389c02Andreas Gruenbacher	struct drbd_connection *connection = first_peer_device(device)->connection;
8052a67d8b93b3363d4a5608d16d510a4bf6b3863fbPhilipp Reisner
806bde89a9e151b482765ed40e04307a6190236b387Andreas Gruenbacher	clear_bit(GOT_PING_ACK, &connection->flags);
807bde89a9e151b482765ed40e04307a6190236b387Andreas Gruenbacher	request_ping(connection);
808bde89a9e151b482765ed40e04307a6190236b387Andreas Gruenbacher	wait_event(connection->ping_wait,
809bde89a9e151b482765ed40e04307a6190236b387Andreas Gruenbacher		   test_bit(GOT_PING_ACK, &connection->flags) || device->state.conn < C_CONNECTED);
810af85e8e83d160f72a10e4467852646ac08614260Lars Ellenberg}
811af85e8e83d160f72a10e4467852646ac08614260Lars Ellenberg
812b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacherint drbd_resync_finished(struct drbd_device *device)
813b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
814b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	unsigned long db, dt, dbdt;
815b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	unsigned long n_oos;
816b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	union drbd_state os, ns;
817b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	struct drbd_work *w;
818b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	char *khelper_cmd = NULL;
81926525618863afcc4aab8b2a83451d37c6f513460Lars Ellenberg	int verify_done = 0;
820b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
821b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	/* Remove all elements from the resync LRU. Since future actions
822b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	 * might set bits in the (main) bitmap, then the entries in the
823b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	 * resync LRU would be wrong. */
824b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	if (drbd_rs_del_all(device)) {
825b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		/* In case this is not possible now, most probably because
826b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		 * there are P_RS_DATA_REPLY Packets lingering on the worker's
827b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		 * queue (or even the read operations for those packets
828b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		 * is not finished by now).   Retry in 100ms. */
829b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
83020ee639024e3d33111df0e343050b218c656bf16Philipp Reisner		schedule_timeout_interruptible(HZ / 10);
831b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		w = kmalloc(sizeof(struct drbd_work), GFP_ATOMIC);
832b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (w) {
833b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			w->cb = w_resync_finished;
834b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher			w->device = device;
835a6b32bc3cebd3fb6848c526763733b9dbc389c02Andreas Gruenbacher			drbd_queue_work(&first_peer_device(device)->connection->sender_work, w);
836b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			return 1;
837b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
838b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		dev_err(DEV, "Warn failed to drbd_rs_del_all() and to kmalloc(w).\n");
839b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
840b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
841b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	dt = (jiffies - device->rs_start - device->rs_paused) / HZ;
842b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (dt <= 0)
843b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		dt = 1;
84458ffa580a748dd16b1e5ab260bea39cdbd1e94efLars Ellenberg
845b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	db = device->rs_total;
84658ffa580a748dd16b1e5ab260bea39cdbd1e94efLars Ellenberg	/* adjust for verify start and stop sectors, respective reached position */
847b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	if (device->state.conn == C_VERIFY_S || device->state.conn == C_VERIFY_T)
848b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		db -= device->ov_left;
84958ffa580a748dd16b1e5ab260bea39cdbd1e94efLars Ellenberg
850b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	dbdt = Bit2KB(db/dt);
851b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	device->rs_paused /= HZ;
852b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
853b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	if (!get_ldev(device))
854b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		goto out;
855b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
856b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	ping_peer(device);
857af85e8e83d160f72a10e4467852646ac08614260Lars Ellenberg
858a6b32bc3cebd3fb6848c526763733b9dbc389c02Andreas Gruenbacher	spin_lock_irq(&first_peer_device(device)->connection->req_lock);
859b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	os = drbd_read_state(device);
860b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
86126525618863afcc4aab8b2a83451d37c6f513460Lars Ellenberg	verify_done = (os.conn == C_VERIFY_S || os.conn == C_VERIFY_T);
86226525618863afcc4aab8b2a83451d37c6f513460Lars Ellenberg
863b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	/* This protects us against multiple calls (that can happen in the presence
864b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	   of application IO), and against connectivity loss just before we arrive here. */
865b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (os.conn <= C_CONNECTED)
866b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		goto out_unlock;
867b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
868b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	ns = os;
869b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	ns.conn = C_CONNECTED;
870b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
871b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	dev_info(DEV, "%s done (total %lu sec; paused %lu sec; %lu K/sec)\n",
87258ffa580a748dd16b1e5ab260bea39cdbd1e94efLars Ellenberg	     verify_done ? "Online verify" : "Resync",
873b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	     dt + device->rs_paused, device->rs_paused, dbdt);
874b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
875b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	n_oos = drbd_bm_total_weight(device);
876b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
877b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (os.conn == C_VERIFY_S || os.conn == C_VERIFY_T) {
878b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (n_oos) {
879b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			dev_alert(DEV, "Online verify found %lu %dk block out of sync!\n",
880b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			      n_oos, Bit2KB(1));
881b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			khelper_cmd = "out-of-sync";
882b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
883b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	} else {
884b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		D_ASSERT((n_oos - device->rs_failed) == 0);
885b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
886b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (os.conn == C_SYNC_TARGET || os.conn == C_PAUSED_SYNC_T)
887b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			khelper_cmd = "after-resync-target";
888b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
889a6b32bc3cebd3fb6848c526763733b9dbc389c02Andreas Gruenbacher		if (first_peer_device(device)->connection->csums_tfm && device->rs_total) {
890b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher			const unsigned long s = device->rs_same_csum;
891b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher			const unsigned long t = device->rs_total;
892b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			const int ratio =
893b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner				(t == 0)     ? 0 :
894b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			(t < 100000) ? ((s*100)/t) : (s/(t/100));
89524c4830c8ec3cbc904d84c213126a35f41a4e455Bart Van Assche			dev_info(DEV, "%u %% had equal checksums, eliminated: %luK; "
896b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			     "transferred %luK total %luK\n",
897b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			     ratio,
898b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher			     Bit2KB(device->rs_same_csum),
899b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher			     Bit2KB(device->rs_total - device->rs_same_csum),
900b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher			     Bit2KB(device->rs_total));
901b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
902b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
903b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
904b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	if (device->rs_failed) {
905b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		dev_info(DEV, "            %lu failed blocks\n", device->rs_failed);
906b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
907b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (os.conn == C_SYNC_TARGET || os.conn == C_PAUSED_SYNC_T) {
908b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			ns.disk = D_INCONSISTENT;
909b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			ns.pdsk = D_UP_TO_DATE;
910b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		} else {
911b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			ns.disk = D_UP_TO_DATE;
912b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			ns.pdsk = D_INCONSISTENT;
913b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
914b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	} else {
915b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		ns.disk = D_UP_TO_DATE;
916b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		ns.pdsk = D_UP_TO_DATE;
917b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
918b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (os.conn == C_SYNC_TARGET || os.conn == C_PAUSED_SYNC_T) {
919b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher			if (device->p_uuid) {
920b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner				int i;
921b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner				for (i = UI_BITMAP ; i <= UI_HISTORY_END ; i++)
922b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher					_drbd_uuid_set(device, i, device->p_uuid[i]);
923b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher				drbd_uuid_set(device, UI_BITMAP, device->ldev->md.uuid[UI_CURRENT]);
924b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher				_drbd_uuid_set(device, UI_CURRENT, device->p_uuid[UI_CURRENT]);
925b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			} else {
926b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher				dev_err(DEV, "device->p_uuid is NULL! BUG\n");
927b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			}
928b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
929b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
93062b0da3a244ac33d25a77861ef1cc0080103f2ffLars Ellenberg		if (!(os.conn == C_VERIFY_S || os.conn == C_VERIFY_T)) {
93162b0da3a244ac33d25a77861ef1cc0080103f2ffLars Ellenberg			/* for verify runs, we don't update uuids here,
93262b0da3a244ac33d25a77861ef1cc0080103f2ffLars Ellenberg			 * so there would be nothing to report. */
933b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher			drbd_uuid_set_bm(device, 0UL);
934b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher			drbd_print_uuids(device, "updated UUIDs");
935b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher			if (device->p_uuid) {
93662b0da3a244ac33d25a77861ef1cc0080103f2ffLars Ellenberg				/* Now the two UUID sets are equal, update what we
93762b0da3a244ac33d25a77861ef1cc0080103f2ffLars Ellenberg				 * know of the peer. */
93862b0da3a244ac33d25a77861ef1cc0080103f2ffLars Ellenberg				int i;
93962b0da3a244ac33d25a77861ef1cc0080103f2ffLars Ellenberg				for (i = UI_CURRENT ; i <= UI_HISTORY_END ; i++)
940b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher					device->p_uuid[i] = device->ldev->md.uuid[i];
94162b0da3a244ac33d25a77861ef1cc0080103f2ffLars Ellenberg			}
942b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
943b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
944b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
945b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	_drbd_set_state(device, ns, CS_VERBOSE, NULL);
946b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisnerout_unlock:
947a6b32bc3cebd3fb6848c526763733b9dbc389c02Andreas Gruenbacher	spin_unlock_irq(&first_peer_device(device)->connection->req_lock);
948b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	put_ldev(device);
949b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisnerout:
950b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	device->rs_total  = 0;
951b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	device->rs_failed = 0;
952b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	device->rs_paused = 0;
95358ffa580a748dd16b1e5ab260bea39cdbd1e94efLars Ellenberg
95458ffa580a748dd16b1e5ab260bea39cdbd1e94efLars Ellenberg	/* reset start sector, if we reached end of device */
955b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	if (verify_done && device->ov_left == 0)
956b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		device->ov_start_sector = 0;
957b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
958b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	drbd_md_sync(device);
95913d42685bec1f012dcbc5d187490eb1d15ec8219Lars Ellenberg
960b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (khelper_cmd)
961b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		drbd_khelper(device, khelper_cmd);
962b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
963b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	return 1;
964b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
965b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
966b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner/* helper */
967b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacherstatic void move_to_net_ee_or_free(struct drbd_device *device, struct drbd_peer_request *peer_req)
968b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
969045417f75c718a4ac97fd44106b8aafcbca5a6daAndreas Gruenbacher	if (drbd_peer_req_has_active_page(peer_req)) {
970b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		/* This might happen if sendpage() has not finished */
971db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher		int i = (peer_req->i.size + PAGE_SIZE -1) >> PAGE_SHIFT;
972b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		atomic_add(i, &device->pp_in_use_by_net);
973b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		atomic_sub(i, &device->pp_in_use);
974a6b32bc3cebd3fb6848c526763733b9dbc389c02Andreas Gruenbacher		spin_lock_irq(&first_peer_device(device)->connection->req_lock);
975b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		list_add_tail(&peer_req->w.list, &device->net_ee);
976a6b32bc3cebd3fb6848c526763733b9dbc389c02Andreas Gruenbacher		spin_unlock_irq(&first_peer_device(device)->connection->req_lock);
977435f07402b3165b90592073bc0f8c6f8fa160ff9Lars Ellenberg		wake_up(&drbd_pp_wait);
978b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	} else
979b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		drbd_free_peer_req(device, peer_req);
980b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
981b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
982b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner/**
983b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * w_e_end_data_req() - Worker callback, to send a P_DATA_REPLY packet in response to a P_DATA_REQUEST
984b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher * @device:	DRBD device.
985b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * @w:		work object.
986b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * @cancel:	The connection will be closed anyways
987b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner */
98899920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacherint w_e_end_data_req(struct drbd_work *w, int cancel)
989b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
990db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher	struct drbd_peer_request *peer_req = container_of(w, struct drbd_peer_request, w);
991b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	struct drbd_device *device = w->device;
99299920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacher	int err;
993b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
994b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (unlikely(cancel)) {
995b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		drbd_free_peer_req(device, peer_req);
996b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		dec_unacked(device);
99799920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacher		return 0;
998b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
999b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1000db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher	if (likely((peer_req->flags & EE_WAS_ERROR) == 0)) {
1001b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		err = drbd_send_block(device, P_DATA_REPLY, peer_req);
1002b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	} else {
1003b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (__ratelimit(&drbd_ratelimit_state))
1004b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			dev_err(DEV, "Sending NegDReply. sector=%llus.\n",
1005db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher			    (unsigned long long)peer_req->i.sector);
1006b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1007b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		err = drbd_send_ack(device, P_NEG_DREPLY, peer_req);
1008b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
1009b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1010b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	dec_unacked(device);
1011b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1012b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	move_to_net_ee_or_free(device, peer_req);
1013b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
101499920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacher	if (unlikely(err))
1015b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		dev_err(DEV, "drbd_send_block() failed\n");
101699920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacher	return err;
1017b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
1018b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1019b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner/**
1020a209b4aec31d4b672b7a70f5de272ebf6ce40e1bAndreas Gruenbacher * w_e_end_rsdata_req() - Worker callback to send a P_RS_DATA_REPLY packet in response to a P_RS_DATA_REQUEST
1021b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher * @device:	DRBD device.
1022b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * @w:		work object.
1023b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * @cancel:	The connection will be closed anyways
1024b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner */
102599920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacherint w_e_end_rsdata_req(struct drbd_work *w, int cancel)
1026b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
1027db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher	struct drbd_peer_request *peer_req = container_of(w, struct drbd_peer_request, w);
1028b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	struct drbd_device *device = w->device;
102999920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacher	int err;
1030b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1031b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (unlikely(cancel)) {
1032b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		drbd_free_peer_req(device, peer_req);
1033b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		dec_unacked(device);
103499920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacher		return 0;
1035b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
1036b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1037b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	if (get_ldev_if_state(device, D_FAILED)) {
1038b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		drbd_rs_complete_io(device, peer_req->i.sector);
1039b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		put_ldev(device);
1040b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
1041b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1042b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	if (device->state.conn == C_AHEAD) {
1043b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		err = drbd_send_ack(device, P_RS_CANCEL, peer_req);
1044db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher	} else if (likely((peer_req->flags & EE_WAS_ERROR) == 0)) {
1045b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		if (likely(device->state.pdsk >= D_INCONSISTENT)) {
1046b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher			inc_rs_pending(device);
1047b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher			err = drbd_send_block(device, P_RS_DATA_REPLY, peer_req);
1048b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		} else {
1049b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			if (__ratelimit(&drbd_ratelimit_state))
1050b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner				dev_err(DEV, "Not sending RSDataReply, "
1051b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner				    "partner DISKLESS!\n");
105299920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacher			err = 0;
1053b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
1054b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	} else {
1055b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (__ratelimit(&drbd_ratelimit_state))
1056b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			dev_err(DEV, "Sending NegRSDReply. sector %llus.\n",
1057db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher			    (unsigned long long)peer_req->i.sector);
1058b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1059b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		err = drbd_send_ack(device, P_NEG_RS_DREPLY, peer_req);
1060b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1061b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		/* update resync data with failure */
1062b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		drbd_rs_failed_io(device, peer_req->i.sector, peer_req->i.size);
1063b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
1064b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1065b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	dec_unacked(device);
1066b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1067b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	move_to_net_ee_or_free(device, peer_req);
1068b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
106999920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacher	if (unlikely(err))
1070b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		dev_err(DEV, "drbd_send_block() failed\n");
107199920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacher	return err;
1072b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
1073b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
107499920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacherint w_e_end_csum_rs_req(struct drbd_work *w, int cancel)
1075b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
1076db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher	struct drbd_peer_request *peer_req = container_of(w, struct drbd_peer_request, w);
1077b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	struct drbd_device *device = w->device;
1078b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	struct digest_info *di;
1079b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	int digest_size;
1080b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	void *digest = NULL;
108199920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacher	int err, eq = 0;
1082b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1083b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (unlikely(cancel)) {
1084b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		drbd_free_peer_req(device, peer_req);
1085b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		dec_unacked(device);
108699920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacher		return 0;
1087b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
1088b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1089b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	if (get_ldev(device)) {
1090b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		drbd_rs_complete_io(device, peer_req->i.sector);
1091b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		put_ldev(device);
10921d53f09e170e477de67babd7a10e277479260d51Lars Ellenberg	}
1093b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1094db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher	di = peer_req->digest;
1095b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1096db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher	if (likely((peer_req->flags & EE_WAS_ERROR) == 0)) {
1097b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		/* quick hack to try to avoid a race against reconfiguration.
1098b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		 * a real fix would be much more involved,
1099b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		 * introducing more locking mechanisms */
1100a6b32bc3cebd3fb6848c526763733b9dbc389c02Andreas Gruenbacher		if (first_peer_device(device)->connection->csums_tfm) {
1101a6b32bc3cebd3fb6848c526763733b9dbc389c02Andreas Gruenbacher			digest_size = crypto_hash_digestsize(first_peer_device(device)->connection->csums_tfm);
1102b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			D_ASSERT(digest_size == di->digest_size);
1103b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			digest = kmalloc(digest_size, GFP_NOIO);
1104b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
1105b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (digest) {
1106a6b32bc3cebd3fb6848c526763733b9dbc389c02Andreas Gruenbacher			drbd_csum_ee(device, first_peer_device(device)->connection->csums_tfm, peer_req, digest);
1107b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			eq = !memcmp(digest, di->digest, digest_size);
1108b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			kfree(digest);
1109b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
1110b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1111b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (eq) {
1112b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher			drbd_set_in_sync(device, peer_req->i.sector, peer_req->i.size);
1113676396d545350a70d922605ec23c2ed26124334aLars Ellenberg			/* rs_same_csums unit is BM_BLOCK_SIZE */
1114b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher			device->rs_same_csum += peer_req->i.size >> BM_BLOCK_SHIFT;
1115b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher			err = drbd_send_ack(device, P_RS_IS_IN_SYNC, peer_req);
1116b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		} else {
1117b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher			inc_rs_pending(device);
1118db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher			peer_req->block_id = ID_SYNCER; /* By setting block_id, digest pointer becomes invalid! */
1119db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher			peer_req->flags &= ~EE_HAS_DIGEST; /* This peer request no longer has a digest pointer */
1120204bba9965c4cc175bf5bc65ddd19889e9085c72Philipp Reisner			kfree(di);
1121b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher			err = drbd_send_block(device, P_RS_DATA_REPLY, peer_req);
1122b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
1123b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	} else {
1124b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		err = drbd_send_ack(device, P_NEG_RS_DREPLY, peer_req);
1125b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (__ratelimit(&drbd_ratelimit_state))
1126b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			dev_err(DEV, "Sending NegDReply. I guess it gets messy.\n");
1127b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
1128b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1129b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	dec_unacked(device);
1130b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	move_to_net_ee_or_free(device, peer_req);
1131b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
113299920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacher	if (unlikely(err))
1133b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		dev_err(DEV, "drbd_send_block/ack() failed\n");
113499920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacher	return err;
1135b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
1136b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
113799920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacherint w_e_end_ov_req(struct drbd_work *w, int cancel)
1138b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
1139db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher	struct drbd_peer_request *peer_req = container_of(w, struct drbd_peer_request, w);
1140b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	struct drbd_device *device = w->device;
1141db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher	sector_t sector = peer_req->i.sector;
1142db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher	unsigned int size = peer_req->i.size;
1143b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	int digest_size;
1144b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	void *digest;
114599920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacher	int err = 0;
1146b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1147b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (unlikely(cancel))
1148b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		goto out;
1149b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1150a6b32bc3cebd3fb6848c526763733b9dbc389c02Andreas Gruenbacher	digest_size = crypto_hash_digestsize(first_peer_device(device)->connection->verify_tfm);
1151b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	digest = kmalloc(digest_size, GFP_NOIO);
11528f21420ebd5ca5a751e2f606b49b0acd2a2af314Philipp Reisner	if (!digest) {
115399920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacher		err = 1;	/* terminate the connection in case the allocation failed */
11548f21420ebd5ca5a751e2f606b49b0acd2a2af314Philipp Reisner		goto out;
1155b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
1156b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1157db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher	if (likely(!(peer_req->flags & EE_WAS_ERROR)))
1158a6b32bc3cebd3fb6848c526763733b9dbc389c02Andreas Gruenbacher		drbd_csum_ee(device, first_peer_device(device)->connection->verify_tfm, peer_req, digest);
11598f21420ebd5ca5a751e2f606b49b0acd2a2af314Philipp Reisner	else
11608f21420ebd5ca5a751e2f606b49b0acd2a2af314Philipp Reisner		memset(digest, 0, digest_size);
11618f21420ebd5ca5a751e2f606b49b0acd2a2af314Philipp Reisner
116253ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenberg	/* Free e and pages before send.
116353ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenberg	 * In case we block on congestion, we could otherwise run into
116453ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenberg	 * some distributed deadlock, if the other side blocks on
116553ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenberg	 * congestion as well, because our receiver blocks in
1166c37c8ecfee685fa42de8fd418ad8ca1e66408bd8Andreas Gruenbacher	 * drbd_alloc_pages due to pp_in_use > max_buffers. */
1167b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	drbd_free_peer_req(device, peer_req);
1168db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher	peer_req = NULL;
1169b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	inc_rs_pending(device);
1170b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	err = drbd_send_drequest_csum(device, sector, size, digest, digest_size, P_OV_REPLY);
117199920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacher	if (err)
1172b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		dec_rs_pending(device);
11738f21420ebd5ca5a751e2f606b49b0acd2a2af314Philipp Reisner	kfree(digest);
11748f21420ebd5ca5a751e2f606b49b0acd2a2af314Philipp Reisner
1175b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisnerout:
1176db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher	if (peer_req)
1177b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		drbd_free_peer_req(device, peer_req);
1178b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	dec_unacked(device);
117999920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacher	return err;
1180b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
1181b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1182b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbachervoid drbd_ov_out_of_sync_found(struct drbd_device *device, sector_t sector, int size)
1183b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
1184b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	if (device->ov_last_oos_start + device->ov_last_oos_size == sector) {
1185b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		device->ov_last_oos_size += size>>9;
1186b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	} else {
1187b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		device->ov_last_oos_start = sector;
1188b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		device->ov_last_oos_size = size>>9;
1189b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
1190b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	drbd_set_out_of_sync(device, sector, size);
1191b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
1192b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
119399920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacherint w_e_end_ov_reply(struct drbd_work *w, int cancel)
1194b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
1195db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher	struct drbd_peer_request *peer_req = container_of(w, struct drbd_peer_request, w);
1196b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	struct drbd_device *device = w->device;
1197b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	struct digest_info *di;
1198b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	void *digest;
1199db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher	sector_t sector = peer_req->i.sector;
1200db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher	unsigned int size = peer_req->i.size;
120153ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenberg	int digest_size;
120299920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacher	int err, eq = 0;
120358ffa580a748dd16b1e5ab260bea39cdbd1e94efLars Ellenberg	bool stop_sector_reached = false;
1204b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1205b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (unlikely(cancel)) {
1206b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		drbd_free_peer_req(device, peer_req);
1207b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		dec_unacked(device);
120899920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacher		return 0;
1209b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
1210b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1211b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	/* after "cancel", because after drbd_disconnect/drbd_rs_cancel_all
1212b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	 * the resync lru has been cleaned up already */
1213b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	if (get_ldev(device)) {
1214b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		drbd_rs_complete_io(device, peer_req->i.sector);
1215b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		put_ldev(device);
12161d53f09e170e477de67babd7a10e277479260d51Lars Ellenberg	}
1217b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1218db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher	di = peer_req->digest;
1219b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1220db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher	if (likely((peer_req->flags & EE_WAS_ERROR) == 0)) {
1221a6b32bc3cebd3fb6848c526763733b9dbc389c02Andreas Gruenbacher		digest_size = crypto_hash_digestsize(first_peer_device(device)->connection->verify_tfm);
1222b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		digest = kmalloc(digest_size, GFP_NOIO);
1223b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (digest) {
1224a6b32bc3cebd3fb6848c526763733b9dbc389c02Andreas Gruenbacher			drbd_csum_ee(device, first_peer_device(device)->connection->verify_tfm, peer_req, digest);
1225b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1226b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			D_ASSERT(digest_size == di->digest_size);
1227b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			eq = !memcmp(digest, di->digest, digest_size);
1228b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			kfree(digest);
1229b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
1230b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
1231b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
12329676c760979371701ea5a6f8adb7ce8125c22c7dLars Ellenberg	/* Free peer_req and pages before send.
12339676c760979371701ea5a6f8adb7ce8125c22c7dLars Ellenberg	 * In case we block on congestion, we could otherwise run into
12349676c760979371701ea5a6f8adb7ce8125c22c7dLars Ellenberg	 * some distributed deadlock, if the other side blocks on
12359676c760979371701ea5a6f8adb7ce8125c22c7dLars Ellenberg	 * congestion as well, because our receiver blocks in
1236c37c8ecfee685fa42de8fd418ad8ca1e66408bd8Andreas Gruenbacher	 * drbd_alloc_pages due to pp_in_use > max_buffers. */
1237b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	drbd_free_peer_req(device, peer_req);
1238b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (!eq)
1239b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		drbd_ov_out_of_sync_found(device, sector, size);
1240b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	else
1241b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		ov_out_of_sync_print(device);
1242b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1243b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	err = drbd_send_ack_ex(device, P_OV_RESULT, sector, size,
1244fa79abd893f21f458c74af8bca015aa2ef7486a5Andreas Gruenbacher			       eq ? ID_IN_SYNC : ID_OUT_OF_SYNC);
1245b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1246b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	dec_unacked(device);
1247b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1248b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	--device->ov_left;
1249ea5442aff68c559c951373739201721185191748Lars Ellenberg
1250ea5442aff68c559c951373739201721185191748Lars Ellenberg	/* let's advance progress step marks only for every other megabyte */
1251b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	if ((device->ov_left & 0x200) == 0x200)
1252b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		drbd_advance_rs_marks(device, device->ov_left);
1253ea5442aff68c559c951373739201721185191748Lars Ellenberg
1254b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	stop_sector_reached = verify_can_do_stop_sector(device) &&
1255b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		(sector + (size>>9)) >= device->ov_stop_sector;
125658ffa580a748dd16b1e5ab260bea39cdbd1e94efLars Ellenberg
1257b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	if (device->ov_left == 0 || stop_sector_reached) {
1258b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		ov_out_of_sync_print(device);
1259b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		drbd_resync_finished(device);
1260b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
1261b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
126299920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacher	return err;
1263b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
1264b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
126599920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacherint w_prev_work_done(struct drbd_work *w, int cancel)
1266b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
1267b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	struct drbd_wq_barrier *b = container_of(w, struct drbd_wq_barrier, w);
126800d56944ff086f895e9ad184a7785ca1eece4a3bPhilipp Reisner
1269b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	complete(&b->done);
127099920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacher	return 0;
1271b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
1272b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1273b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg/* FIXME
1274b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg * We need to track the number of pending barrier acks,
1275b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg * and to be able to wait for them.
1276b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg * See also comment in drbd_adm_attach before drbd_suspend_io.
1277b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg */
1278bde89a9e151b482765ed40e04307a6190236b387Andreas Gruenbacherstatic int drbd_send_barrier(struct drbd_connection *connection)
1279b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
12809f5bdc339e3becd85aa8add305d794b0b1ec8996Andreas Gruenbacher	struct p_barrier *p;
1281b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg	struct drbd_socket *sock;
1282b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1283bde89a9e151b482765ed40e04307a6190236b387Andreas Gruenbacher	sock = &connection->data;
1284bde89a9e151b482765ed40e04307a6190236b387Andreas Gruenbacher	p = conn_prepare_command(connection, sock);
12859f5bdc339e3becd85aa8add305d794b0b1ec8996Andreas Gruenbacher	if (!p)
12869f5bdc339e3becd85aa8add305d794b0b1ec8996Andreas Gruenbacher		return -EIO;
1287bde89a9e151b482765ed40e04307a6190236b387Andreas Gruenbacher	p->barrier = connection->send.current_epoch_nr;
1288b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg	p->pad = 0;
1289bde89a9e151b482765ed40e04307a6190236b387Andreas Gruenbacher	connection->send.current_epoch_writes = 0;
1290b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg
1291bde89a9e151b482765ed40e04307a6190236b387Andreas Gruenbacher	return conn_send_command(connection, sock, P_BARRIER, sizeof(*p), NULL, 0);
1292b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
1293b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
129499920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacherint w_send_write_hint(struct drbd_work *w, int cancel)
1295b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
1296b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	struct drbd_device *device = w->device;
12979f5bdc339e3becd85aa8add305d794b0b1ec8996Andreas Gruenbacher	struct drbd_socket *sock;
12989f5bdc339e3becd85aa8add305d794b0b1ec8996Andreas Gruenbacher
1299b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (cancel)
130099920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacher		return 0;
1301a6b32bc3cebd3fb6848c526763733b9dbc389c02Andreas Gruenbacher	sock = &first_peer_device(device)->connection->data;
1302b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	if (!drbd_prepare_command(device, sock))
13039f5bdc339e3becd85aa8add305d794b0b1ec8996Andreas Gruenbacher		return -EIO;
1304b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	return drbd_send_command(device, sock, P_UNPLUG_REMOTE, 0, NULL, 0);
1305b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
1306b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1307bde89a9e151b482765ed40e04307a6190236b387Andreas Gruenbacherstatic void re_init_if_first_write(struct drbd_connection *connection, unsigned int epoch)
13084eb9b3cba00471a01699cceb0f4b1f0cb8111ee2Lars Ellenberg{
1309bde89a9e151b482765ed40e04307a6190236b387Andreas Gruenbacher	if (!connection->send.seen_any_write_yet) {
1310bde89a9e151b482765ed40e04307a6190236b387Andreas Gruenbacher		connection->send.seen_any_write_yet = true;
1311bde89a9e151b482765ed40e04307a6190236b387Andreas Gruenbacher		connection->send.current_epoch_nr = epoch;
1312bde89a9e151b482765ed40e04307a6190236b387Andreas Gruenbacher		connection->send.current_epoch_writes = 0;
13134eb9b3cba00471a01699cceb0f4b1f0cb8111ee2Lars Ellenberg	}
13144eb9b3cba00471a01699cceb0f4b1f0cb8111ee2Lars Ellenberg}
13154eb9b3cba00471a01699cceb0f4b1f0cb8111ee2Lars Ellenberg
1316bde89a9e151b482765ed40e04307a6190236b387Andreas Gruenbacherstatic void maybe_send_barrier(struct drbd_connection *connection, unsigned int epoch)
13174eb9b3cba00471a01699cceb0f4b1f0cb8111ee2Lars Ellenberg{
13184eb9b3cba00471a01699cceb0f4b1f0cb8111ee2Lars Ellenberg	/* re-init if first write on this connection */
1319bde89a9e151b482765ed40e04307a6190236b387Andreas Gruenbacher	if (!connection->send.seen_any_write_yet)
13204eb9b3cba00471a01699cceb0f4b1f0cb8111ee2Lars Ellenberg		return;
1321bde89a9e151b482765ed40e04307a6190236b387Andreas Gruenbacher	if (connection->send.current_epoch_nr != epoch) {
1322bde89a9e151b482765ed40e04307a6190236b387Andreas Gruenbacher		if (connection->send.current_epoch_writes)
1323bde89a9e151b482765ed40e04307a6190236b387Andreas Gruenbacher			drbd_send_barrier(connection);
1324bde89a9e151b482765ed40e04307a6190236b387Andreas Gruenbacher		connection->send.current_epoch_nr = epoch;
13254eb9b3cba00471a01699cceb0f4b1f0cb8111ee2Lars Ellenberg	}
13264eb9b3cba00471a01699cceb0f4b1f0cb8111ee2Lars Ellenberg}
13274eb9b3cba00471a01699cceb0f4b1f0cb8111ee2Lars Ellenberg
13288f7bed77740c7418074e6ba82c646a7dd035e6cfAndreas Gruenbacherint w_send_out_of_sync(struct drbd_work *w, int cancel)
132973a01a18b9c28a0fab1131ece5b0a9bc00a879b8Philipp Reisner{
133073a01a18b9c28a0fab1131ece5b0a9bc00a879b8Philipp Reisner	struct drbd_request *req = container_of(w, struct drbd_request, w);
1331b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	struct drbd_device *device = w->device;
1332a6b32bc3cebd3fb6848c526763733b9dbc389c02Andreas Gruenbacher	struct drbd_connection *connection = first_peer_device(device)->connection;
133399920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacher	int err;
133473a01a18b9c28a0fab1131ece5b0a9bc00a879b8Philipp Reisner
133573a01a18b9c28a0fab1131ece5b0a9bc00a879b8Philipp Reisner	if (unlikely(cancel)) {
13368554df1c6d3bb7686b39ed775772f507fa857c19Andreas Gruenbacher		req_mod(req, SEND_CANCELED);
133799920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacher		return 0;
133873a01a18b9c28a0fab1131ece5b0a9bc00a879b8Philipp Reisner	}
133973a01a18b9c28a0fab1131ece5b0a9bc00a879b8Philipp Reisner
1340bde89a9e151b482765ed40e04307a6190236b387Andreas Gruenbacher	/* this time, no connection->send.current_epoch_writes++;
1341b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg	 * If it was sent, it was the closing barrier for the last
1342b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg	 * replicated epoch, before we went into AHEAD mode.
1343b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg	 * No more barriers will be sent, until we leave AHEAD mode again. */
1344bde89a9e151b482765ed40e04307a6190236b387Andreas Gruenbacher	maybe_send_barrier(connection, req->epoch);
1345b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg
1346b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	err = drbd_send_out_of_sync(device, req);
13478554df1c6d3bb7686b39ed775772f507fa857c19Andreas Gruenbacher	req_mod(req, OOS_HANDED_TO_NETWORK);
134873a01a18b9c28a0fab1131ece5b0a9bc00a879b8Philipp Reisner
134999920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacher	return err;
135073a01a18b9c28a0fab1131ece5b0a9bc00a879b8Philipp Reisner}
135173a01a18b9c28a0fab1131ece5b0a9bc00a879b8Philipp Reisner
1352b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner/**
1353b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * w_send_dblock() - Worker callback to send a P_DATA packet in order to mirror a write request
1354b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher * @device:	DRBD device.
1355b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * @w:		work object.
1356b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * @cancel:	The connection will be closed anyways
1357b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner */
135899920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacherint w_send_dblock(struct drbd_work *w, int cancel)
1359b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
1360b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	struct drbd_request *req = container_of(w, struct drbd_request, w);
1361b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	struct drbd_device *device = w->device;
1362a6b32bc3cebd3fb6848c526763733b9dbc389c02Andreas Gruenbacher	struct drbd_connection *connection = first_peer_device(device)->connection;
136399920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacher	int err;
1364b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1365b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (unlikely(cancel)) {
13668554df1c6d3bb7686b39ed775772f507fa857c19Andreas Gruenbacher		req_mod(req, SEND_CANCELED);
136799920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacher		return 0;
1368b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
1369b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1370bde89a9e151b482765ed40e04307a6190236b387Andreas Gruenbacher	re_init_if_first_write(connection, req->epoch);
1371bde89a9e151b482765ed40e04307a6190236b387Andreas Gruenbacher	maybe_send_barrier(connection, req->epoch);
1372bde89a9e151b482765ed40e04307a6190236b387Andreas Gruenbacher	connection->send.current_epoch_writes++;
1373b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg
1374b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	err = drbd_send_dblock(device, req);
137599920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacher	req_mod(req, err ? SEND_FAILED : HANDED_OVER_TO_NETWORK);
1376b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
137799920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacher	return err;
1378b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
1379b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1380b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner/**
1381b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * w_send_read_req() - Worker callback to send a read request (P_DATA_REQUEST) packet
1382b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher * @device:	DRBD device.
1383b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * @w:		work object.
1384b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * @cancel:	The connection will be closed anyways
1385b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner */
138699920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacherint w_send_read_req(struct drbd_work *w, int cancel)
1387b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
1388b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	struct drbd_request *req = container_of(w, struct drbd_request, w);
1389b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	struct drbd_device *device = w->device;
1390a6b32bc3cebd3fb6848c526763733b9dbc389c02Andreas Gruenbacher	struct drbd_connection *connection = first_peer_device(device)->connection;
139199920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacher	int err;
1392b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1393b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (unlikely(cancel)) {
13948554df1c6d3bb7686b39ed775772f507fa857c19Andreas Gruenbacher		req_mod(req, SEND_CANCELED);
139599920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacher		return 0;
1396b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
1397b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1398b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg	/* Even read requests may close a write epoch,
1399b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg	 * if there was any yet. */
1400bde89a9e151b482765ed40e04307a6190236b387Andreas Gruenbacher	maybe_send_barrier(connection, req->epoch);
1401b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg
1402b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	err = drbd_send_drequest(device, P_DATA_REQUEST, req->i.sector, req->i.size,
14036c1005e74d4142511a165edae72cb6648aa308c5Andreas Gruenbacher				 (unsigned long)req);
1404b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
140599920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacher	req_mod(req, err ? SEND_FAILED : HANDED_OVER_TO_NETWORK);
1406b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
140799920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacher	return err;
1408b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
1409b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
141099920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacherint w_restart_disk_io(struct drbd_work *w, int cancel)
1411265be2d09853d425ad14a61cda0ca63345613d0cPhilipp Reisner{
1412265be2d09853d425ad14a61cda0ca63345613d0cPhilipp Reisner	struct drbd_request *req = container_of(w, struct drbd_request, w);
1413b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	struct drbd_device *device = w->device;
1414265be2d09853d425ad14a61cda0ca63345613d0cPhilipp Reisner
14150778286a133d2d3f81861a4e5db308e359583006Philipp Reisner	if (bio_data_dir(req->master_bio) == WRITE && req->rq_state & RQ_IN_ACT_LOG)
1416b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		drbd_al_begin_io(device, &req->i, false);
1417265be2d09853d425ad14a61cda0ca63345613d0cPhilipp Reisner
1418265be2d09853d425ad14a61cda0ca63345613d0cPhilipp Reisner	drbd_req_make_private_bio(req, req->master_bio);
1419b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	req->private_bio->bi_bdev = device->ldev->backing_bdev;
1420265be2d09853d425ad14a61cda0ca63345613d0cPhilipp Reisner	generic_make_request(req->private_bio);
1421265be2d09853d425ad14a61cda0ca63345613d0cPhilipp Reisner
142299920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacher	return 0;
1423265be2d09853d425ad14a61cda0ca63345613d0cPhilipp Reisner}
1424265be2d09853d425ad14a61cda0ca63345613d0cPhilipp Reisner
1425b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacherstatic int _drbd_may_sync_now(struct drbd_device *device)
1426b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
1427b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	struct drbd_device *odev = device;
142895f8efd08bcce65df994049a292b94e56c7ada67Andreas Gruenbacher	int resync_after;
1429b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1430b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	while (1) {
1431a3f8f7dc7ad652cd84c12cb5efa0f7722dff4786Lars Ellenberg		if (!odev->ldev || odev->state.disk == D_DISKLESS)
1432438c8374ae3e87f44d945a2ac2901e3b14aec1a8Philipp Reisner			return 1;
1433daeda1cca91d58bb6c8e45f6734f021bab9c28b7Philipp Reisner		rcu_read_lock();
143495f8efd08bcce65df994049a292b94e56c7ada67Andreas Gruenbacher		resync_after = rcu_dereference(odev->ldev->disk_conf)->resync_after;
1435daeda1cca91d58bb6c8e45f6734f021bab9c28b7Philipp Reisner		rcu_read_unlock();
143695f8efd08bcce65df994049a292b94e56c7ada67Andreas Gruenbacher		if (resync_after == -1)
1437b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			return 1;
1438b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		odev = minor_to_device(resync_after);
1439a3f8f7dc7ad652cd84c12cb5efa0f7722dff4786Lars Ellenberg		if (!odev)
1440841ce241fa355048f66172a47e356bb6e9159c9dAndreas Gruenbacher			return 1;
1441b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if ((odev->state.conn >= C_SYNC_SOURCE &&
1442b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		     odev->state.conn <= C_PAUSED_SYNC_T) ||
1443b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		    odev->state.aftr_isp || odev->state.peer_isp ||
1444b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		    odev->state.user_isp)
1445b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			return 0;
1446b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
1447b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
1448b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1449b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner/**
1450b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * _drbd_pause_after() - Pause resync on all devices that may not resync now
1451b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher * @device:	DRBD device.
1452b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner *
1453b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * Called from process context only (admin command and after_state_ch).
1454b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner */
1455b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacherstatic int _drbd_pause_after(struct drbd_device *device)
1456b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
1457547616979372b65646d691e8dab90e850be582feAndreas Gruenbacher	struct drbd_device *odev;
1458b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	int i, rv = 0;
1459b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1460695d08fa94ce5bb8d9880e260445fbcf50fa41b4Philipp Reisner	rcu_read_lock();
146181a5d60ecfe1d94627abb54810445f0fd5892f42Philipp Reisner	idr_for_each_entry(&minors, odev, i) {
1462b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (odev->state.conn == C_STANDALONE && odev->state.disk == D_DISKLESS)
1463b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			continue;
1464b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (!_drbd_may_sync_now(odev))
1465b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			rv |= (__drbd_set_state(_NS(odev, aftr_isp, 1), CS_HARD, NULL)
1466b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			       != SS_NOTHING_TO_DO);
1467b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
1468695d08fa94ce5bb8d9880e260445fbcf50fa41b4Philipp Reisner	rcu_read_unlock();
1469b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1470b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	return rv;
1471b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
1472b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1473b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner/**
1474b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * _drbd_resume_next() - Resume resync on all devices that may resync now
1475b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher * @device:	DRBD device.
1476b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner *
1477b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * Called from process context only (admin command and worker).
1478b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner */
1479b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacherstatic int _drbd_resume_next(struct drbd_device *device)
1480b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
1481547616979372b65646d691e8dab90e850be582feAndreas Gruenbacher	struct drbd_device *odev;
1482b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	int i, rv = 0;
1483b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1484695d08fa94ce5bb8d9880e260445fbcf50fa41b4Philipp Reisner	rcu_read_lock();
148581a5d60ecfe1d94627abb54810445f0fd5892f42Philipp Reisner	idr_for_each_entry(&minors, odev, i) {
1486b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (odev->state.conn == C_STANDALONE && odev->state.disk == D_DISKLESS)
1487b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			continue;
1488b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (odev->state.aftr_isp) {
1489b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			if (_drbd_may_sync_now(odev))
1490b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner				rv |= (__drbd_set_state(_NS(odev, aftr_isp, 0),
1491b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner							CS_HARD, NULL)
1492b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner				       != SS_NOTHING_TO_DO) ;
1493b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
1494b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
1495695d08fa94ce5bb8d9880e260445fbcf50fa41b4Philipp Reisner	rcu_read_unlock();
1496b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	return rv;
1497b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
1498b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1499b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbachervoid resume_next_sg(struct drbd_device *device)
1500b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
1501b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	write_lock_irq(&global_state_lock);
1502b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	_drbd_resume_next(device);
1503b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	write_unlock_irq(&global_state_lock);
1504b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
1505b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1506b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbachervoid suspend_other_sg(struct drbd_device *device)
1507b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
1508b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	write_lock_irq(&global_state_lock);
1509b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	_drbd_pause_after(device);
1510b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	write_unlock_irq(&global_state_lock);
1511b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
1512b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1513dc97b70801667ea8b1432b37f5c122405c8d6f96Philipp Reisner/* caller must hold global_state_lock */
1514b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacherenum drbd_ret_code drbd_resync_after_valid(struct drbd_device *device, int o_minor)
1515b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
1516547616979372b65646d691e8dab90e850be582feAndreas Gruenbacher	struct drbd_device *odev;
151795f8efd08bcce65df994049a292b94e56c7ada67Andreas Gruenbacher	int resync_after;
1518b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1519b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (o_minor == -1)
1520b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		return NO_ERROR;
1521a3f8f7dc7ad652cd84c12cb5efa0f7722dff4786Lars Ellenberg	if (o_minor < -1 || o_minor > MINORMASK)
152295f8efd08bcce65df994049a292b94e56c7ada67Andreas Gruenbacher		return ERR_RESYNC_AFTER;
1523b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1524b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	/* check for loops */
1525b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	odev = minor_to_device(o_minor);
1526b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	while (1) {
1527b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		if (odev == device)
152895f8efd08bcce65df994049a292b94e56c7ada67Andreas Gruenbacher			return ERR_RESYNC_AFTER_CYCLE;
1529b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1530a3f8f7dc7ad652cd84c12cb5efa0f7722dff4786Lars Ellenberg		/* You are free to depend on diskless, non-existing,
1531a3f8f7dc7ad652cd84c12cb5efa0f7722dff4786Lars Ellenberg		 * or not yet/no longer existing minors.
1532a3f8f7dc7ad652cd84c12cb5efa0f7722dff4786Lars Ellenberg		 * We only reject dependency loops.
1533a3f8f7dc7ad652cd84c12cb5efa0f7722dff4786Lars Ellenberg		 * We cannot follow the dependency chain beyond a detached or
1534a3f8f7dc7ad652cd84c12cb5efa0f7722dff4786Lars Ellenberg		 * missing minor.
1535a3f8f7dc7ad652cd84c12cb5efa0f7722dff4786Lars Ellenberg		 */
1536a3f8f7dc7ad652cd84c12cb5efa0f7722dff4786Lars Ellenberg		if (!odev || !odev->ldev || odev->state.disk == D_DISKLESS)
1537a3f8f7dc7ad652cd84c12cb5efa0f7722dff4786Lars Ellenberg			return NO_ERROR;
1538a3f8f7dc7ad652cd84c12cb5efa0f7722dff4786Lars Ellenberg
1539daeda1cca91d58bb6c8e45f6734f021bab9c28b7Philipp Reisner		rcu_read_lock();
154095f8efd08bcce65df994049a292b94e56c7ada67Andreas Gruenbacher		resync_after = rcu_dereference(odev->ldev->disk_conf)->resync_after;
1541daeda1cca91d58bb6c8e45f6734f021bab9c28b7Philipp Reisner		rcu_read_unlock();
1542b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		/* dependency chain ends here, no cycles. */
154395f8efd08bcce65df994049a292b94e56c7ada67Andreas Gruenbacher		if (resync_after == -1)
1544b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			return NO_ERROR;
1545b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1546b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		/* follow the dependency chain */
1547b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		odev = minor_to_device(resync_after);
1548b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
1549b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
1550b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1551dc97b70801667ea8b1432b37f5c122405c8d6f96Philipp Reisner/* caller must hold global_state_lock */
1552b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbachervoid drbd_resync_after_changed(struct drbd_device *device)
1553b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
1554b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	int changes;
1555b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1556dc97b70801667ea8b1432b37f5c122405c8d6f96Philipp Reisner	do {
1557b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		changes  = _drbd_pause_after(device);
1558b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		changes |= _drbd_resume_next(device);
1559dc97b70801667ea8b1432b37f5c122405c8d6f96Philipp Reisner	} while (changes);
1560b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
1561b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1562b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbachervoid drbd_rs_controller_reset(struct drbd_device *device)
15639bd28d3c90c80c7ec46085de281b38f67331da41Lars Ellenberg{
1564813472ced7fac734157fe5be1137ce2bac942902Philipp Reisner	struct fifo_buffer *plan;
1565813472ced7fac734157fe5be1137ce2bac942902Philipp Reisner
1566b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	atomic_set(&device->rs_sect_in, 0);
1567b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	atomic_set(&device->rs_sect_ev, 0);
1568b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	device->rs_in_flight = 0;
1569813472ced7fac734157fe5be1137ce2bac942902Philipp Reisner
1570813472ced7fac734157fe5be1137ce2bac942902Philipp Reisner	/* Updating the RCU protected object in place is necessary since
1571813472ced7fac734157fe5be1137ce2bac942902Philipp Reisner	   this function gets called from atomic context.
1572813472ced7fac734157fe5be1137ce2bac942902Philipp Reisner	   It is valid since all other updates also lead to an completely
1573813472ced7fac734157fe5be1137ce2bac942902Philipp Reisner	   empty fifo */
1574813472ced7fac734157fe5be1137ce2bac942902Philipp Reisner	rcu_read_lock();
1575b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	plan = rcu_dereference(device->rs_plan_s);
1576813472ced7fac734157fe5be1137ce2bac942902Philipp Reisner	plan->total = 0;
1577813472ced7fac734157fe5be1137ce2bac942902Philipp Reisner	fifo_set(plan, 0);
1578813472ced7fac734157fe5be1137ce2bac942902Philipp Reisner	rcu_read_unlock();
15799bd28d3c90c80c7ec46085de281b38f67331da41Lars Ellenberg}
15809bd28d3c90c80c7ec46085de281b38f67331da41Lars Ellenberg
15811f04af33fe7db542d75a487b8381b5a3402b7896Philipp Reisnervoid start_resync_timer_fn(unsigned long data)
15821f04af33fe7db542d75a487b8381b5a3402b7896Philipp Reisner{
1583b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	struct drbd_device *device = (struct drbd_device *) data;
15841f04af33fe7db542d75a487b8381b5a3402b7896Philipp Reisner
1585a6b32bc3cebd3fb6848c526763733b9dbc389c02Andreas Gruenbacher	drbd_queue_work(&first_peer_device(device)->connection->sender_work, &device->start_resync_work);
15861f04af33fe7db542d75a487b8381b5a3402b7896Philipp Reisner}
15871f04af33fe7db542d75a487b8381b5a3402b7896Philipp Reisner
158899920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacherint w_start_resync(struct drbd_work *w, int cancel)
15891f04af33fe7db542d75a487b8381b5a3402b7896Philipp Reisner{
1590b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	struct drbd_device *device = w->device;
159100d56944ff086f895e9ad184a7785ca1eece4a3bPhilipp Reisner
1592b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	if (atomic_read(&device->unacked_cnt) || atomic_read(&device->rs_pending_cnt)) {
15931f04af33fe7db542d75a487b8381b5a3402b7896Philipp Reisner		dev_warn(DEV, "w_start_resync later...\n");
1594b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		device->start_resync_timer.expires = jiffies + HZ/10;
1595b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		add_timer(&device->start_resync_timer);
159699920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacher		return 0;
15971f04af33fe7db542d75a487b8381b5a3402b7896Philipp Reisner	}
15981f04af33fe7db542d75a487b8381b5a3402b7896Philipp Reisner
1599b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	drbd_start_resync(device, C_SYNC_SOURCE);
1600b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	clear_bit(AHEAD_TO_SYNC_SOURCE, &device->flags);
160199920dc5c5fe52182fe922aa70330861e2b6418bAndreas Gruenbacher	return 0;
16021f04af33fe7db542d75a487b8381b5a3402b7896Philipp Reisner}
16031f04af33fe7db542d75a487b8381b5a3402b7896Philipp Reisner
1604b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner/**
1605b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * drbd_start_resync() - Start the resync process
1606b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher * @device:	DRBD device.
1607b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * @side:	Either C_SYNC_SOURCE or C_SYNC_TARGET
1608b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner *
1609b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * This function might bring you directly into one of the
1610b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * C_PAUSED_SYNC_* states.
1611b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner */
1612b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbachervoid drbd_start_resync(struct drbd_device *device, enum drbd_conns side)
1613b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
1614b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	union drbd_state ns;
1615b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	int r;
1616b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1617b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	if (device->state.conn >= C_SYNC_SOURCE && device->state.conn < C_AHEAD) {
1618b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		dev_err(DEV, "Resync already running!\n");
1619b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		return;
1620b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
1621b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1622b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	if (!test_bit(B_RS_H_DONE, &device->flags)) {
1623e64a32945902a178c9de9b38e0ea3290981605bcPhilipp Reisner		if (side == C_SYNC_TARGET) {
1624e64a32945902a178c9de9b38e0ea3290981605bcPhilipp Reisner			/* Since application IO was locked out during C_WF_BITMAP_T and
1625e64a32945902a178c9de9b38e0ea3290981605bcPhilipp Reisner			   C_WF_SYNC_UUID we are still unmodified. Before going to C_SYNC_TARGET
1626e64a32945902a178c9de9b38e0ea3290981605bcPhilipp Reisner			   we check that we might make the data inconsistent. */
1627b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher			r = drbd_khelper(device, "before-resync-target");
1628e64a32945902a178c9de9b38e0ea3290981605bcPhilipp Reisner			r = (r >> 8) & 0xff;
1629e64a32945902a178c9de9b38e0ea3290981605bcPhilipp Reisner			if (r > 0) {
1630e64a32945902a178c9de9b38e0ea3290981605bcPhilipp Reisner				dev_info(DEV, "before-resync-target handler returned %d, "
163109b9e7979378fe070784de20e50bb1d42aa643abPhilipp Reisner					 "dropping connection.\n", r);
1632a6b32bc3cebd3fb6848c526763733b9dbc389c02Andreas Gruenbacher				conn_request_state(first_peer_device(device)->connection, NS(conn, C_DISCONNECTING), CS_HARD);
163309b9e7979378fe070784de20e50bb1d42aa643abPhilipp Reisner				return;
163409b9e7979378fe070784de20e50bb1d42aa643abPhilipp Reisner			}
1635e64a32945902a178c9de9b38e0ea3290981605bcPhilipp Reisner		} else /* C_SYNC_SOURCE */ {
1636b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher			r = drbd_khelper(device, "before-resync-source");
1637e64a32945902a178c9de9b38e0ea3290981605bcPhilipp Reisner			r = (r >> 8) & 0xff;
1638e64a32945902a178c9de9b38e0ea3290981605bcPhilipp Reisner			if (r > 0) {
1639e64a32945902a178c9de9b38e0ea3290981605bcPhilipp Reisner				if (r == 3) {
1640e64a32945902a178c9de9b38e0ea3290981605bcPhilipp Reisner					dev_info(DEV, "before-resync-source handler returned %d, "
1641e64a32945902a178c9de9b38e0ea3290981605bcPhilipp Reisner						 "ignoring. Old userland tools?", r);
1642e64a32945902a178c9de9b38e0ea3290981605bcPhilipp Reisner				} else {
1643e64a32945902a178c9de9b38e0ea3290981605bcPhilipp Reisner					dev_info(DEV, "before-resync-source handler returned %d, "
1644e64a32945902a178c9de9b38e0ea3290981605bcPhilipp Reisner						 "dropping connection.\n", r);
1645a6b32bc3cebd3fb6848c526763733b9dbc389c02Andreas Gruenbacher					conn_request_state(first_peer_device(device)->connection,
1646a6b32bc3cebd3fb6848c526763733b9dbc389c02Andreas Gruenbacher							   NS(conn, C_DISCONNECTING), CS_HARD);
1647e64a32945902a178c9de9b38e0ea3290981605bcPhilipp Reisner					return;
1648e64a32945902a178c9de9b38e0ea3290981605bcPhilipp Reisner				}
1649e64a32945902a178c9de9b38e0ea3290981605bcPhilipp Reisner			}
165009b9e7979378fe070784de20e50bb1d42aa643abPhilipp Reisner		}
1651b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
1652b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1653a6b32bc3cebd3fb6848c526763733b9dbc389c02Andreas Gruenbacher	if (current == first_peer_device(device)->connection->worker.task) {
1654dad20554812e73a2bfbe45d1b161d5d3c249e597Philipp Reisner		/* The worker should not sleep waiting for state_mutex,
1655e64a32945902a178c9de9b38e0ea3290981605bcPhilipp Reisner		   that can take long */
1656b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		if (!mutex_trylock(device->state_mutex)) {
1657b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher			set_bit(B_RS_H_DONE, &device->flags);
1658b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher			device->start_resync_timer.expires = jiffies + HZ/5;
1659b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher			add_timer(&device->start_resync_timer);
1660e64a32945902a178c9de9b38e0ea3290981605bcPhilipp Reisner			return;
1661e64a32945902a178c9de9b38e0ea3290981605bcPhilipp Reisner		}
1662e64a32945902a178c9de9b38e0ea3290981605bcPhilipp Reisner	} else {
1663b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		mutex_lock(device->state_mutex);
1664e64a32945902a178c9de9b38e0ea3290981605bcPhilipp Reisner	}
1665b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	clear_bit(B_RS_H_DONE, &device->flags);
1666b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
16670cfac5dd904ec8b376beb27f6ad265b12d71bf9ePhilipp Reisner	write_lock_irq(&global_state_lock);
1668a700471bf335965e7603273fd51034415553246aPhilipp Reisner	/* Did some connection breakage or IO error race with us? */
1669b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	if (device->state.conn < C_CONNECTED
1670b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	|| !get_ldev_if_state(device, D_NEGOTIATING)) {
16710cfac5dd904ec8b376beb27f6ad265b12d71bf9ePhilipp Reisner		write_unlock_irq(&global_state_lock);
1672b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		mutex_unlock(device->state_mutex);
1673b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		return;
1674b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
1675b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1676b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	ns = drbd_read_state(device);
1677b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1678b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	ns.aftr_isp = !_drbd_may_sync_now(device);
1679b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1680b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	ns.conn = side;
1681b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1682b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (side == C_SYNC_TARGET)
1683b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		ns.disk = D_INCONSISTENT;
1684b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	else /* side == C_SYNC_SOURCE */
1685b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		ns.pdsk = D_INCONSISTENT;
1686b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1687b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	r = __drbd_set_state(device, ns, CS_VERBOSE, NULL);
1688b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	ns = drbd_read_state(device);
1689b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1690b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (ns.conn < C_CONNECTED)
1691b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		r = SS_UNKNOWN_ERROR;
1692b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1693b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (r == SS_SUCCESS) {
1694b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		unsigned long tw = drbd_bm_total_weight(device);
16951d7734a0df02ff5068ff8baa1447c7baee601db1Lars Ellenberg		unsigned long now = jiffies;
16961d7734a0df02ff5068ff8baa1447c7baee601db1Lars Ellenberg		int i;
16971d7734a0df02ff5068ff8baa1447c7baee601db1Lars Ellenberg
1698b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		device->rs_failed    = 0;
1699b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		device->rs_paused    = 0;
1700b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		device->rs_same_csum = 0;
1701b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		device->rs_last_events = 0;
1702b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		device->rs_last_sect_ev = 0;
1703b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		device->rs_total     = tw;
1704b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		device->rs_start     = now;
17051d7734a0df02ff5068ff8baa1447c7baee601db1Lars Ellenberg		for (i = 0; i < DRBD_SYNC_MARKS; i++) {
1706b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher			device->rs_mark_left[i] = tw;
1707b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher			device->rs_mark_time[i] = now;
17081d7734a0df02ff5068ff8baa1447c7baee601db1Lars Ellenberg		}
1709b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		_drbd_pause_after(device);
1710b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
1711b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	write_unlock_irq(&global_state_lock);
17125a22db8968a69bec835d1ed9a96ab3381719e0c0Lars Ellenberg
1713b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (r == SS_SUCCESS) {
1714328e0f125bf41f4f33f684db22015f92cb44fe56Philipp Reisner		/* reset rs_last_bcast when a resync or verify is started,
1715328e0f125bf41f4f33f684db22015f92cb44fe56Philipp Reisner		 * to deal with potential jiffies wrap. */
1716b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		device->rs_last_bcast = jiffies - HZ;
1717328e0f125bf41f4f33f684db22015f92cb44fe56Philipp Reisner
1718b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		dev_info(DEV, "Began resync as %s (will sync %lu KB [%lu bits set]).\n",
1719b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		     drbd_conn_str(ns.conn),
1720b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		     (unsigned long) device->rs_total << (BM_BLOCK_SHIFT-10),
1721b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		     (unsigned long) device->rs_total);
17226c922ed543bee1bc6685ade07be59f3fa49a7288Lars Ellenberg		if (side == C_SYNC_TARGET)
1723b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher			device->bm_resync_fo = 0;
17246c922ed543bee1bc6685ade07be59f3fa49a7288Lars Ellenberg
17256c922ed543bee1bc6685ade07be59f3fa49a7288Lars Ellenberg		/* Since protocol 96, we must serialize drbd_gen_and_send_sync_uuid
17266c922ed543bee1bc6685ade07be59f3fa49a7288Lars Ellenberg		 * with w_send_oos, or the sync target will get confused as to
17276c922ed543bee1bc6685ade07be59f3fa49a7288Lars Ellenberg		 * how much bits to resync.  We cannot do that always, because for an
17286c922ed543bee1bc6685ade07be59f3fa49a7288Lars Ellenberg		 * empty resync and protocol < 95, we need to do it here, as we call
17296c922ed543bee1bc6685ade07be59f3fa49a7288Lars Ellenberg		 * drbd_resync_finished from here in that case.
17306c922ed543bee1bc6685ade07be59f3fa49a7288Lars Ellenberg		 * We drbd_gen_and_send_sync_uuid here for protocol < 96,
17316c922ed543bee1bc6685ade07be59f3fa49a7288Lars Ellenberg		 * and from after_state_ch otherwise. */
1732a6b32bc3cebd3fb6848c526763733b9dbc389c02Andreas Gruenbacher		if (side == C_SYNC_SOURCE &&
1733a6b32bc3cebd3fb6848c526763733b9dbc389c02Andreas Gruenbacher		    first_peer_device(device)->connection->agreed_pro_version < 96)
1734b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher			drbd_gen_and_send_sync_uuid(device);
1735b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1736a6b32bc3cebd3fb6848c526763733b9dbc389c02Andreas Gruenbacher		if (first_peer_device(device)->connection->agreed_pro_version < 95 &&
1737a6b32bc3cebd3fb6848c526763733b9dbc389c02Andreas Gruenbacher		    device->rs_total == 0) {
1738af85e8e83d160f72a10e4467852646ac08614260Lars Ellenberg			/* This still has a race (about when exactly the peers
1739af85e8e83d160f72a10e4467852646ac08614260Lars Ellenberg			 * detect connection loss) that can lead to a full sync
1740af85e8e83d160f72a10e4467852646ac08614260Lars Ellenberg			 * on next handshake. In 8.3.9 we fixed this with explicit
1741af85e8e83d160f72a10e4467852646ac08614260Lars Ellenberg			 * resync-finished notifications, but the fix
1742af85e8e83d160f72a10e4467852646ac08614260Lars Ellenberg			 * introduces a protocol change.  Sleeping for some
1743af85e8e83d160f72a10e4467852646ac08614260Lars Ellenberg			 * time longer than the ping interval + timeout on the
1744af85e8e83d160f72a10e4467852646ac08614260Lars Ellenberg			 * SyncSource, to give the SyncTarget the chance to
1745af85e8e83d160f72a10e4467852646ac08614260Lars Ellenberg			 * detect connection loss, then waiting for a ping
1746af85e8e83d160f72a10e4467852646ac08614260Lars Ellenberg			 * response (implicit in drbd_resync_finished) reduces
1747af85e8e83d160f72a10e4467852646ac08614260Lars Ellenberg			 * the race considerably, but does not solve it. */
174844ed167da74825bfb7950d45a4f83bce3e84921cPhilipp Reisner			if (side == C_SYNC_SOURCE) {
174944ed167da74825bfb7950d45a4f83bce3e84921cPhilipp Reisner				struct net_conf *nc;
175044ed167da74825bfb7950d45a4f83bce3e84921cPhilipp Reisner				int timeo;
175144ed167da74825bfb7950d45a4f83bce3e84921cPhilipp Reisner
175244ed167da74825bfb7950d45a4f83bce3e84921cPhilipp Reisner				rcu_read_lock();
1753a6b32bc3cebd3fb6848c526763733b9dbc389c02Andreas Gruenbacher				nc = rcu_dereference(first_peer_device(device)->connection->net_conf);
175444ed167da74825bfb7950d45a4f83bce3e84921cPhilipp Reisner				timeo = nc->ping_int * HZ + nc->ping_timeo * HZ / 9;
175544ed167da74825bfb7950d45a4f83bce3e84921cPhilipp Reisner				rcu_read_unlock();
175644ed167da74825bfb7950d45a4f83bce3e84921cPhilipp Reisner				schedule_timeout_interruptible(timeo);
175744ed167da74825bfb7950d45a4f83bce3e84921cPhilipp Reisner			}
1758b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher			drbd_resync_finished(device);
1759b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
1760b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1761b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		drbd_rs_controller_reset(device);
1762b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		/* ns.conn may already be != device->state.conn,
1763b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		 * we may have been paused in between, or become paused until
1764b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		 * the timer triggers.
1765b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		 * No matter, that is handled in resync_timer_fn() */
1766b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (ns.conn == C_SYNC_TARGET)
1767b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher			mod_timer(&device->resync_timer, jiffies);
1768b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1769b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		drbd_md_sync(device);
1770b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
1771b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	put_ldev(device);
1772b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	mutex_unlock(device->state_mutex);
1773b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
1774b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1775b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg/* If the resource already closed the current epoch, but we did not
1776b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg * (because we have not yet seen new requests), we should send the
1777b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg * corresponding barrier now.  Must be checked within the same spinlock
1778b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg * that is used to check for new requests. */
1779bde89a9e151b482765ed40e04307a6190236b387Andreas Gruenbacherstatic bool need_to_send_barrier(struct drbd_connection *connection)
1780b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg{
1781b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg	if (!connection->send.seen_any_write_yet)
1782b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg		return false;
1783b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg
1784b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg	/* Skip barriers that do not contain any writes.
1785b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg	 * This may happen during AHEAD mode. */
1786b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg	if (!connection->send.current_epoch_writes)
1787b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg		return false;
1788b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg
1789b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg	/* ->req_lock is held when requests are queued on
1790b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg	 * connection->sender_work, and put into ->transfer_log.
1791b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg	 * It is also held when ->current_tle_nr is increased.
1792b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg	 * So either there are already new requests queued,
1793b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg	 * and corresponding barriers will be send there.
1794b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg	 * Or nothing new is queued yet, so the difference will be 1.
1795b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg	 */
1796b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg	if (atomic_read(&connection->current_tle_nr) !=
1797b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg	    connection->send.current_epoch_nr + 1)
1798b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg		return false;
1799b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg
1800b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg	return true;
1801b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg}
1802b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg
1803a186e47856c7877fa684d75901436c1d35ec35e0Rashika Kheriastatic bool dequeue_work_batch(struct drbd_work_queue *queue, struct list_head *work_list)
18048c0785a5c9a0f2472aff68dc32247be01728c416Lars Ellenberg{
18058c0785a5c9a0f2472aff68dc32247be01728c416Lars Ellenberg	spin_lock_irq(&queue->q_lock);
18068c0785a5c9a0f2472aff68dc32247be01728c416Lars Ellenberg	list_splice_init(&queue->q, work_list);
18078c0785a5c9a0f2472aff68dc32247be01728c416Lars Ellenberg	spin_unlock_irq(&queue->q_lock);
18088c0785a5c9a0f2472aff68dc32247be01728c416Lars Ellenberg	return !list_empty(work_list);
18098c0785a5c9a0f2472aff68dc32247be01728c416Lars Ellenberg}
18108c0785a5c9a0f2472aff68dc32247be01728c416Lars Ellenberg
1811a186e47856c7877fa684d75901436c1d35ec35e0Rashika Kheriastatic bool dequeue_work_item(struct drbd_work_queue *queue, struct list_head *work_list)
18128c0785a5c9a0f2472aff68dc32247be01728c416Lars Ellenberg{
18138c0785a5c9a0f2472aff68dc32247be01728c416Lars Ellenberg	spin_lock_irq(&queue->q_lock);
18148c0785a5c9a0f2472aff68dc32247be01728c416Lars Ellenberg	if (!list_empty(&queue->q))
18158c0785a5c9a0f2472aff68dc32247be01728c416Lars Ellenberg		list_move(queue->q.next, work_list);
18168c0785a5c9a0f2472aff68dc32247be01728c416Lars Ellenberg	spin_unlock_irq(&queue->q_lock);
18178c0785a5c9a0f2472aff68dc32247be01728c416Lars Ellenberg	return !list_empty(work_list);
18188c0785a5c9a0f2472aff68dc32247be01728c416Lars Ellenberg}
18198c0785a5c9a0f2472aff68dc32247be01728c416Lars Ellenberg
1820bde89a9e151b482765ed40e04307a6190236b387Andreas Gruenbacherstatic void wait_for_work(struct drbd_connection *connection, struct list_head *work_list)
1821b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg{
1822b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg	DEFINE_WAIT(wait);
1823b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg	struct net_conf *nc;
1824b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg	int uncork, cork;
1825b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg
1826b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg	dequeue_work_item(&connection->sender_work, work_list);
1827b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg	if (!list_empty(work_list))
1828b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg		return;
1829b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg
1830b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg	/* Still nothing to do?
1831b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg	 * Maybe we still need to close the current epoch,
1832b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg	 * even if no new requests are queued yet.
1833b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg	 *
1834b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg	 * Also, poke TCP, just in case.
1835b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg	 * Then wait for new work (or signal). */
1836b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg	rcu_read_lock();
1837b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg	nc = rcu_dereference(connection->net_conf);
1838b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg	uncork = nc ? nc->tcp_cork : 0;
1839b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg	rcu_read_unlock();
1840b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg	if (uncork) {
1841b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg		mutex_lock(&connection->data.mutex);
1842b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg		if (connection->data.socket)
1843b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg			drbd_tcp_uncork(connection->data.socket);
1844b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg		mutex_unlock(&connection->data.mutex);
1845b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg	}
1846b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg
1847b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg	for (;;) {
1848b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg		int send_barrier;
1849b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg		prepare_to_wait(&connection->sender_work.q_wait, &wait, TASK_INTERRUPTIBLE);
1850b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg		spin_lock_irq(&connection->req_lock);
1851b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg		spin_lock(&connection->sender_work.q_lock);	/* FIXME get rid of this one? */
1852bc317a9ecd641b78a4b237cb22b30ecf11443c77Lars Ellenberg		/* dequeue single item only,
1853bc317a9ecd641b78a4b237cb22b30ecf11443c77Lars Ellenberg		 * we still use drbd_queue_work_front() in some places */
1854bc317a9ecd641b78a4b237cb22b30ecf11443c77Lars Ellenberg		if (!list_empty(&connection->sender_work.q))
1855bc317a9ecd641b78a4b237cb22b30ecf11443c77Lars Ellenberg			list_move(connection->sender_work.q.next, work_list);
1856b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg		spin_unlock(&connection->sender_work.q_lock);	/* FIXME get rid of this one? */
1857b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg		if (!list_empty(work_list) || signal_pending(current)) {
1858b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg			spin_unlock_irq(&connection->req_lock);
1859b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg			break;
1860b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg		}
1861b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg		send_barrier = need_to_send_barrier(connection);
1862b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg		spin_unlock_irq(&connection->req_lock);
1863b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg		if (send_barrier) {
1864b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg			drbd_send_barrier(connection);
1865b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg			connection->send.current_epoch_nr++;
1866b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg		}
1867b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg		schedule();
1868b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg		/* may be woken up for other things but new work, too,
1869b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg		 * e.g. if the current epoch got closed.
1870b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg		 * In which case we send the barrier above. */
1871b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg	}
1872b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg	finish_wait(&connection->sender_work.q_wait, &wait);
1873b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg
1874b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg	/* someone may have changed the config while we have been waiting above. */
1875b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg	rcu_read_lock();
1876b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg	nc = rcu_dereference(connection->net_conf);
1877b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg	cork = nc ? nc->tcp_cork : 0;
1878b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg	rcu_read_unlock();
1879b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg	mutex_lock(&connection->data.mutex);
1880b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg	if (connection->data.socket) {
1881b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg		if (cork)
1882b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg			drbd_tcp_cork(connection->data.socket);
1883b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg		else if (!uncork)
1884b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg			drbd_tcp_uncork(connection->data.socket);
1885b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg	}
1886b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg	mutex_unlock(&connection->data.mutex);
1887b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg}
1888b6dd1a89767bc33e9c98b3195f8925b46c5c95f3Lars Ellenberg
1889b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisnerint drbd_worker(struct drbd_thread *thi)
1890b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
1891bde89a9e151b482765ed40e04307a6190236b387Andreas Gruenbacher	struct drbd_connection *connection = thi->connection;
1892b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	struct drbd_work *w = NULL;
1893b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher	struct drbd_device *device;
1894b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	LIST_HEAD(work_list);
18958c0785a5c9a0f2472aff68dc32247be01728c416Lars Ellenberg	int vnr;
1896b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1897e77a0a5cc1e6961f485b5623ef42f3b910969675Andreas Gruenbacher	while (get_t_state(thi) == RUNNING) {
1898808222845d62e551630699a1381bbf8a1fd4a286Philipp Reisner		drbd_thread_current_set_cpu(thi);
1899b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
19008c0785a5c9a0f2472aff68dc32247be01728c416Lars Ellenberg		/* as long as we use drbd_queue_work_front(),
19018c0785a5c9a0f2472aff68dc32247be01728c416Lars Ellenberg		 * we may only dequeue single work items here, not batches. */
19028c0785a5c9a0f2472aff68dc32247be01728c416Lars Ellenberg		if (list_empty(&work_list))
1903bde89a9e151b482765ed40e04307a6190236b387Andreas Gruenbacher			wait_for_work(connection, &work_list);
1904b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
19058c0785a5c9a0f2472aff68dc32247be01728c416Lars Ellenberg		if (signal_pending(current)) {
1906b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			flush_signals(current);
190719393e105f9702a014d3ce08bce92b3ad9cf96b5Philipp Reisner			if (get_t_state(thi) == RUNNING) {
1908bde89a9e151b482765ed40e04307a6190236b387Andreas Gruenbacher				conn_warn(connection, "Worker got an unexpected signal\n");
1909b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner				continue;
191019393e105f9702a014d3ce08bce92b3ad9cf96b5Philipp Reisner			}
1911b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			break;
1912b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
1913b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1914e77a0a5cc1e6961f485b5623ef42f3b910969675Andreas Gruenbacher		if (get_t_state(thi) != RUNNING)
1915b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			break;
1916b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
19178c0785a5c9a0f2472aff68dc32247be01728c416Lars Ellenberg		while (!list_empty(&work_list)) {
19188c0785a5c9a0f2472aff68dc32247be01728c416Lars Ellenberg			w = list_first_entry(&work_list, struct drbd_work, list);
19198c0785a5c9a0f2472aff68dc32247be01728c416Lars Ellenberg			list_del_init(&w->list);
1920bde89a9e151b482765ed40e04307a6190236b387Andreas Gruenbacher			if (w->cb(w, connection->cstate < C_WF_REPORT_PARAMS) == 0)
19218c0785a5c9a0f2472aff68dc32247be01728c416Lars Ellenberg				continue;
1922bde89a9e151b482765ed40e04307a6190236b387Andreas Gruenbacher			if (connection->cstate >= C_WF_REPORT_PARAMS)
1923bde89a9e151b482765ed40e04307a6190236b387Andreas Gruenbacher				conn_request_state(connection, NS(conn, C_NETWORK_FAILURE), CS_HARD);
1924b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
1925b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
1926b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
19278c0785a5c9a0f2472aff68dc32247be01728c416Lars Ellenberg	do {
1928b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		while (!list_empty(&work_list)) {
19298c0785a5c9a0f2472aff68dc32247be01728c416Lars Ellenberg			w = list_first_entry(&work_list, struct drbd_work, list);
1930b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			list_del_init(&w->list);
193100d56944ff086f895e9ad184a7785ca1eece4a3bPhilipp Reisner			w->cb(w, 1);
1932b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
1933bde89a9e151b482765ed40e04307a6190236b387Andreas Gruenbacher		dequeue_work_batch(&connection->sender_work, &work_list);
19348c0785a5c9a0f2472aff68dc32247be01728c416Lars Ellenberg	} while (!list_empty(&work_list));
1935b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1936c141ebda031a0550d75634f7c94f7c85c2d5c9f5Philipp Reisner	rcu_read_lock();
1937bde89a9e151b482765ed40e04307a6190236b387Andreas Gruenbacher	idr_for_each_entry(&connection->volumes, device, vnr) {
1938b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		D_ASSERT(device->state.disk == D_DISKLESS && device->state.conn == C_STANDALONE);
1939b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		kref_get(&device->kref);
1940c141ebda031a0550d75634f7c94f7c85c2d5c9f5Philipp Reisner		rcu_read_unlock();
1941b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		drbd_device_cleanup(device);
1942b30ab7913b0a7b1d3b1091c8cb3abb1a9f1e0824Andreas Gruenbacher		kref_put(&device->kref, &drbd_minor_destroy);
1943c141ebda031a0550d75634f7c94f7c85c2d5c9f5Philipp Reisner		rcu_read_lock();
19440e29d163f7ec8369b3f1fb70900d29b1c4a1dc8bPhilipp Reisner	}
1945c141ebda031a0550d75634f7c94f7c85c2d5c9f5Philipp Reisner	rcu_read_unlock();
1946b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1947b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	return 0;
1948b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
1949