drbd_worker.c revision bbeb641c3e4982d6bba21188545a7fd44ab0a715
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"
39b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner#include "drbd_req.h"
40b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
4100d56944ff086f895e9ad184a7785ca1eece4a3bPhilipp Reisnerstatic int w_make_ov_request(struct drbd_work *w, int cancel);
4200d56944ff086f895e9ad184a7785ca1eece4a3bPhilipp Reisnerstatic int w_make_resync_request(struct drbd_work *w, int cancel);
43b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
44b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
45b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
46c5a91619793d444e5103ec5841045bf878718398Andreas Gruenbacher/* endio handlers:
47c5a91619793d444e5103ec5841045bf878718398Andreas Gruenbacher *   drbd_md_io_complete (defined here)
48c5a91619793d444e5103ec5841045bf878718398Andreas Gruenbacher *   drbd_endio_pri (defined here)
49c5a91619793d444e5103ec5841045bf878718398Andreas Gruenbacher *   drbd_endio_sec (defined here)
50c5a91619793d444e5103ec5841045bf878718398Andreas Gruenbacher *   bm_async_io_complete (defined in drbd_bitmap.c)
51c5a91619793d444e5103ec5841045bf878718398Andreas Gruenbacher *
52b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * For all these callbacks, note the following:
53b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * The callbacks will be called in irq context by the IDE drivers,
54b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * and in Softirqs/Tasklets/BH context by the SCSI drivers.
55b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * Try to get the locking right :)
56b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner *
57b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner */
58b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
59b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
60b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner/* About the global_state_lock
61b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner   Each state transition on an device holds a read lock. In case we have
62b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner   to evaluate the sync after dependencies, we grab a write lock, because
63b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner   we need stable states on all devices for that.  */
64b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisnerrwlock_t global_state_lock;
65b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
66b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner/* used for synchronous meta data and bitmap IO
67b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * submitted by drbd_md_sync_page_io()
68b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner */
69b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisnervoid drbd_md_io_complete(struct bio *bio, int error)
70b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
71b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	struct drbd_md_io *md_io;
72b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
73b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	md_io = (struct drbd_md_io *)bio->bi_private;
74b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	md_io->error = error;
75b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
76b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	complete(&md_io->event);
77b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
78b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
79b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner/* reads on behalf of the partner,
80b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * "submitted" by the receiver
81b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner */
82db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbachervoid drbd_endio_read_sec_final(struct drbd_peer_request *peer_req) __releases(local)
83b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
84b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	unsigned long flags = 0;
85a21e9298275a0145e43c2413725549112d99ba01Philipp Reisner	struct drbd_conf *mdev = peer_req->w.mdev;
86b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
8787eeee41f8740451b61a1e7d37a494333a906861Philipp Reisner	spin_lock_irqsave(&mdev->tconn->req_lock, flags);
88db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher	mdev->read_cnt += peer_req->i.size >> 9;
89db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher	list_del(&peer_req->w.list);
90b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (list_empty(&mdev->read_ee))
91b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		wake_up(&mdev->ee_wait);
92db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher	if (test_bit(__EE_WAS_ERROR, &peer_req->flags))
9381e84650c200de0695372461964dd960365696dbAndreas Gruenbacher		__drbd_chk_io_error(mdev, false);
9487eeee41f8740451b61a1e7d37a494333a906861Philipp Reisner	spin_unlock_irqrestore(&mdev->tconn->req_lock, flags);
95b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
96db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher	drbd_queue_work(&mdev->tconn->data.work, &peer_req->w);
97b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	put_ldev(mdev);
98b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
99b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
100b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner/* writes on behalf of the partner, or resync writes,
10145bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg * "submitted" by the receiver, final stage.  */
102db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacherstatic void drbd_endio_write_sec_final(struct drbd_peer_request *peer_req) __releases(local)
103b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
104b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	unsigned long flags = 0;
105a21e9298275a0145e43c2413725549112d99ba01Philipp Reisner	struct drbd_conf *mdev = peer_req->w.mdev;
106b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	sector_t e_sector;
107b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	int do_wake;
108579b57ed730819970a3542b4bbcc2d4176f25c72Andreas Gruenbacher	u64 block_id;
109b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	int do_al_complete_io;
110b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
111db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher	/* after we moved peer_req to done_ee,
112b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	 * we may no longer access it,
113b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	 * it may be freed/reused already!
114b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	 * (as soon as we release the req_lock) */
115db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher	e_sector = peer_req->i.sector;
116db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher	do_al_complete_io = peer_req->flags & EE_CALL_AL_COMPLETE_IO;
117db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher	block_id = peer_req->block_id;
118b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
11987eeee41f8740451b61a1e7d37a494333a906861Philipp Reisner	spin_lock_irqsave(&mdev->tconn->req_lock, flags);
120db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher	mdev->writ_cnt += peer_req->i.size >> 9;
121db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher	list_del(&peer_req->w.list); /* has been on active_ee or sync_ee */
122db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher	list_add_tail(&peer_req->w.list, &mdev->done_ee);
123b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
124bb3bfe96144a4535d47ccfea444bc1ef8e02f4e3Andreas Gruenbacher	/*
1255e4722645afb27ee749ea65988544450f08f78baAndreas Gruenbacher	 * Do not remove from the write_requests tree here: we did not send the
126bb3bfe96144a4535d47ccfea444bc1ef8e02f4e3Andreas Gruenbacher	 * Ack yet and did not wake possibly waiting conflicting requests.
127bb3bfe96144a4535d47ccfea444bc1ef8e02f4e3Andreas Gruenbacher	 * Removed from the tree from "drbd_process_done_ee" within the
128bb3bfe96144a4535d47ccfea444bc1ef8e02f4e3Andreas Gruenbacher	 * appropriate w.cb (e_end_block/e_end_resync_block) or from
129bb3bfe96144a4535d47ccfea444bc1ef8e02f4e3Andreas Gruenbacher	 * _drbd_clear_done_ee.
130bb3bfe96144a4535d47ccfea444bc1ef8e02f4e3Andreas Gruenbacher	 */
131b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
132579b57ed730819970a3542b4bbcc2d4176f25c72Andreas Gruenbacher	do_wake = list_empty(block_id == ID_SYNCER ? &mdev->sync_ee : &mdev->active_ee);
133b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
134db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher	if (test_bit(__EE_WAS_ERROR, &peer_req->flags))
13581e84650c200de0695372461964dd960365696dbAndreas Gruenbacher		__drbd_chk_io_error(mdev, false);
13687eeee41f8740451b61a1e7d37a494333a906861Philipp Reisner	spin_unlock_irqrestore(&mdev->tconn->req_lock, flags);
137b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
138579b57ed730819970a3542b4bbcc2d4176f25c72Andreas Gruenbacher	if (block_id == ID_SYNCER)
139b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		drbd_rs_complete_io(mdev, e_sector);
140b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
141b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (do_wake)
142b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		wake_up(&mdev->ee_wait);
143b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
144b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (do_al_complete_io)
145b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		drbd_al_complete_io(mdev, e_sector);
146b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1470625ac190d222fd0855bad79e93f1556fc45dd20Philipp Reisner	wake_asender(mdev->tconn);
148b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	put_ldev(mdev);
14945bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg}
150b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
15145bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg/* writes on behalf of the partner, or resync writes,
15245bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg * "submitted" by the receiver.
15345bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg */
15445bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenbergvoid drbd_endio_sec(struct bio *bio, int error)
15545bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg{
156db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher	struct drbd_peer_request *peer_req = bio->bi_private;
157a21e9298275a0145e43c2413725549112d99ba01Philipp Reisner	struct drbd_conf *mdev = peer_req->w.mdev;
15845bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	int uptodate = bio_flagged(bio, BIO_UPTODATE);
15945bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	int is_write = bio_data_dir(bio) == WRITE;
16045bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg
161071942727824bab03b1a3f6b6eeb5b269697b333Lars Ellenberg	if (error && __ratelimit(&drbd_ratelimit_state))
16245bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg		dev_warn(DEV, "%s: error=%d s=%llus\n",
16345bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg				is_write ? "write" : "read", error,
164db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher				(unsigned long long)peer_req->i.sector);
16545bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	if (!error && !uptodate) {
166071942727824bab03b1a3f6b6eeb5b269697b333Lars Ellenberg		if (__ratelimit(&drbd_ratelimit_state))
167071942727824bab03b1a3f6b6eeb5b269697b333Lars Ellenberg			dev_warn(DEV, "%s: setting error to -EIO s=%llus\n",
168071942727824bab03b1a3f6b6eeb5b269697b333Lars Ellenberg					is_write ? "write" : "read",
169db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher					(unsigned long long)peer_req->i.sector);
17045bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg		/* strange behavior of some lower level drivers...
17145bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg		 * fail the request by clearing the uptodate flag,
17245bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg		 * but do not return any error?! */
17345bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg		error = -EIO;
17445bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	}
17545bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg
17645bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	if (error)
177db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher		set_bit(__EE_WAS_ERROR, &peer_req->flags);
17845bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg
17945bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	bio_put(bio); /* no need for the bio anymore */
180db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher	if (atomic_dec_and_test(&peer_req->pending_bios)) {
18145bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg		if (is_write)
182db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher			drbd_endio_write_sec_final(peer_req);
18345bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg		else
184db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher			drbd_endio_read_sec_final(peer_req);
18545bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	}
186b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
187b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
188b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner/* read, readA or write requests on R_PRIMARY coming from drbd_make_request
189b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner */
190b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisnervoid drbd_endio_pri(struct bio *bio, int error)
191b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
192a115413de13ae6beb0cbfc198afe385a261ab284Lars Ellenberg	unsigned long flags;
193b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	struct drbd_request *req = bio->bi_private;
194a21e9298275a0145e43c2413725549112d99ba01Philipp Reisner	struct drbd_conf *mdev = req->w.mdev;
195a115413de13ae6beb0cbfc198afe385a261ab284Lars Ellenberg	struct bio_and_error m;
196b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	enum drbd_req_event what;
197b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	int uptodate = bio_flagged(bio, BIO_UPTODATE);
198b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
199b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (!error && !uptodate) {
200b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		dev_warn(DEV, "p %s: setting error to -EIO\n",
201b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			 bio_data_dir(bio) == WRITE ? "write" : "read");
202b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		/* strange behavior of some lower level drivers...
203b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		 * fail the request by clearing the uptodate flag,
204b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		 * but do not return any error?! */
205b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		error = -EIO;
206b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
207b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
208b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	/* to avoid recursion in __req_mod */
209b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (unlikely(error)) {
210b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		what = (bio_data_dir(bio) == WRITE)
2118554df1c6d3bb7686b39ed775772f507fa857c19Andreas Gruenbacher			? WRITE_COMPLETED_WITH_ERROR
2125c3c7e64bbcb60db39d0d55c8fab39ef41d41c30Lars Ellenberg			: (bio_rw(bio) == READ)
2138554df1c6d3bb7686b39ed775772f507fa857c19Andreas Gruenbacher			  ? READ_COMPLETED_WITH_ERROR
2148554df1c6d3bb7686b39ed775772f507fa857c19Andreas Gruenbacher			  : READ_AHEAD_COMPLETED_WITH_ERROR;
215b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	} else
2168554df1c6d3bb7686b39ed775772f507fa857c19Andreas Gruenbacher		what = COMPLETED_OK;
217b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
218b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	bio_put(req->private_bio);
219b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	req->private_bio = ERR_PTR(error);
220b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
221a115413de13ae6beb0cbfc198afe385a261ab284Lars Ellenberg	/* not req_mod(), we need irqsave here! */
22287eeee41f8740451b61a1e7d37a494333a906861Philipp Reisner	spin_lock_irqsave(&mdev->tconn->req_lock, flags);
223a115413de13ae6beb0cbfc198afe385a261ab284Lars Ellenberg	__req_mod(req, what, &m);
22487eeee41f8740451b61a1e7d37a494333a906861Philipp Reisner	spin_unlock_irqrestore(&mdev->tconn->req_lock, flags);
225a115413de13ae6beb0cbfc198afe385a261ab284Lars Ellenberg
226a115413de13ae6beb0cbfc198afe385a261ab284Lars Ellenberg	if (m.bio)
227a115413de13ae6beb0cbfc198afe385a261ab284Lars Ellenberg		complete_master_bio(mdev, &m);
228b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
229b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
23000d56944ff086f895e9ad184a7785ca1eece4a3bPhilipp Reisnerint w_read_retry_remote(struct drbd_work *w, int cancel)
231b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
232b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	struct drbd_request *req = container_of(w, struct drbd_request, w);
23300d56944ff086f895e9ad184a7785ca1eece4a3bPhilipp Reisner	struct drbd_conf *mdev = w->mdev;
234b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
235b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	/* We should not detach for read io-error,
236b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	 * but try to WRITE the P_DATA_REPLY to the failed location,
237b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	 * to give the disk the chance to relocate that block */
238b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
23987eeee41f8740451b61a1e7d37a494333a906861Philipp Reisner	spin_lock_irq(&mdev->tconn->req_lock);
240d255e5ff5fc6cc6c60dd014d1261448a7bbc8134Lars Ellenberg	if (cancel || mdev->state.pdsk != D_UP_TO_DATE) {
2418554df1c6d3bb7686b39ed775772f507fa857c19Andreas Gruenbacher		_req_mod(req, READ_RETRY_REMOTE_CANCELED);
24287eeee41f8740451b61a1e7d37a494333a906861Philipp Reisner		spin_unlock_irq(&mdev->tconn->req_lock);
243b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		return 1;
244b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
24587eeee41f8740451b61a1e7d37a494333a906861Philipp Reisner	spin_unlock_irq(&mdev->tconn->req_lock);
246b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
24700d56944ff086f895e9ad184a7785ca1eece4a3bPhilipp Reisner	return w_send_read_req(w, 0);
248b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
249b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
250f6ffca9f42902556bcf72426d2d0714bdbfdbe09Andreas Gruenbachervoid drbd_csum_ee(struct drbd_conf *mdev, struct crypto_hash *tfm,
251db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher		  struct drbd_peer_request *peer_req, void *digest)
25245bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg{
25345bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	struct hash_desc desc;
25445bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	struct scatterlist sg;
255db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher	struct page *page = peer_req->pages;
25645bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	struct page *tmp;
25745bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	unsigned len;
25845bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg
25945bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	desc.tfm = tfm;
26045bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	desc.flags = 0;
26145bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg
26245bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	sg_init_table(&sg, 1);
26345bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	crypto_hash_init(&desc);
26445bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg
26545bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	while ((tmp = page_chain_next(page))) {
26645bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg		/* all but the last page will be fully used */
26745bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg		sg_set_page(&sg, page, PAGE_SIZE, 0);
26845bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg		crypto_hash_update(&desc, &sg, sg.length);
26945bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg		page = tmp;
27045bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	}
27145bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	/* and now the last, possibly only partially used page */
272db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher	len = peer_req->i.size & (PAGE_SIZE - 1);
27345bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	sg_set_page(&sg, page, len ?: PAGE_SIZE, 0);
27445bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	crypto_hash_update(&desc, &sg, sg.length);
27545bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	crypto_hash_final(&desc, digest);
27645bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg}
27745bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg
27845bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenbergvoid drbd_csum_bio(struct drbd_conf *mdev, struct crypto_hash *tfm, struct bio *bio, void *digest)
279b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
280b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	struct hash_desc desc;
281b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	struct scatterlist sg;
282b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	struct bio_vec *bvec;
283b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	int i;
284b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
285b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	desc.tfm = tfm;
286b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	desc.flags = 0;
287b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
288b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	sg_init_table(&sg, 1);
289b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	crypto_hash_init(&desc);
290b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
291b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	__bio_for_each_segment(bvec, bio, i, 0) {
292b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		sg_set_page(&sg, bvec->bv_page, bvec->bv_len, bvec->bv_offset);
293b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		crypto_hash_update(&desc, &sg, sg.length);
294b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
295b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	crypto_hash_final(&desc, digest);
296b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
297b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
29800d56944ff086f895e9ad184a7785ca1eece4a3bPhilipp Reisnerstatic int w_e_send_csum(struct drbd_work *w, int cancel)
299b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
30000d56944ff086f895e9ad184a7785ca1eece4a3bPhilipp Reisner	struct drbd_peer_request *peer_req = container_of(w, struct drbd_peer_request, w);
30100d56944ff086f895e9ad184a7785ca1eece4a3bPhilipp Reisner	struct drbd_conf *mdev = w->mdev;
302b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	int digest_size;
303b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	void *digest;
30453ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenberg	int ok = 1;
305b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
30653ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenberg	if (unlikely(cancel))
30753ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenberg		goto out;
308b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
309db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher	if (likely((peer_req->flags & EE_WAS_ERROR) != 0))
31053ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenberg		goto out;
311b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
31253ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenberg	digest_size = crypto_hash_digestsize(mdev->csums_tfm);
31353ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenberg	digest = kmalloc(digest_size, GFP_NOIO);
31453ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenberg	if (digest) {
315db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher		sector_t sector = peer_req->i.sector;
316db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher		unsigned int size = peer_req->i.size;
317db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher		drbd_csum_ee(mdev, mdev->csums_tfm, peer_req, digest);
31853ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenberg		/* Free e and pages before send.
31953ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenberg		 * In case we block on congestion, we could otherwise run into
32053ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenberg		 * some distributed deadlock, if the other side blocks on
32153ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenberg		 * congestion as well, because our receiver blocks in
32253ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenberg		 * drbd_pp_alloc due to pp_in_use > max_buffers. */
323db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher		drbd_free_ee(mdev, peer_req);
324db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher		peer_req = NULL;
32553ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenberg		inc_rs_pending(mdev);
32653ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenberg		ok = drbd_send_drequest_csum(mdev, sector, size,
32753ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenberg					     digest, digest_size,
32853ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenberg					     P_CSUM_RS_REQUEST);
32953ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenberg		kfree(digest);
33053ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenberg	} else {
33153ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenberg		dev_err(DEV, "kmalloc() of digest failed.\n");
33253ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenberg		ok = 0;
33353ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenberg	}
334b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
33553ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenbergout:
336db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher	if (peer_req)
337db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher		drbd_free_ee(mdev, peer_req);
338b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
339b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (unlikely(!ok))
340b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		dev_err(DEV, "drbd_send_drequest(..., csum) failed\n");
341b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	return ok;
342b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
343b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
344b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner#define GFP_TRY	(__GFP_HIGHMEM | __GFP_NOWARN)
345b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
346b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisnerstatic int read_for_csum(struct drbd_conf *mdev, sector_t sector, int size)
347b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
348db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher	struct drbd_peer_request *peer_req;
349b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
350b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (!get_ldev(mdev))
35180a40e439e5a3f30b0a6210a1add6d7c33392e54Lars Ellenberg		return -EIO;
352b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
353e3555d8545976703938d1b59e2db509426dbe02cPhilipp Reisner	if (drbd_rs_should_slow_down(mdev, sector))
3540f0601f4ea2f53cfd8bcae060fb03d9bbde070ecLars Ellenberg		goto defer;
3550f0601f4ea2f53cfd8bcae060fb03d9bbde070ecLars Ellenberg
356b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	/* GFP_TRY, because if there is no memory available right now, this may
357b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	 * be rescheduled for later. It is "only" background resync, after all. */
358db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher	peer_req = drbd_alloc_ee(mdev, ID_SYNCER /* unused */, sector, size, GFP_TRY);
359db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher	if (!peer_req)
36080a40e439e5a3f30b0a6210a1add6d7c33392e54Lars Ellenberg		goto defer;
361b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
362db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher	peer_req->w.cb = w_e_send_csum;
36387eeee41f8740451b61a1e7d37a494333a906861Philipp Reisner	spin_lock_irq(&mdev->tconn->req_lock);
364db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher	list_add(&peer_req->w.list, &mdev->read_ee);
36587eeee41f8740451b61a1e7d37a494333a906861Philipp Reisner	spin_unlock_irq(&mdev->tconn->req_lock);
366b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
3670f0601f4ea2f53cfd8bcae060fb03d9bbde070ecLars Ellenberg	atomic_add(size >> 9, &mdev->rs_sect_ev);
368db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher	if (drbd_submit_ee(mdev, peer_req, READ, DRBD_FAULT_RS_RD) == 0)
36980a40e439e5a3f30b0a6210a1add6d7c33392e54Lars Ellenberg		return 0;
370b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
37110f6d9926cd17afff9dc03c967706419798b4929Lars Ellenberg	/* If it failed because of ENOMEM, retry should help.  If it failed
37210f6d9926cd17afff9dc03c967706419798b4929Lars Ellenberg	 * because bio_add_page failed (probably broken lower level driver),
37310f6d9926cd17afff9dc03c967706419798b4929Lars Ellenberg	 * retry may or may not help.
37410f6d9926cd17afff9dc03c967706419798b4929Lars Ellenberg	 * If it does not, you may need to force disconnect. */
37587eeee41f8740451b61a1e7d37a494333a906861Philipp Reisner	spin_lock_irq(&mdev->tconn->req_lock);
376db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher	list_del(&peer_req->w.list);
37787eeee41f8740451b61a1e7d37a494333a906861Philipp Reisner	spin_unlock_irq(&mdev->tconn->req_lock);
37822cc37a943832c948808884604ec6f5ff2594c1dLars Ellenberg
379db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher	drbd_free_ee(mdev, peer_req);
38080a40e439e5a3f30b0a6210a1add6d7c33392e54Lars Ellenbergdefer:
38145bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	put_ldev(mdev);
38280a40e439e5a3f30b0a6210a1add6d7c33392e54Lars Ellenberg	return -EAGAIN;
383b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
384b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
38500d56944ff086f895e9ad184a7785ca1eece4a3bPhilipp Reisnerint w_resync_timer(struct drbd_work *w, int cancel)
386b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
38700d56944ff086f895e9ad184a7785ca1eece4a3bPhilipp Reisner	struct drbd_conf *mdev = w->mdev;
38863106d3c6c769b6219bd04edde513b12abae3f61Philipp Reisner	switch (mdev->state.conn) {
38963106d3c6c769b6219bd04edde513b12abae3f61Philipp Reisner	case C_VERIFY_S:
39000d56944ff086f895e9ad184a7785ca1eece4a3bPhilipp Reisner		w_make_ov_request(w, cancel);
39163106d3c6c769b6219bd04edde513b12abae3f61Philipp Reisner		break;
39263106d3c6c769b6219bd04edde513b12abae3f61Philipp Reisner	case C_SYNC_TARGET:
39300d56944ff086f895e9ad184a7785ca1eece4a3bPhilipp Reisner		w_make_resync_request(w, cancel);
39463106d3c6c769b6219bd04edde513b12abae3f61Philipp Reisner		break;
395b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
396b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
397794abb753e29e85949b3719dbc2ab6a98711a47ePhilipp Reisner	return 1;
398794abb753e29e85949b3719dbc2ab6a98711a47ePhilipp Reisner}
399794abb753e29e85949b3719dbc2ab6a98711a47ePhilipp Reisner
400794abb753e29e85949b3719dbc2ab6a98711a47ePhilipp Reisnervoid resync_timer_fn(unsigned long data)
401794abb753e29e85949b3719dbc2ab6a98711a47ePhilipp Reisner{
402794abb753e29e85949b3719dbc2ab6a98711a47ePhilipp Reisner	struct drbd_conf *mdev = (struct drbd_conf *) data;
403794abb753e29e85949b3719dbc2ab6a98711a47ePhilipp Reisner
404794abb753e29e85949b3719dbc2ab6a98711a47ePhilipp Reisner	if (list_empty(&mdev->resync_work.list))
405e42325a57606396539807ff55c24febda39f8d01Philipp Reisner		drbd_queue_work(&mdev->tconn->data.work, &mdev->resync_work);
406b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
407b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
408778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisnerstatic void fifo_set(struct fifo_buffer *fb, int value)
409778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner{
410778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	int i;
411778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner
412778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	for (i = 0; i < fb->size; i++)
413f10f262349762c96ab247b6108af3a30b52b6f5aPhilipp Reisner		fb->values[i] = value;
414778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner}
415778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner
416778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisnerstatic int fifo_push(struct fifo_buffer *fb, int value)
417778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner{
418778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	int ov;
419778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner
420778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	ov = fb->values[fb->head_index];
421778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	fb->values[fb->head_index++] = value;
422778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner
423778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	if (fb->head_index >= fb->size)
424778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner		fb->head_index = 0;
425778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner
426778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	return ov;
427778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner}
428778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner
429778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisnerstatic void fifo_add_val(struct fifo_buffer *fb, int value)
430778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner{
431778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	int i;
432778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner
433778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	for (i = 0; i < fb->size; i++)
434778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner		fb->values[i] += value;
435778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner}
436778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner
4379d77a5fee9d2a1ea4cd9a841d27b107df5913b33Philipp Reisnerstatic int drbd_rs_controller(struct drbd_conf *mdev)
438778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner{
439778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	unsigned int sect_in;  /* Number of sectors that came in since the last turn */
440778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	unsigned int want;     /* The number of sectors we want in the proxy */
441778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	int req_sect; /* Number of sectors to request in this turn */
442778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	int correction; /* Number of sectors more we need in the proxy*/
443778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	int cps; /* correction per invocation of drbd_rs_controller() */
444778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	int steps; /* Number of time steps to plan ahead */
445778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	int curr_corr;
446778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	int max_sect;
447778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner
448778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	sect_in = atomic_xchg(&mdev->rs_sect_in, 0); /* Number of sectors that came in */
449778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	mdev->rs_in_flight -= sect_in;
450778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner
451778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	spin_lock(&mdev->peer_seq_lock); /* get an atomic view on mdev->rs_plan_s */
452778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner
453778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	steps = mdev->rs_plan_s.size; /* (mdev->sync_conf.c_plan_ahead * 10 * SLEEP_TIME) / HZ; */
454778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner
455778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	if (mdev->rs_in_flight + sect_in == 0) { /* At start of resync */
456778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner		want = ((mdev->sync_conf.rate * 2 * SLEEP_TIME) / HZ) * steps;
457778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	} else { /* normal path */
458778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner		want = mdev->sync_conf.c_fill_target ? mdev->sync_conf.c_fill_target :
459778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner			sect_in * mdev->sync_conf.c_delay_target * HZ / (SLEEP_TIME * 10);
460778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	}
461778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner
462778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	correction = want - mdev->rs_in_flight - mdev->rs_planed;
463778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner
464778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	/* Plan ahead */
465778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	cps = correction / steps;
466778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	fifo_add_val(&mdev->rs_plan_s, cps);
467778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	mdev->rs_planed += cps * steps;
468778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner
469778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	/* What we do in this step */
470778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	curr_corr = fifo_push(&mdev->rs_plan_s, 0);
471778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	spin_unlock(&mdev->peer_seq_lock);
472778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	mdev->rs_planed -= curr_corr;
473778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner
474778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	req_sect = sect_in + curr_corr;
475778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	if (req_sect < 0)
476778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner		req_sect = 0;
477778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner
478778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	max_sect = (mdev->sync_conf.c_max_rate * 2 * SLEEP_TIME) / HZ;
479778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	if (req_sect > max_sect)
480778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner		req_sect = max_sect;
481778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner
482778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	/*
483778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	dev_warn(DEV, "si=%u if=%d wa=%u co=%d st=%d cps=%d pl=%d cc=%d rs=%d\n",
484778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner		 sect_in, mdev->rs_in_flight, want, correction,
485778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner		 steps, cps, mdev->rs_planed, curr_corr, req_sect);
486778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	*/
487778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner
488778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	return req_sect;
489778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner}
490778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner
4919d77a5fee9d2a1ea4cd9a841d27b107df5913b33Philipp Reisnerstatic int drbd_rs_number_requests(struct drbd_conf *mdev)
492e65f440d474d7d6a6fd8a2c844e851d8c96ed9c5Lars Ellenberg{
493e65f440d474d7d6a6fd8a2c844e851d8c96ed9c5Lars Ellenberg	int number;
494e65f440d474d7d6a6fd8a2c844e851d8c96ed9c5Lars Ellenberg	if (mdev->rs_plan_s.size) { /* mdev->sync_conf.c_plan_ahead */
495e65f440d474d7d6a6fd8a2c844e851d8c96ed9c5Lars Ellenberg		number = drbd_rs_controller(mdev) >> (BM_BLOCK_SHIFT - 9);
496e65f440d474d7d6a6fd8a2c844e851d8c96ed9c5Lars Ellenberg		mdev->c_sync_rate = number * HZ * (BM_BLOCK_SIZE / 1024) / SLEEP_TIME;
497e65f440d474d7d6a6fd8a2c844e851d8c96ed9c5Lars Ellenberg	} else {
498e65f440d474d7d6a6fd8a2c844e851d8c96ed9c5Lars Ellenberg		mdev->c_sync_rate = mdev->sync_conf.rate;
499e65f440d474d7d6a6fd8a2c844e851d8c96ed9c5Lars Ellenberg		number = SLEEP_TIME * mdev->c_sync_rate  / ((BM_BLOCK_SIZE / 1024) * HZ);
500e65f440d474d7d6a6fd8a2c844e851d8c96ed9c5Lars Ellenberg	}
501e65f440d474d7d6a6fd8a2c844e851d8c96ed9c5Lars Ellenberg
502e65f440d474d7d6a6fd8a2c844e851d8c96ed9c5Lars Ellenberg	/* ignore the amount of pending requests, the resync controller should
503e65f440d474d7d6a6fd8a2c844e851d8c96ed9c5Lars Ellenberg	 * throttle down to incoming reply rate soon enough anyways. */
504e65f440d474d7d6a6fd8a2c844e851d8c96ed9c5Lars Ellenberg	return number;
505e65f440d474d7d6a6fd8a2c844e851d8c96ed9c5Lars Ellenberg}
506e65f440d474d7d6a6fd8a2c844e851d8c96ed9c5Lars Ellenberg
50700d56944ff086f895e9ad184a7785ca1eece4a3bPhilipp Reisnerstatic int w_make_resync_request(struct drbd_work *w, int cancel)
508b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
50900d56944ff086f895e9ad184a7785ca1eece4a3bPhilipp Reisner	struct drbd_conf *mdev = w->mdev;
510b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	unsigned long bit;
511b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	sector_t sector;
512b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	const sector_t capacity = drbd_get_capacity(mdev->this_bdev);
5131816a2b47afae838e53a177d5d166cc7be97d6b5Lars Ellenberg	int max_bio_size;
514e65f440d474d7d6a6fd8a2c844e851d8c96ed9c5Lars Ellenberg	int number, rollback_i, size;
515b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	int align, queued, sndbuf;
5160f0601f4ea2f53cfd8bcae060fb03d9bbde070ecLars Ellenberg	int i = 0;
517b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
518b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (unlikely(cancel))
519b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		return 1;
520b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
521af85e8e83d160f72a10e4467852646ac08614260Lars Ellenberg	if (mdev->rs_total == 0) {
522af85e8e83d160f72a10e4467852646ac08614260Lars Ellenberg		/* empty resync? */
523af85e8e83d160f72a10e4467852646ac08614260Lars Ellenberg		drbd_resync_finished(mdev);
524af85e8e83d160f72a10e4467852646ac08614260Lars Ellenberg		return 1;
525af85e8e83d160f72a10e4467852646ac08614260Lars Ellenberg	}
526af85e8e83d160f72a10e4467852646ac08614260Lars Ellenberg
527b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (!get_ldev(mdev)) {
528b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		/* Since we only need to access mdev->rsync a
529b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		   get_ldev_if_state(mdev,D_FAILED) would be sufficient, but
530b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		   to continue resync with a broken disk makes no sense at
531b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		   all */
532b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		dev_err(DEV, "Disk broke down during resync!\n");
533b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		return 1;
534b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
535b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
5360cfdd247d1779d5ffc8f685b172a526ecdc6773fPhilipp Reisner	max_bio_size = queue_max_hw_sectors(mdev->rq_queue) << 9;
537e65f440d474d7d6a6fd8a2c844e851d8c96ed9c5Lars Ellenberg	number = drbd_rs_number_requests(mdev);
538e65f440d474d7d6a6fd8a2c844e851d8c96ed9c5Lars Ellenberg	if (number == 0)
5390f0601f4ea2f53cfd8bcae060fb03d9bbde070ecLars Ellenberg		goto requeue;
540b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
541b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	for (i = 0; i < number; i++) {
542b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		/* Stop generating RS requests, when half of the send buffer is filled */
543e42325a57606396539807ff55c24febda39f8d01Philipp Reisner		mutex_lock(&mdev->tconn->data.mutex);
544e42325a57606396539807ff55c24febda39f8d01Philipp Reisner		if (mdev->tconn->data.socket) {
545e42325a57606396539807ff55c24febda39f8d01Philipp Reisner			queued = mdev->tconn->data.socket->sk->sk_wmem_queued;
546e42325a57606396539807ff55c24febda39f8d01Philipp Reisner			sndbuf = mdev->tconn->data.socket->sk->sk_sndbuf;
547b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		} else {
548b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			queued = 1;
549b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			sndbuf = 0;
550b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
551e42325a57606396539807ff55c24febda39f8d01Philipp Reisner		mutex_unlock(&mdev->tconn->data.mutex);
552b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (queued > sndbuf / 2)
553b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			goto requeue;
554b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
555b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisnernext_sector:
556b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		size = BM_BLOCK_SIZE;
557b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		bit  = drbd_bm_find_next(mdev, mdev->bm_resync_fo);
558b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
5594b0715f09655e76ca24c35a9e25e7c464c2f7346Lars Ellenberg		if (bit == DRBD_END_OF_BITMAP) {
560b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			mdev->bm_resync_fo = drbd_bm_bits(mdev);
561b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			put_ldev(mdev);
562b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			return 1;
563b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
564b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
565b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		sector = BM_BIT_TO_SECT(bit);
566b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
567e3555d8545976703938d1b59e2db509426dbe02cPhilipp Reisner		if (drbd_rs_should_slow_down(mdev, sector) ||
568e3555d8545976703938d1b59e2db509426dbe02cPhilipp Reisner		    drbd_try_rs_begin_io(mdev, sector)) {
569b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			mdev->bm_resync_fo = bit;
570b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			goto requeue;
571b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
572b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		mdev->bm_resync_fo = bit + 1;
573b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
574b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (unlikely(drbd_bm_test_bit(mdev, bit) == 0)) {
575b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			drbd_rs_complete_io(mdev, sector);
576b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			goto next_sector;
577b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
578b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
5791816a2b47afae838e53a177d5d166cc7be97d6b5Lars Ellenberg#if DRBD_MAX_BIO_SIZE > BM_BLOCK_SIZE
580b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		/* try to find some adjacent bits.
581b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		 * we stop if we have already the maximum req size.
582b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		 *
583b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		 * Additionally always align bigger requests, in order to
584b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		 * be prepared for all stripe sizes of software RAIDs.
585b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		 */
586b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		align = 1;
587d207450cf2731c6a2afa8c78fb31c7206cd35ebaPhilipp Reisner		rollback_i = i;
588b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		for (;;) {
5891816a2b47afae838e53a177d5d166cc7be97d6b5Lars Ellenberg			if (size + BM_BLOCK_SIZE > max_bio_size)
590b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner				break;
591b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
592b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			/* Be always aligned */
593b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			if (sector & ((1<<(align+3))-1))
594b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner				break;
595b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
596b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			/* do not cross extent boundaries */
597b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			if (((bit+1) & BM_BLOCKS_PER_BM_EXT_MASK) == 0)
598b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner				break;
599b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			/* now, is it actually dirty, after all?
600b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			 * caution, drbd_bm_test_bit is tri-state for some
601b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			 * obscure reason; ( b == 0 ) would get the out-of-band
602b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			 * only accidentally right because of the "oddly sized"
603b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			 * adjustment below */
604b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			if (drbd_bm_test_bit(mdev, bit+1) != 1)
605b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner				break;
606b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			bit++;
607b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			size += BM_BLOCK_SIZE;
608b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			if ((BM_BLOCK_SIZE << align) <= size)
609b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner				align++;
610b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			i++;
611b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
612b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		/* if we merged some,
613b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		 * reset the offset to start the next drbd_bm_find_next from */
614b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (size > BM_BLOCK_SIZE)
615b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			mdev->bm_resync_fo = bit + 1;
616b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner#endif
617b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
618b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		/* adjust very last sectors, in case we are oddly sized */
619b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (sector + (size>>9) > capacity)
620b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			size = (capacity-sector)<<9;
62131890f4ab299c4116cf0a104ca9ce4f9ca2c5da0Philipp Reisner		if (mdev->tconn->agreed_pro_version >= 89 && mdev->csums_tfm) {
622b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			switch (read_for_csum(mdev, sector, size)) {
62380a40e439e5a3f30b0a6210a1add6d7c33392e54Lars Ellenberg			case -EIO: /* Disk failure */
624b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner				put_ldev(mdev);
625b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner				return 0;
62680a40e439e5a3f30b0a6210a1add6d7c33392e54Lars Ellenberg			case -EAGAIN: /* allocation failed, or ldev busy */
627b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner				drbd_rs_complete_io(mdev, sector);
628b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner				mdev->bm_resync_fo = BM_SECT_TO_BIT(sector);
629d207450cf2731c6a2afa8c78fb31c7206cd35ebaPhilipp Reisner				i = rollback_i;
630b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner				goto requeue;
63180a40e439e5a3f30b0a6210a1add6d7c33392e54Lars Ellenberg			case 0:
63280a40e439e5a3f30b0a6210a1add6d7c33392e54Lars Ellenberg				/* everything ok */
63380a40e439e5a3f30b0a6210a1add6d7c33392e54Lars Ellenberg				break;
63480a40e439e5a3f30b0a6210a1add6d7c33392e54Lars Ellenberg			default:
63580a40e439e5a3f30b0a6210a1add6d7c33392e54Lars Ellenberg				BUG();
636b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			}
637b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		} else {
638b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			inc_rs_pending(mdev);
639b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			if (!drbd_send_drequest(mdev, P_RS_DATA_REQUEST,
640b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner					       sector, size, ID_SYNCER)) {
641b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner				dev_err(DEV, "drbd_send_drequest() failed, aborting...\n");
642b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner				dec_rs_pending(mdev);
643b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner				put_ldev(mdev);
644b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner				return 0;
645b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			}
646b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
647b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
648b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
649b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (mdev->bm_resync_fo >= drbd_bm_bits(mdev)) {
650b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		/* last syncer _request_ was sent,
651b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		 * but the P_RS_DATA_REPLY not yet received.  sync will end (and
652b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		 * next sync group will resume), as soon as we receive the last
653b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		 * resync data block, and the last bit is cleared.
654b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		 * until then resync "work" is "inactive" ...
655b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		 */
656b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		put_ldev(mdev);
657b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		return 1;
658b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
659b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
660b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner requeue:
661778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	mdev->rs_in_flight += (i << (BM_BLOCK_SHIFT - 9));
662b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	mod_timer(&mdev->resync_timer, jiffies + SLEEP_TIME);
663b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	put_ldev(mdev);
664b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	return 1;
665b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
666b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
66700d56944ff086f895e9ad184a7785ca1eece4a3bPhilipp Reisnerstatic int w_make_ov_request(struct drbd_work *w, int cancel)
668b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
66900d56944ff086f895e9ad184a7785ca1eece4a3bPhilipp Reisner	struct drbd_conf *mdev = w->mdev;
670b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	int number, i, size;
671b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	sector_t sector;
672b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	const sector_t capacity = drbd_get_capacity(mdev->this_bdev);
673b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
674b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (unlikely(cancel))
675b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		return 1;
676b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
6772649f0809f55e4df98c333a2b85c6fc8fee04804Lars Ellenberg	number = drbd_rs_number_requests(mdev);
678b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
679b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	sector = mdev->ov_position;
680b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	for (i = 0; i < number; i++) {
681b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (sector >= capacity) {
682b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			return 1;
683b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
684b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
685b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		size = BM_BLOCK_SIZE;
686b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
687e3555d8545976703938d1b59e2db509426dbe02cPhilipp Reisner		if (drbd_rs_should_slow_down(mdev, sector) ||
688e3555d8545976703938d1b59e2db509426dbe02cPhilipp Reisner		    drbd_try_rs_begin_io(mdev, sector)) {
689b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			mdev->ov_position = sector;
690b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			goto requeue;
691b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
692b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
693b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (sector + (size>>9) > capacity)
694b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			size = (capacity-sector)<<9;
695b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
696b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		inc_rs_pending(mdev);
697b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (!drbd_send_ov_request(mdev, sector, size)) {
698b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			dec_rs_pending(mdev);
699b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			return 0;
700b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
701b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		sector += BM_SECT_PER_BIT;
702b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
703b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	mdev->ov_position = sector;
704b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
705b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner requeue:
7062649f0809f55e4df98c333a2b85c6fc8fee04804Lars Ellenberg	mdev->rs_in_flight += (i << (BM_BLOCK_SHIFT - 9));
707b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	mod_timer(&mdev->resync_timer, jiffies + SLEEP_TIME);
708b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	return 1;
709b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
710b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
71100d56944ff086f895e9ad184a7785ca1eece4a3bPhilipp Reisnerint w_ov_finished(struct drbd_work *w, int cancel)
712b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
71300d56944ff086f895e9ad184a7785ca1eece4a3bPhilipp Reisner	struct drbd_conf *mdev = w->mdev;
714b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	kfree(w);
715b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	ov_oos_print(mdev);
716b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	drbd_resync_finished(mdev);
717b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
718b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	return 1;
719b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
720b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
72100d56944ff086f895e9ad184a7785ca1eece4a3bPhilipp Reisnerstatic int w_resync_finished(struct drbd_work *w, int cancel)
722b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
72300d56944ff086f895e9ad184a7785ca1eece4a3bPhilipp Reisner	struct drbd_conf *mdev = w->mdev;
724b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	kfree(w);
725b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
726b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	drbd_resync_finished(mdev);
727b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
728b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	return 1;
729b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
730b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
731af85e8e83d160f72a10e4467852646ac08614260Lars Ellenbergstatic void ping_peer(struct drbd_conf *mdev)
732af85e8e83d160f72a10e4467852646ac08614260Lars Ellenberg{
7332a67d8b93b3363d4a5608d16d510a4bf6b3863fbPhilipp Reisner	struct drbd_tconn *tconn = mdev->tconn;
7342a67d8b93b3363d4a5608d16d510a4bf6b3863fbPhilipp Reisner
7352a67d8b93b3363d4a5608d16d510a4bf6b3863fbPhilipp Reisner	clear_bit(GOT_PING_ACK, &tconn->flags);
7362a67d8b93b3363d4a5608d16d510a4bf6b3863fbPhilipp Reisner	request_ping(tconn);
7372a67d8b93b3363d4a5608d16d510a4bf6b3863fbPhilipp Reisner	wait_event(tconn->ping_wait,
7382a67d8b93b3363d4a5608d16d510a4bf6b3863fbPhilipp Reisner		   test_bit(GOT_PING_ACK, &tconn->flags) || mdev->state.conn < C_CONNECTED);
739af85e8e83d160f72a10e4467852646ac08614260Lars Ellenberg}
740af85e8e83d160f72a10e4467852646ac08614260Lars Ellenberg
741b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisnerint drbd_resync_finished(struct drbd_conf *mdev)
742b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
743b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	unsigned long db, dt, dbdt;
744b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	unsigned long n_oos;
745b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	union drbd_state os, ns;
746b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	struct drbd_work *w;
747b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	char *khelper_cmd = NULL;
74826525618863afcc4aab8b2a83451d37c6f513460Lars Ellenberg	int verify_done = 0;
749b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
750b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	/* Remove all elements from the resync LRU. Since future actions
751b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	 * might set bits in the (main) bitmap, then the entries in the
752b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	 * resync LRU would be wrong. */
753b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (drbd_rs_del_all(mdev)) {
754b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		/* In case this is not possible now, most probably because
755b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		 * there are P_RS_DATA_REPLY Packets lingering on the worker's
756b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		 * queue (or even the read operations for those packets
757b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		 * is not finished by now).   Retry in 100ms. */
758b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
75920ee639024e3d33111df0e343050b218c656bf16Philipp Reisner		schedule_timeout_interruptible(HZ / 10);
760b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		w = kmalloc(sizeof(struct drbd_work), GFP_ATOMIC);
761b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (w) {
762b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			w->cb = w_resync_finished;
763e42325a57606396539807ff55c24febda39f8d01Philipp Reisner			drbd_queue_work(&mdev->tconn->data.work, w);
764b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			return 1;
765b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
766b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		dev_err(DEV, "Warn failed to drbd_rs_del_all() and to kmalloc(w).\n");
767b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
768b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
769b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	dt = (jiffies - mdev->rs_start - mdev->rs_paused) / HZ;
770b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (dt <= 0)
771b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		dt = 1;
772b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	db = mdev->rs_total;
773b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	dbdt = Bit2KB(db/dt);
774b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	mdev->rs_paused /= HZ;
775b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
776b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (!get_ldev(mdev))
777b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		goto out;
778b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
779af85e8e83d160f72a10e4467852646ac08614260Lars Ellenberg	ping_peer(mdev);
780af85e8e83d160f72a10e4467852646ac08614260Lars Ellenberg
78187eeee41f8740451b61a1e7d37a494333a906861Philipp Reisner	spin_lock_irq(&mdev->tconn->req_lock);
782b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	os = mdev->state;
783b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
78426525618863afcc4aab8b2a83451d37c6f513460Lars Ellenberg	verify_done = (os.conn == C_VERIFY_S || os.conn == C_VERIFY_T);
78526525618863afcc4aab8b2a83451d37c6f513460Lars Ellenberg
786b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	/* This protects us against multiple calls (that can happen in the presence
787b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	   of application IO), and against connectivity loss just before we arrive here. */
788b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (os.conn <= C_CONNECTED)
789b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		goto out_unlock;
790b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
791b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	ns = os;
792b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	ns.conn = C_CONNECTED;
793b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
794b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	dev_info(DEV, "%s done (total %lu sec; paused %lu sec; %lu K/sec)\n",
79526525618863afcc4aab8b2a83451d37c6f513460Lars Ellenberg	     verify_done ? "Online verify " : "Resync",
796b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	     dt + mdev->rs_paused, mdev->rs_paused, dbdt);
797b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
798b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	n_oos = drbd_bm_total_weight(mdev);
799b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
800b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (os.conn == C_VERIFY_S || os.conn == C_VERIFY_T) {
801b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (n_oos) {
802b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			dev_alert(DEV, "Online verify found %lu %dk block out of sync!\n",
803b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			      n_oos, Bit2KB(1));
804b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			khelper_cmd = "out-of-sync";
805b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
806b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	} else {
807b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		D_ASSERT((n_oos - mdev->rs_failed) == 0);
808b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
809b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (os.conn == C_SYNC_TARGET || os.conn == C_PAUSED_SYNC_T)
810b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			khelper_cmd = "after-resync-target";
811b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
812b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (mdev->csums_tfm && mdev->rs_total) {
813b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			const unsigned long s = mdev->rs_same_csum;
814b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			const unsigned long t = mdev->rs_total;
815b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			const int ratio =
816b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner				(t == 0)     ? 0 :
817b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			(t < 100000) ? ((s*100)/t) : (s/(t/100));
81824c4830c8ec3cbc904d84c213126a35f41a4e455Bart Van Assche			dev_info(DEV, "%u %% had equal checksums, eliminated: %luK; "
819b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			     "transferred %luK total %luK\n",
820b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			     ratio,
821b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			     Bit2KB(mdev->rs_same_csum),
822b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			     Bit2KB(mdev->rs_total - mdev->rs_same_csum),
823b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			     Bit2KB(mdev->rs_total));
824b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
825b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
826b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
827b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (mdev->rs_failed) {
828b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		dev_info(DEV, "            %lu failed blocks\n", mdev->rs_failed);
829b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
830b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (os.conn == C_SYNC_TARGET || os.conn == C_PAUSED_SYNC_T) {
831b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			ns.disk = D_INCONSISTENT;
832b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			ns.pdsk = D_UP_TO_DATE;
833b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		} else {
834b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			ns.disk = D_UP_TO_DATE;
835b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			ns.pdsk = D_INCONSISTENT;
836b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
837b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	} else {
838b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		ns.disk = D_UP_TO_DATE;
839b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		ns.pdsk = D_UP_TO_DATE;
840b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
841b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (os.conn == C_SYNC_TARGET || os.conn == C_PAUSED_SYNC_T) {
842b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			if (mdev->p_uuid) {
843b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner				int i;
844b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner				for (i = UI_BITMAP ; i <= UI_HISTORY_END ; i++)
845b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner					_drbd_uuid_set(mdev, i, mdev->p_uuid[i]);
846b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner				drbd_uuid_set(mdev, UI_BITMAP, mdev->ldev->md.uuid[UI_CURRENT]);
847b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner				_drbd_uuid_set(mdev, UI_CURRENT, mdev->p_uuid[UI_CURRENT]);
848b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			} else {
849b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner				dev_err(DEV, "mdev->p_uuid is NULL! BUG\n");
850b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			}
851b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
852b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
85362b0da3a244ac33d25a77861ef1cc0080103f2ffLars Ellenberg		if (!(os.conn == C_VERIFY_S || os.conn == C_VERIFY_T)) {
85462b0da3a244ac33d25a77861ef1cc0080103f2ffLars Ellenberg			/* for verify runs, we don't update uuids here,
85562b0da3a244ac33d25a77861ef1cc0080103f2ffLars Ellenberg			 * so there would be nothing to report. */
85662b0da3a244ac33d25a77861ef1cc0080103f2ffLars Ellenberg			drbd_uuid_set_bm(mdev, 0UL);
85762b0da3a244ac33d25a77861ef1cc0080103f2ffLars Ellenberg			drbd_print_uuids(mdev, "updated UUIDs");
85862b0da3a244ac33d25a77861ef1cc0080103f2ffLars Ellenberg			if (mdev->p_uuid) {
85962b0da3a244ac33d25a77861ef1cc0080103f2ffLars Ellenberg				/* Now the two UUID sets are equal, update what we
86062b0da3a244ac33d25a77861ef1cc0080103f2ffLars Ellenberg				 * know of the peer. */
86162b0da3a244ac33d25a77861ef1cc0080103f2ffLars Ellenberg				int i;
86262b0da3a244ac33d25a77861ef1cc0080103f2ffLars Ellenberg				for (i = UI_CURRENT ; i <= UI_HISTORY_END ; i++)
86362b0da3a244ac33d25a77861ef1cc0080103f2ffLars Ellenberg					mdev->p_uuid[i] = mdev->ldev->md.uuid[i];
86462b0da3a244ac33d25a77861ef1cc0080103f2ffLars Ellenberg			}
865b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
866b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
867b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
868b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	_drbd_set_state(mdev, ns, CS_VERBOSE, NULL);
869b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisnerout_unlock:
87087eeee41f8740451b61a1e7d37a494333a906861Philipp Reisner	spin_unlock_irq(&mdev->tconn->req_lock);
871b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	put_ldev(mdev);
872b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisnerout:
873b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	mdev->rs_total  = 0;
874b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	mdev->rs_failed = 0;
875b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	mdev->rs_paused = 0;
87626525618863afcc4aab8b2a83451d37c6f513460Lars Ellenberg	if (verify_done)
87726525618863afcc4aab8b2a83451d37c6f513460Lars Ellenberg		mdev->ov_start_sector = 0;
878b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
87913d42685bec1f012dcbc5d187490eb1d15ec8219Lars Ellenberg	drbd_md_sync(mdev);
88013d42685bec1f012dcbc5d187490eb1d15ec8219Lars Ellenberg
881b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (khelper_cmd)
882b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		drbd_khelper(mdev, khelper_cmd);
883b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
884b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	return 1;
885b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
886b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
887b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner/* helper */
888db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacherstatic void move_to_net_ee_or_free(struct drbd_conf *mdev, struct drbd_peer_request *peer_req)
889b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
890db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher	if (drbd_ee_has_active_page(peer_req)) {
891b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		/* This might happen if sendpage() has not finished */
892db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher		int i = (peer_req->i.size + PAGE_SIZE -1) >> PAGE_SHIFT;
893435f07402b3165b90592073bc0f8c6f8fa160ff9Lars Ellenberg		atomic_add(i, &mdev->pp_in_use_by_net);
894435f07402b3165b90592073bc0f8c6f8fa160ff9Lars Ellenberg		atomic_sub(i, &mdev->pp_in_use);
89587eeee41f8740451b61a1e7d37a494333a906861Philipp Reisner		spin_lock_irq(&mdev->tconn->req_lock);
896db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher		list_add_tail(&peer_req->w.list, &mdev->net_ee);
89787eeee41f8740451b61a1e7d37a494333a906861Philipp Reisner		spin_unlock_irq(&mdev->tconn->req_lock);
898435f07402b3165b90592073bc0f8c6f8fa160ff9Lars Ellenberg		wake_up(&drbd_pp_wait);
899b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	} else
900db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher		drbd_free_ee(mdev, peer_req);
901b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
902b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
903b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner/**
904b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * w_e_end_data_req() - Worker callback, to send a P_DATA_REPLY packet in response to a P_DATA_REQUEST
905b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * @mdev:	DRBD device.
906b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * @w:		work object.
907b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * @cancel:	The connection will be closed anyways
908b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner */
90900d56944ff086f895e9ad184a7785ca1eece4a3bPhilipp Reisnerint w_e_end_data_req(struct drbd_work *w, int cancel)
910b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
911db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher	struct drbd_peer_request *peer_req = container_of(w, struct drbd_peer_request, w);
91200d56944ff086f895e9ad184a7785ca1eece4a3bPhilipp Reisner	struct drbd_conf *mdev = w->mdev;
913b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	int ok;
914b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
915b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (unlikely(cancel)) {
916db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher		drbd_free_ee(mdev, peer_req);
917b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		dec_unacked(mdev);
918b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		return 1;
919b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
920b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
921db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher	if (likely((peer_req->flags & EE_WAS_ERROR) == 0)) {
922db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher		ok = drbd_send_block(mdev, P_DATA_REPLY, peer_req);
923b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	} else {
924b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (__ratelimit(&drbd_ratelimit_state))
925b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			dev_err(DEV, "Sending NegDReply. sector=%llus.\n",
926db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher			    (unsigned long long)peer_req->i.sector);
927b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
928db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher		ok = drbd_send_ack(mdev, P_NEG_DREPLY, peer_req);
929b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
930b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
931b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	dec_unacked(mdev);
932b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
933db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher	move_to_net_ee_or_free(mdev, peer_req);
934b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
935b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (unlikely(!ok))
936b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		dev_err(DEV, "drbd_send_block() failed\n");
937b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	return ok;
938b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
939b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
940b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner/**
941b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * w_e_end_rsdata_req() - Worker callback to send a P_RS_DATA_REPLY packet in response to a P_RS_DATA_REQUESTRS
942b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * @mdev:	DRBD device.
943b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * @w:		work object.
944b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * @cancel:	The connection will be closed anyways
945b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner */
94600d56944ff086f895e9ad184a7785ca1eece4a3bPhilipp Reisnerint w_e_end_rsdata_req(struct drbd_work *w, int cancel)
947b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
948db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher	struct drbd_peer_request *peer_req = container_of(w, struct drbd_peer_request, w);
94900d56944ff086f895e9ad184a7785ca1eece4a3bPhilipp Reisner	struct drbd_conf *mdev = w->mdev;
950b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	int ok;
951b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
952b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (unlikely(cancel)) {
953db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher		drbd_free_ee(mdev, peer_req);
954b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		dec_unacked(mdev);
955b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		return 1;
956b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
957b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
958b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (get_ldev_if_state(mdev, D_FAILED)) {
959db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher		drbd_rs_complete_io(mdev, peer_req->i.sector);
960b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		put_ldev(mdev);
961b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
962b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
963d612d309e4c8401ad94c531678b59c4a8b7c41cePhilipp Reisner	if (mdev->state.conn == C_AHEAD) {
964db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher		ok = drbd_send_ack(mdev, P_RS_CANCEL, peer_req);
965db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher	} else if (likely((peer_req->flags & EE_WAS_ERROR) == 0)) {
966b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (likely(mdev->state.pdsk >= D_INCONSISTENT)) {
967b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			inc_rs_pending(mdev);
968db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher			ok = drbd_send_block(mdev, P_RS_DATA_REPLY, peer_req);
969b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		} else {
970b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			if (__ratelimit(&drbd_ratelimit_state))
971b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner				dev_err(DEV, "Not sending RSDataReply, "
972b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner				    "partner DISKLESS!\n");
973b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			ok = 1;
974b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
975b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	} else {
976b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (__ratelimit(&drbd_ratelimit_state))
977b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			dev_err(DEV, "Sending NegRSDReply. sector %llus.\n",
978db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher			    (unsigned long long)peer_req->i.sector);
979b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
980db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher		ok = drbd_send_ack(mdev, P_NEG_RS_DREPLY, peer_req);
981b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
982b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		/* update resync data with failure */
983db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher		drbd_rs_failed_io(mdev, peer_req->i.sector, peer_req->i.size);
984b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
985b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
986b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	dec_unacked(mdev);
987b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
988db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher	move_to_net_ee_or_free(mdev, peer_req);
989b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
990b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (unlikely(!ok))
991b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		dev_err(DEV, "drbd_send_block() failed\n");
992b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	return ok;
993b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
994b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
99500d56944ff086f895e9ad184a7785ca1eece4a3bPhilipp Reisnerint w_e_end_csum_rs_req(struct drbd_work *w, int cancel)
996b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
997db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher	struct drbd_peer_request *peer_req = container_of(w, struct drbd_peer_request, w);
99800d56944ff086f895e9ad184a7785ca1eece4a3bPhilipp Reisner	struct drbd_conf *mdev = w->mdev;
999b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	struct digest_info *di;
1000b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	int digest_size;
1001b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	void *digest = NULL;
1002b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	int ok, eq = 0;
1003b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1004b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (unlikely(cancel)) {
1005db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher		drbd_free_ee(mdev, peer_req);
1006b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		dec_unacked(mdev);
1007b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		return 1;
1008b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
1009b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
10101d53f09e170e477de67babd7a10e277479260d51Lars Ellenberg	if (get_ldev(mdev)) {
1011db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher		drbd_rs_complete_io(mdev, peer_req->i.sector);
10121d53f09e170e477de67babd7a10e277479260d51Lars Ellenberg		put_ldev(mdev);
10131d53f09e170e477de67babd7a10e277479260d51Lars Ellenberg	}
1014b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1015db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher	di = peer_req->digest;
1016b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1017db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher	if (likely((peer_req->flags & EE_WAS_ERROR) == 0)) {
1018b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		/* quick hack to try to avoid a race against reconfiguration.
1019b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		 * a real fix would be much more involved,
1020b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		 * introducing more locking mechanisms */
1021b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (mdev->csums_tfm) {
1022b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			digest_size = crypto_hash_digestsize(mdev->csums_tfm);
1023b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			D_ASSERT(digest_size == di->digest_size);
1024b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			digest = kmalloc(digest_size, GFP_NOIO);
1025b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
1026b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (digest) {
1027db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher			drbd_csum_ee(mdev, mdev->csums_tfm, peer_req, digest);
1028b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			eq = !memcmp(digest, di->digest, digest_size);
1029b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			kfree(digest);
1030b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
1031b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1032b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (eq) {
1033db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher			drbd_set_in_sync(mdev, peer_req->i.sector, peer_req->i.size);
1034676396d545350a70d922605ec23c2ed26124334aLars Ellenberg			/* rs_same_csums unit is BM_BLOCK_SIZE */
1035db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher			mdev->rs_same_csum += peer_req->i.size >> BM_BLOCK_SHIFT;
1036db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher			ok = drbd_send_ack(mdev, P_RS_IS_IN_SYNC, peer_req);
1037b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		} else {
1038b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			inc_rs_pending(mdev);
1039db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher			peer_req->block_id = ID_SYNCER; /* By setting block_id, digest pointer becomes invalid! */
1040db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher			peer_req->flags &= ~EE_HAS_DIGEST; /* This peer request no longer has a digest pointer */
1041204bba9965c4cc175bf5bc65ddd19889e9085c72Philipp Reisner			kfree(di);
1042db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher			ok = drbd_send_block(mdev, P_RS_DATA_REPLY, peer_req);
1043b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
1044b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	} else {
1045db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher		ok = drbd_send_ack(mdev, P_NEG_RS_DREPLY, peer_req);
1046b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (__ratelimit(&drbd_ratelimit_state))
1047b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			dev_err(DEV, "Sending NegDReply. I guess it gets messy.\n");
1048b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
1049b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1050b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	dec_unacked(mdev);
1051db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher	move_to_net_ee_or_free(mdev, peer_req);
1052b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1053b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (unlikely(!ok))
1054b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		dev_err(DEV, "drbd_send_block/ack() failed\n");
1055b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	return ok;
1056b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
1057b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
105800d56944ff086f895e9ad184a7785ca1eece4a3bPhilipp Reisnerint w_e_end_ov_req(struct drbd_work *w, int cancel)
1059b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
1060db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher	struct drbd_peer_request *peer_req = container_of(w, struct drbd_peer_request, w);
106100d56944ff086f895e9ad184a7785ca1eece4a3bPhilipp Reisner	struct drbd_conf *mdev = w->mdev;
1062db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher	sector_t sector = peer_req->i.sector;
1063db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher	unsigned int size = peer_req->i.size;
1064b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	int digest_size;
1065b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	void *digest;
1066b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	int ok = 1;
1067b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1068b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (unlikely(cancel))
1069b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		goto out;
1070b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1071b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	digest_size = crypto_hash_digestsize(mdev->verify_tfm);
1072b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	digest = kmalloc(digest_size, GFP_NOIO);
10738f21420ebd5ca5a751e2f606b49b0acd2a2af314Philipp Reisner	if (!digest) {
10748f21420ebd5ca5a751e2f606b49b0acd2a2af314Philipp Reisner		ok = 0;	/* terminate the connection in case the allocation failed */
10758f21420ebd5ca5a751e2f606b49b0acd2a2af314Philipp Reisner		goto out;
1076b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
1077b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1078db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher	if (likely(!(peer_req->flags & EE_WAS_ERROR)))
1079db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher		drbd_csum_ee(mdev, mdev->verify_tfm, peer_req, digest);
10808f21420ebd5ca5a751e2f606b49b0acd2a2af314Philipp Reisner	else
10818f21420ebd5ca5a751e2f606b49b0acd2a2af314Philipp Reisner		memset(digest, 0, digest_size);
10828f21420ebd5ca5a751e2f606b49b0acd2a2af314Philipp Reisner
108353ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenberg	/* Free e and pages before send.
108453ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenberg	 * In case we block on congestion, we could otherwise run into
108553ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenberg	 * some distributed deadlock, if the other side blocks on
108653ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenberg	 * congestion as well, because our receiver blocks in
108753ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenberg	 * drbd_pp_alloc due to pp_in_use > max_buffers. */
1088db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher	drbd_free_ee(mdev, peer_req);
1089db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher	peer_req = NULL;
10908f21420ebd5ca5a751e2f606b49b0acd2a2af314Philipp Reisner	inc_rs_pending(mdev);
109153ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenberg	ok = drbd_send_drequest_csum(mdev, sector, size,
109253ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenberg				     digest, digest_size,
109353ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenberg				     P_OV_REPLY);
10948f21420ebd5ca5a751e2f606b49b0acd2a2af314Philipp Reisner	if (!ok)
10958f21420ebd5ca5a751e2f606b49b0acd2a2af314Philipp Reisner		dec_rs_pending(mdev);
10968f21420ebd5ca5a751e2f606b49b0acd2a2af314Philipp Reisner	kfree(digest);
10978f21420ebd5ca5a751e2f606b49b0acd2a2af314Philipp Reisner
1098b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisnerout:
1099db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher	if (peer_req)
1100db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher		drbd_free_ee(mdev, peer_req);
1101b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	dec_unacked(mdev);
1102b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	return ok;
1103b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
1104b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1105b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisnervoid drbd_ov_oos_found(struct drbd_conf *mdev, sector_t sector, int size)
1106b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
1107b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (mdev->ov_last_oos_start + mdev->ov_last_oos_size == sector) {
1108b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		mdev->ov_last_oos_size += size>>9;
1109b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	} else {
1110b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		mdev->ov_last_oos_start = sector;
1111b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		mdev->ov_last_oos_size = size>>9;
1112b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
1113b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	drbd_set_out_of_sync(mdev, sector, size);
1114b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
1115b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
111600d56944ff086f895e9ad184a7785ca1eece4a3bPhilipp Reisnerint w_e_end_ov_reply(struct drbd_work *w, int cancel)
1117b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
1118db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher	struct drbd_peer_request *peer_req = container_of(w, struct drbd_peer_request, w);
111900d56944ff086f895e9ad184a7785ca1eece4a3bPhilipp Reisner	struct drbd_conf *mdev = w->mdev;
1120b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	struct digest_info *di;
1121b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	void *digest;
1122db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher	sector_t sector = peer_req->i.sector;
1123db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher	unsigned int size = peer_req->i.size;
112453ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenberg	int digest_size;
1125b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	int ok, eq = 0;
1126b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1127b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (unlikely(cancel)) {
1128db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher		drbd_free_ee(mdev, peer_req);
1129b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		dec_unacked(mdev);
1130b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		return 1;
1131b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
1132b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1133b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	/* after "cancel", because after drbd_disconnect/drbd_rs_cancel_all
1134b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	 * the resync lru has been cleaned up already */
11351d53f09e170e477de67babd7a10e277479260d51Lars Ellenberg	if (get_ldev(mdev)) {
1136db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher		drbd_rs_complete_io(mdev, peer_req->i.sector);
11371d53f09e170e477de67babd7a10e277479260d51Lars Ellenberg		put_ldev(mdev);
11381d53f09e170e477de67babd7a10e277479260d51Lars Ellenberg	}
1139b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1140db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher	di = peer_req->digest;
1141b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1142db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher	if (likely((peer_req->flags & EE_WAS_ERROR) == 0)) {
1143b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		digest_size = crypto_hash_digestsize(mdev->verify_tfm);
1144b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		digest = kmalloc(digest_size, GFP_NOIO);
1145b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (digest) {
1146db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher			drbd_csum_ee(mdev, mdev->verify_tfm, peer_req, digest);
1147b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1148b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			D_ASSERT(digest_size == di->digest_size);
1149b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			eq = !memcmp(digest, di->digest, digest_size);
1150b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			kfree(digest);
1151b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
1152b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
1153b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
115453ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenberg		/* Free e and pages before send.
115553ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenberg		 * In case we block on congestion, we could otherwise run into
115653ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenberg		 * some distributed deadlock, if the other side blocks on
115753ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenberg		 * congestion as well, because our receiver blocks in
115853ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenberg		 * drbd_pp_alloc due to pp_in_use > max_buffers. */
1159db830c464b69e26ea4d371e38bb2320c99c82f41Andreas Gruenbacher	drbd_free_ee(mdev, peer_req);
1160b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (!eq)
116153ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenberg		drbd_ov_oos_found(mdev, sector, size);
1162b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	else
1163b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		ov_oos_print(mdev);
1164b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
116553ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenberg	ok = drbd_send_ack_ex(mdev, P_OV_RESULT, sector, size,
1166b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			      eq ? ID_IN_SYNC : ID_OUT_OF_SYNC);
1167b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
116853ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenberg	dec_unacked(mdev);
1169b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1170ea5442aff68c559c951373739201721185191748Lars Ellenberg	--mdev->ov_left;
1171ea5442aff68c559c951373739201721185191748Lars Ellenberg
1172ea5442aff68c559c951373739201721185191748Lars Ellenberg	/* let's advance progress step marks only for every other megabyte */
1173ea5442aff68c559c951373739201721185191748Lars Ellenberg	if ((mdev->ov_left & 0x200) == 0x200)
1174ea5442aff68c559c951373739201721185191748Lars Ellenberg		drbd_advance_rs_marks(mdev, mdev->ov_left);
1175ea5442aff68c559c951373739201721185191748Lars Ellenberg
1176ea5442aff68c559c951373739201721185191748Lars Ellenberg	if (mdev->ov_left == 0) {
1177b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		ov_oos_print(mdev);
1178b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		drbd_resync_finished(mdev);
1179b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
1180b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1181b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	return ok;
1182b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
1183b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
118400d56944ff086f895e9ad184a7785ca1eece4a3bPhilipp Reisnerint w_prev_work_done(struct drbd_work *w, int cancel)
1185b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
1186b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	struct drbd_wq_barrier *b = container_of(w, struct drbd_wq_barrier, w);
118700d56944ff086f895e9ad184a7785ca1eece4a3bPhilipp Reisner
1188b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	complete(&b->done);
1189b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	return 1;
1190b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
1191b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
119200d56944ff086f895e9ad184a7785ca1eece4a3bPhilipp Reisnerint w_send_barrier(struct drbd_work *w, int cancel)
1193b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
1194b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	struct drbd_tl_epoch *b = container_of(w, struct drbd_tl_epoch, w);
119500d56944ff086f895e9ad184a7785ca1eece4a3bPhilipp Reisner	struct drbd_conf *mdev = w->mdev;
1196e42325a57606396539807ff55c24febda39f8d01Philipp Reisner	struct p_barrier *p = &mdev->tconn->data.sbuf.barrier;
1197b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	int ok = 1;
1198b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1199b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	/* really avoid racing with tl_clear.  w.cb may have been referenced
1200b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	 * just before it was reassigned and re-queued, so double check that.
1201b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	 * actually, this race was harmless, since we only try to send the
1202b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	 * barrier packet here, and otherwise do nothing with the object.
1203b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	 * but compare with the head of w_clear_epoch */
120487eeee41f8740451b61a1e7d37a494333a906861Philipp Reisner	spin_lock_irq(&mdev->tconn->req_lock);
1205b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (w->cb != w_send_barrier || mdev->state.conn < C_CONNECTED)
1206b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		cancel = 1;
120787eeee41f8740451b61a1e7d37a494333a906861Philipp Reisner	spin_unlock_irq(&mdev->tconn->req_lock);
1208b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (cancel)
1209b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		return 1;
1210b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1211611208706f28c502c8c01791ac4f0b14cde395b2Philipp Reisner	if (!drbd_get_data_sock(mdev->tconn))
1212b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		return 0;
1213b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	p->barrier = b->br_number;
1214b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	/* inc_ap_pending was done where this was queued.
1215b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	 * dec_ap_pending will be done in got_BarrierAck
1216b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	 * or (on connection loss) in w_clear_epoch.  */
1217e42325a57606396539807ff55c24febda39f8d01Philipp Reisner	ok = _drbd_send_cmd(mdev, mdev->tconn->data.socket, P_BARRIER,
1218c012949a4084a9f91654121d28f199ef408cb9d7Philipp Reisner			    &p->head, sizeof(*p), 0);
1219611208706f28c502c8c01791ac4f0b14cde395b2Philipp Reisner	drbd_put_data_sock(mdev->tconn);
1220b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1221b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	return ok;
1222b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
1223b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
122400d56944ff086f895e9ad184a7785ca1eece4a3bPhilipp Reisnerint w_send_write_hint(struct drbd_work *w, int cancel)
1225b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
122600d56944ff086f895e9ad184a7785ca1eece4a3bPhilipp Reisner	struct drbd_conf *mdev = w->mdev;
1227b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (cancel)
1228b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		return 1;
1229b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	return drbd_send_short_cmd(mdev, P_UNPLUG_REMOTE);
1230b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
1231b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
123200d56944ff086f895e9ad184a7785ca1eece4a3bPhilipp Reisnerint w_send_oos(struct drbd_work *w, int cancel)
123373a01a18b9c28a0fab1131ece5b0a9bc00a879b8Philipp Reisner{
123473a01a18b9c28a0fab1131ece5b0a9bc00a879b8Philipp Reisner	struct drbd_request *req = container_of(w, struct drbd_request, w);
123500d56944ff086f895e9ad184a7785ca1eece4a3bPhilipp Reisner	struct drbd_conf *mdev = w->mdev;
123673a01a18b9c28a0fab1131ece5b0a9bc00a879b8Philipp Reisner	int ok;
123773a01a18b9c28a0fab1131ece5b0a9bc00a879b8Philipp Reisner
123873a01a18b9c28a0fab1131ece5b0a9bc00a879b8Philipp Reisner	if (unlikely(cancel)) {
12398554df1c6d3bb7686b39ed775772f507fa857c19Andreas Gruenbacher		req_mod(req, SEND_CANCELED);
124073a01a18b9c28a0fab1131ece5b0a9bc00a879b8Philipp Reisner		return 1;
124173a01a18b9c28a0fab1131ece5b0a9bc00a879b8Philipp Reisner	}
124273a01a18b9c28a0fab1131ece5b0a9bc00a879b8Philipp Reisner
124373a01a18b9c28a0fab1131ece5b0a9bc00a879b8Philipp Reisner	ok = drbd_send_oos(mdev, req);
12448554df1c6d3bb7686b39ed775772f507fa857c19Andreas Gruenbacher	req_mod(req, OOS_HANDED_TO_NETWORK);
124573a01a18b9c28a0fab1131ece5b0a9bc00a879b8Philipp Reisner
124673a01a18b9c28a0fab1131ece5b0a9bc00a879b8Philipp Reisner	return ok;
124773a01a18b9c28a0fab1131ece5b0a9bc00a879b8Philipp Reisner}
124873a01a18b9c28a0fab1131ece5b0a9bc00a879b8Philipp Reisner
1249b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner/**
1250b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * w_send_dblock() - Worker callback to send a P_DATA packet in order to mirror a write request
1251b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * @mdev:	DRBD device.
1252b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * @w:		work object.
1253b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * @cancel:	The connection will be closed anyways
1254b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner */
125500d56944ff086f895e9ad184a7785ca1eece4a3bPhilipp Reisnerint w_send_dblock(struct drbd_work *w, int cancel)
1256b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
1257b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	struct drbd_request *req = container_of(w, struct drbd_request, w);
125800d56944ff086f895e9ad184a7785ca1eece4a3bPhilipp Reisner	struct drbd_conf *mdev = w->mdev;
1259b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	int ok;
1260b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1261b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (unlikely(cancel)) {
12628554df1c6d3bb7686b39ed775772f507fa857c19Andreas Gruenbacher		req_mod(req, SEND_CANCELED);
1263b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		return 1;
1264b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
1265b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1266b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	ok = drbd_send_dblock(mdev, req);
12678554df1c6d3bb7686b39ed775772f507fa857c19Andreas Gruenbacher	req_mod(req, ok ? HANDED_OVER_TO_NETWORK : SEND_FAILED);
1268b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1269b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	return ok;
1270b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
1271b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1272b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner/**
1273b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * w_send_read_req() - Worker callback to send a read request (P_DATA_REQUEST) packet
1274b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * @mdev:	DRBD device.
1275b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * @w:		work object.
1276b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * @cancel:	The connection will be closed anyways
1277b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner */
127800d56944ff086f895e9ad184a7785ca1eece4a3bPhilipp Reisnerint w_send_read_req(struct drbd_work *w, int cancel)
1279b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
1280b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	struct drbd_request *req = container_of(w, struct drbd_request, w);
128100d56944ff086f895e9ad184a7785ca1eece4a3bPhilipp Reisner	struct drbd_conf *mdev = w->mdev;
1282b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	int ok;
1283b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1284b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (unlikely(cancel)) {
12858554df1c6d3bb7686b39ed775772f507fa857c19Andreas Gruenbacher		req_mod(req, SEND_CANCELED);
1286b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		return 1;
1287b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
1288b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1289ace652acf2d7e564dac48c615d9184e7ed575f9cAndreas Gruenbacher	ok = drbd_send_drequest(mdev, P_DATA_REQUEST, req->i.sector, req->i.size,
1290b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner				(unsigned long)req);
1291b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1292b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (!ok) {
1293b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		/* ?? we set C_TIMEOUT or C_BROKEN_PIPE in drbd_send();
1294b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		 * so this is probably redundant */
1295b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (mdev->state.conn >= C_CONNECTED)
1296b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			drbd_force_state(mdev, NS(conn, C_NETWORK_FAILURE));
1297b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
12988554df1c6d3bb7686b39ed775772f507fa857c19Andreas Gruenbacher	req_mod(req, ok ? HANDED_OVER_TO_NETWORK : SEND_FAILED);
1299b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1300b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	return ok;
1301b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
1302b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
130300d56944ff086f895e9ad184a7785ca1eece4a3bPhilipp Reisnerint w_restart_disk_io(struct drbd_work *w, int cancel)
1304265be2d09853d425ad14a61cda0ca63345613d0cPhilipp Reisner{
1305265be2d09853d425ad14a61cda0ca63345613d0cPhilipp Reisner	struct drbd_request *req = container_of(w, struct drbd_request, w);
130600d56944ff086f895e9ad184a7785ca1eece4a3bPhilipp Reisner	struct drbd_conf *mdev = w->mdev;
1307265be2d09853d425ad14a61cda0ca63345613d0cPhilipp Reisner
13080778286a133d2d3f81861a4e5db308e359583006Philipp Reisner	if (bio_data_dir(req->master_bio) == WRITE && req->rq_state & RQ_IN_ACT_LOG)
1309ace652acf2d7e564dac48c615d9184e7ed575f9cAndreas Gruenbacher		drbd_al_begin_io(mdev, req->i.sector);
1310265be2d09853d425ad14a61cda0ca63345613d0cPhilipp Reisner	/* Calling drbd_al_begin_io() out of the worker might deadlocks
1311265be2d09853d425ad14a61cda0ca63345613d0cPhilipp Reisner	   theoretically. Practically it can not deadlock, since this is
1312265be2d09853d425ad14a61cda0ca63345613d0cPhilipp Reisner	   only used when unfreezing IOs. All the extents of the requests
1313265be2d09853d425ad14a61cda0ca63345613d0cPhilipp Reisner	   that made it into the TL are already active */
1314265be2d09853d425ad14a61cda0ca63345613d0cPhilipp Reisner
1315265be2d09853d425ad14a61cda0ca63345613d0cPhilipp Reisner	drbd_req_make_private_bio(req, req->master_bio);
1316265be2d09853d425ad14a61cda0ca63345613d0cPhilipp Reisner	req->private_bio->bi_bdev = mdev->ldev->backing_bdev;
1317265be2d09853d425ad14a61cda0ca63345613d0cPhilipp Reisner	generic_make_request(req->private_bio);
1318265be2d09853d425ad14a61cda0ca63345613d0cPhilipp Reisner
1319265be2d09853d425ad14a61cda0ca63345613d0cPhilipp Reisner	return 1;
1320265be2d09853d425ad14a61cda0ca63345613d0cPhilipp Reisner}
1321265be2d09853d425ad14a61cda0ca63345613d0cPhilipp Reisner
1322b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisnerstatic int _drbd_may_sync_now(struct drbd_conf *mdev)
1323b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
1324b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	struct drbd_conf *odev = mdev;
1325b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1326b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	while (1) {
1327b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (odev->sync_conf.after == -1)
1328b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			return 1;
1329b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		odev = minor_to_mdev(odev->sync_conf.after);
1330841ce241fa355048f66172a47e356bb6e9159c9dAndreas Gruenbacher		if (!expect(odev))
1331841ce241fa355048f66172a47e356bb6e9159c9dAndreas Gruenbacher			return 1;
1332b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if ((odev->state.conn >= C_SYNC_SOURCE &&
1333b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		     odev->state.conn <= C_PAUSED_SYNC_T) ||
1334b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		    odev->state.aftr_isp || odev->state.peer_isp ||
1335b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		    odev->state.user_isp)
1336b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			return 0;
1337b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
1338b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
1339b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1340b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner/**
1341b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * _drbd_pause_after() - Pause resync on all devices that may not resync now
1342b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * @mdev:	DRBD device.
1343b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner *
1344b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * Called from process context only (admin command and after_state_ch).
1345b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner */
1346b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisnerstatic int _drbd_pause_after(struct drbd_conf *mdev)
1347b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
1348b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	struct drbd_conf *odev;
1349b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	int i, rv = 0;
1350b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1351b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	for (i = 0; i < minor_count; i++) {
1352b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		odev = minor_to_mdev(i);
1353b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (!odev)
1354b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			continue;
1355b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (odev->state.conn == C_STANDALONE && odev->state.disk == D_DISKLESS)
1356b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			continue;
1357b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (!_drbd_may_sync_now(odev))
1358b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			rv |= (__drbd_set_state(_NS(odev, aftr_isp, 1), CS_HARD, NULL)
1359b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			       != SS_NOTHING_TO_DO);
1360b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
1361b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1362b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	return rv;
1363b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
1364b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1365b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner/**
1366b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * _drbd_resume_next() - Resume resync on all devices that may resync now
1367b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * @mdev:	DRBD device.
1368b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner *
1369b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * Called from process context only (admin command and worker).
1370b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner */
1371b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisnerstatic int _drbd_resume_next(struct drbd_conf *mdev)
1372b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
1373b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	struct drbd_conf *odev;
1374b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	int i, rv = 0;
1375b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1376b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	for (i = 0; i < minor_count; i++) {
1377b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		odev = minor_to_mdev(i);
1378b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (!odev)
1379b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			continue;
1380b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (odev->state.conn == C_STANDALONE && odev->state.disk == D_DISKLESS)
1381b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			continue;
1382b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (odev->state.aftr_isp) {
1383b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			if (_drbd_may_sync_now(odev))
1384b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner				rv |= (__drbd_set_state(_NS(odev, aftr_isp, 0),
1385b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner							CS_HARD, NULL)
1386b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner				       != SS_NOTHING_TO_DO) ;
1387b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
1388b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
1389b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	return rv;
1390b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
1391b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1392b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisnervoid resume_next_sg(struct drbd_conf *mdev)
1393b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
1394b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	write_lock_irq(&global_state_lock);
1395b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	_drbd_resume_next(mdev);
1396b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	write_unlock_irq(&global_state_lock);
1397b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
1398b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1399b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisnervoid suspend_other_sg(struct drbd_conf *mdev)
1400b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
1401b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	write_lock_irq(&global_state_lock);
1402b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	_drbd_pause_after(mdev);
1403b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	write_unlock_irq(&global_state_lock);
1404b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
1405b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1406b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisnerstatic int sync_after_error(struct drbd_conf *mdev, int o_minor)
1407b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
1408b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	struct drbd_conf *odev;
1409b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1410b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (o_minor == -1)
1411b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		return NO_ERROR;
1412b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (o_minor < -1 || minor_to_mdev(o_minor) == NULL)
1413b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		return ERR_SYNC_AFTER;
1414b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1415b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	/* check for loops */
1416b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	odev = minor_to_mdev(o_minor);
1417b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	while (1) {
1418b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (odev == mdev)
1419b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			return ERR_SYNC_AFTER_CYCLE;
1420b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1421b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		/* dependency chain ends here, no cycles. */
1422b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (odev->sync_conf.after == -1)
1423b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			return NO_ERROR;
1424b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1425b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		/* follow the dependency chain */
1426b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		odev = minor_to_mdev(odev->sync_conf.after);
1427b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
1428b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
1429b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1430b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisnerint drbd_alter_sa(struct drbd_conf *mdev, int na)
1431b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
1432b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	int changes;
1433b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	int retcode;
1434b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1435b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	write_lock_irq(&global_state_lock);
1436b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	retcode = sync_after_error(mdev, na);
1437b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (retcode == NO_ERROR) {
1438b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		mdev->sync_conf.after = na;
1439b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		do {
1440b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			changes  = _drbd_pause_after(mdev);
1441b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			changes |= _drbd_resume_next(mdev);
1442b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		} while (changes);
1443b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
1444b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	write_unlock_irq(&global_state_lock);
1445b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	return retcode;
1446b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
1447b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
14489bd28d3c90c80c7ec46085de281b38f67331da41Lars Ellenbergvoid drbd_rs_controller_reset(struct drbd_conf *mdev)
14499bd28d3c90c80c7ec46085de281b38f67331da41Lars Ellenberg{
14509bd28d3c90c80c7ec46085de281b38f67331da41Lars Ellenberg	atomic_set(&mdev->rs_sect_in, 0);
14519bd28d3c90c80c7ec46085de281b38f67331da41Lars Ellenberg	atomic_set(&mdev->rs_sect_ev, 0);
14529bd28d3c90c80c7ec46085de281b38f67331da41Lars Ellenberg	mdev->rs_in_flight = 0;
14539bd28d3c90c80c7ec46085de281b38f67331da41Lars Ellenberg	mdev->rs_planed = 0;
14549bd28d3c90c80c7ec46085de281b38f67331da41Lars Ellenberg	spin_lock(&mdev->peer_seq_lock);
14559bd28d3c90c80c7ec46085de281b38f67331da41Lars Ellenberg	fifo_set(&mdev->rs_plan_s, 0);
14569bd28d3c90c80c7ec46085de281b38f67331da41Lars Ellenberg	spin_unlock(&mdev->peer_seq_lock);
14579bd28d3c90c80c7ec46085de281b38f67331da41Lars Ellenberg}
14589bd28d3c90c80c7ec46085de281b38f67331da41Lars Ellenberg
14591f04af33fe7db542d75a487b8381b5a3402b7896Philipp Reisnervoid start_resync_timer_fn(unsigned long data)
14601f04af33fe7db542d75a487b8381b5a3402b7896Philipp Reisner{
14611f04af33fe7db542d75a487b8381b5a3402b7896Philipp Reisner	struct drbd_conf *mdev = (struct drbd_conf *) data;
14621f04af33fe7db542d75a487b8381b5a3402b7896Philipp Reisner
14631f04af33fe7db542d75a487b8381b5a3402b7896Philipp Reisner	drbd_queue_work(&mdev->tconn->data.work, &mdev->start_resync_work);
14641f04af33fe7db542d75a487b8381b5a3402b7896Philipp Reisner}
14651f04af33fe7db542d75a487b8381b5a3402b7896Philipp Reisner
146600d56944ff086f895e9ad184a7785ca1eece4a3bPhilipp Reisnerint w_start_resync(struct drbd_work *w, int cancel)
14671f04af33fe7db542d75a487b8381b5a3402b7896Philipp Reisner{
146800d56944ff086f895e9ad184a7785ca1eece4a3bPhilipp Reisner	struct drbd_conf *mdev = w->mdev;
146900d56944ff086f895e9ad184a7785ca1eece4a3bPhilipp Reisner
14701f04af33fe7db542d75a487b8381b5a3402b7896Philipp Reisner	if (atomic_read(&mdev->unacked_cnt) || atomic_read(&mdev->rs_pending_cnt)) {
14711f04af33fe7db542d75a487b8381b5a3402b7896Philipp Reisner		dev_warn(DEV, "w_start_resync later...\n");
14721f04af33fe7db542d75a487b8381b5a3402b7896Philipp Reisner		mdev->start_resync_timer.expires = jiffies + HZ/10;
14731f04af33fe7db542d75a487b8381b5a3402b7896Philipp Reisner		add_timer(&mdev->start_resync_timer);
14741f04af33fe7db542d75a487b8381b5a3402b7896Philipp Reisner		return 1;
14751f04af33fe7db542d75a487b8381b5a3402b7896Philipp Reisner	}
14761f04af33fe7db542d75a487b8381b5a3402b7896Philipp Reisner
14771f04af33fe7db542d75a487b8381b5a3402b7896Philipp Reisner	drbd_start_resync(mdev, C_SYNC_SOURCE);
14781f04af33fe7db542d75a487b8381b5a3402b7896Philipp Reisner	clear_bit(AHEAD_TO_SYNC_SOURCE, &mdev->current_epoch->flags);
14791f04af33fe7db542d75a487b8381b5a3402b7896Philipp Reisner	return 1;
14801f04af33fe7db542d75a487b8381b5a3402b7896Philipp Reisner}
14811f04af33fe7db542d75a487b8381b5a3402b7896Philipp Reisner
1482b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner/**
1483b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * drbd_start_resync() - Start the resync process
1484b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * @mdev:	DRBD device.
1485b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * @side:	Either C_SYNC_SOURCE or C_SYNC_TARGET
1486b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner *
1487b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * This function might bring you directly into one of the
1488b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * C_PAUSED_SYNC_* states.
1489b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner */
1490b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisnervoid drbd_start_resync(struct drbd_conf *mdev, enum drbd_conns side)
1491b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
1492b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	union drbd_state ns;
1493b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	int r;
1494b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1495c4752ef1284519c3baa1c3b19df34a80b4905245Philipp Reisner	if (mdev->state.conn >= C_SYNC_SOURCE && mdev->state.conn < C_AHEAD) {
1496b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		dev_err(DEV, "Resync already running!\n");
1497b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		return;
1498b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
1499b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
150059817f4fab6a165ba83ce399464ba38432db8233Philipp Reisner	if (mdev->state.conn < C_AHEAD) {
150159817f4fab6a165ba83ce399464ba38432db8233Philipp Reisner		/* In case a previous resync run was aborted by an IO error/detach on the peer. */
150259817f4fab6a165ba83ce399464ba38432db8233Philipp Reisner		drbd_rs_cancel_all(mdev);
150359817f4fab6a165ba83ce399464ba38432db8233Philipp Reisner		/* This should be done when we abort the resync. We definitely do not
150459817f4fab6a165ba83ce399464ba38432db8233Philipp Reisner		   want to have this for connections going back and forth between
150559817f4fab6a165ba83ce399464ba38432db8233Philipp Reisner		   Ahead/Behind and SyncSource/SyncTarget */
150659817f4fab6a165ba83ce399464ba38432db8233Philipp Reisner	}
1507b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1508e64a32945902a178c9de9b38e0ea3290981605bcPhilipp Reisner	if (!test_bit(B_RS_H_DONE, &mdev->flags)) {
1509e64a32945902a178c9de9b38e0ea3290981605bcPhilipp Reisner		if (side == C_SYNC_TARGET) {
1510e64a32945902a178c9de9b38e0ea3290981605bcPhilipp Reisner			/* Since application IO was locked out during C_WF_BITMAP_T and
1511e64a32945902a178c9de9b38e0ea3290981605bcPhilipp Reisner			   C_WF_SYNC_UUID we are still unmodified. Before going to C_SYNC_TARGET
1512e64a32945902a178c9de9b38e0ea3290981605bcPhilipp Reisner			   we check that we might make the data inconsistent. */
1513e64a32945902a178c9de9b38e0ea3290981605bcPhilipp Reisner			r = drbd_khelper(mdev, "before-resync-target");
1514e64a32945902a178c9de9b38e0ea3290981605bcPhilipp Reisner			r = (r >> 8) & 0xff;
1515e64a32945902a178c9de9b38e0ea3290981605bcPhilipp Reisner			if (r > 0) {
1516e64a32945902a178c9de9b38e0ea3290981605bcPhilipp Reisner				dev_info(DEV, "before-resync-target handler returned %d, "
151709b9e7979378fe070784de20e50bb1d42aa643abPhilipp Reisner					 "dropping connection.\n", r);
151809b9e7979378fe070784de20e50bb1d42aa643abPhilipp Reisner				drbd_force_state(mdev, NS(conn, C_DISCONNECTING));
151909b9e7979378fe070784de20e50bb1d42aa643abPhilipp Reisner				return;
152009b9e7979378fe070784de20e50bb1d42aa643abPhilipp Reisner			}
1521e64a32945902a178c9de9b38e0ea3290981605bcPhilipp Reisner		} else /* C_SYNC_SOURCE */ {
1522e64a32945902a178c9de9b38e0ea3290981605bcPhilipp Reisner			r = drbd_khelper(mdev, "before-resync-source");
1523e64a32945902a178c9de9b38e0ea3290981605bcPhilipp Reisner			r = (r >> 8) & 0xff;
1524e64a32945902a178c9de9b38e0ea3290981605bcPhilipp Reisner			if (r > 0) {
1525e64a32945902a178c9de9b38e0ea3290981605bcPhilipp Reisner				if (r == 3) {
1526e64a32945902a178c9de9b38e0ea3290981605bcPhilipp Reisner					dev_info(DEV, "before-resync-source handler returned %d, "
1527e64a32945902a178c9de9b38e0ea3290981605bcPhilipp Reisner						 "ignoring. Old userland tools?", r);
1528e64a32945902a178c9de9b38e0ea3290981605bcPhilipp Reisner				} else {
1529e64a32945902a178c9de9b38e0ea3290981605bcPhilipp Reisner					dev_info(DEV, "before-resync-source handler returned %d, "
1530e64a32945902a178c9de9b38e0ea3290981605bcPhilipp Reisner						 "dropping connection.\n", r);
1531e64a32945902a178c9de9b38e0ea3290981605bcPhilipp Reisner					drbd_force_state(mdev, NS(conn, C_DISCONNECTING));
1532e64a32945902a178c9de9b38e0ea3290981605bcPhilipp Reisner					return;
1533e64a32945902a178c9de9b38e0ea3290981605bcPhilipp Reisner				}
1534e64a32945902a178c9de9b38e0ea3290981605bcPhilipp Reisner			}
153509b9e7979378fe070784de20e50bb1d42aa643abPhilipp Reisner		}
1536b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
1537b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1538e64a32945902a178c9de9b38e0ea3290981605bcPhilipp Reisner	if (current == mdev->tconn->worker.task) {
1539e64a32945902a178c9de9b38e0ea3290981605bcPhilipp Reisner		/* The worker should not sleep waiting for drbd_state_lock(),
1540e64a32945902a178c9de9b38e0ea3290981605bcPhilipp Reisner		   that can take long */
1541e64a32945902a178c9de9b38e0ea3290981605bcPhilipp Reisner		if (test_and_set_bit(CLUSTER_ST_CHANGE, &mdev->flags)) {
1542e64a32945902a178c9de9b38e0ea3290981605bcPhilipp Reisner			set_bit(B_RS_H_DONE, &mdev->flags);
1543e64a32945902a178c9de9b38e0ea3290981605bcPhilipp Reisner			mdev->start_resync_timer.expires = jiffies + HZ/5;
1544e64a32945902a178c9de9b38e0ea3290981605bcPhilipp Reisner			add_timer(&mdev->start_resync_timer);
1545e64a32945902a178c9de9b38e0ea3290981605bcPhilipp Reisner			return;
1546e64a32945902a178c9de9b38e0ea3290981605bcPhilipp Reisner		}
1547e64a32945902a178c9de9b38e0ea3290981605bcPhilipp Reisner	} else {
1548e64a32945902a178c9de9b38e0ea3290981605bcPhilipp Reisner		drbd_state_lock(mdev);
1549e64a32945902a178c9de9b38e0ea3290981605bcPhilipp Reisner	}
1550e64a32945902a178c9de9b38e0ea3290981605bcPhilipp Reisner	clear_bit(B_RS_H_DONE, &mdev->flags);
1551b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1552b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (!get_ldev_if_state(mdev, D_NEGOTIATING)) {
1553b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		drbd_state_unlock(mdev);
1554b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		return;
1555b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
1556b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1557b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	write_lock_irq(&global_state_lock);
1558b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	ns = mdev->state;
1559b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1560b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	ns.aftr_isp = !_drbd_may_sync_now(mdev);
1561b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1562b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	ns.conn = side;
1563b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1564b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (side == C_SYNC_TARGET)
1565b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		ns.disk = D_INCONSISTENT;
1566b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	else /* side == C_SYNC_SOURCE */
1567b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		ns.pdsk = D_INCONSISTENT;
1568b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1569b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	r = __drbd_set_state(mdev, ns, CS_VERBOSE, NULL);
1570b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	ns = mdev->state;
1571b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1572b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (ns.conn < C_CONNECTED)
1573b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		r = SS_UNKNOWN_ERROR;
1574b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1575b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (r == SS_SUCCESS) {
15761d7734a0df02ff5068ff8baa1447c7baee601db1Lars Ellenberg		unsigned long tw = drbd_bm_total_weight(mdev);
15771d7734a0df02ff5068ff8baa1447c7baee601db1Lars Ellenberg		unsigned long now = jiffies;
15781d7734a0df02ff5068ff8baa1447c7baee601db1Lars Ellenberg		int i;
15791d7734a0df02ff5068ff8baa1447c7baee601db1Lars Ellenberg
1580b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		mdev->rs_failed    = 0;
1581b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		mdev->rs_paused    = 0;
1582b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		mdev->rs_same_csum = 0;
15830f0601f4ea2f53cfd8bcae060fb03d9bbde070ecLars Ellenberg		mdev->rs_last_events = 0;
15840f0601f4ea2f53cfd8bcae060fb03d9bbde070ecLars Ellenberg		mdev->rs_last_sect_ev = 0;
15851d7734a0df02ff5068ff8baa1447c7baee601db1Lars Ellenberg		mdev->rs_total     = tw;
15861d7734a0df02ff5068ff8baa1447c7baee601db1Lars Ellenberg		mdev->rs_start     = now;
15871d7734a0df02ff5068ff8baa1447c7baee601db1Lars Ellenberg		for (i = 0; i < DRBD_SYNC_MARKS; i++) {
15881d7734a0df02ff5068ff8baa1447c7baee601db1Lars Ellenberg			mdev->rs_mark_left[i] = tw;
15891d7734a0df02ff5068ff8baa1447c7baee601db1Lars Ellenberg			mdev->rs_mark_time[i] = now;
15901d7734a0df02ff5068ff8baa1447c7baee601db1Lars Ellenberg		}
1591b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		_drbd_pause_after(mdev);
1592b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
1593b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	write_unlock_irq(&global_state_lock);
15945a22db8968a69bec835d1ed9a96ab3381719e0c0Lars Ellenberg
1595b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (r == SS_SUCCESS) {
1596b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		dev_info(DEV, "Began resync as %s (will sync %lu KB [%lu bits set]).\n",
1597b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		     drbd_conn_str(ns.conn),
1598b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		     (unsigned long) mdev->rs_total << (BM_BLOCK_SHIFT-10),
1599b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		     (unsigned long) mdev->rs_total);
16006c922ed543bee1bc6685ade07be59f3fa49a7288Lars Ellenberg		if (side == C_SYNC_TARGET)
16016c922ed543bee1bc6685ade07be59f3fa49a7288Lars Ellenberg			mdev->bm_resync_fo = 0;
16026c922ed543bee1bc6685ade07be59f3fa49a7288Lars Ellenberg
16036c922ed543bee1bc6685ade07be59f3fa49a7288Lars Ellenberg		/* Since protocol 96, we must serialize drbd_gen_and_send_sync_uuid
16046c922ed543bee1bc6685ade07be59f3fa49a7288Lars Ellenberg		 * with w_send_oos, or the sync target will get confused as to
16056c922ed543bee1bc6685ade07be59f3fa49a7288Lars Ellenberg		 * how much bits to resync.  We cannot do that always, because for an
16066c922ed543bee1bc6685ade07be59f3fa49a7288Lars Ellenberg		 * empty resync and protocol < 95, we need to do it here, as we call
16076c922ed543bee1bc6685ade07be59f3fa49a7288Lars Ellenberg		 * drbd_resync_finished from here in that case.
16086c922ed543bee1bc6685ade07be59f3fa49a7288Lars Ellenberg		 * We drbd_gen_and_send_sync_uuid here for protocol < 96,
16096c922ed543bee1bc6685ade07be59f3fa49a7288Lars Ellenberg		 * and from after_state_ch otherwise. */
161031890f4ab299c4116cf0a104ca9ce4f9ca2c5da0Philipp Reisner		if (side == C_SYNC_SOURCE && mdev->tconn->agreed_pro_version < 96)
16116c922ed543bee1bc6685ade07be59f3fa49a7288Lars Ellenberg			drbd_gen_and_send_sync_uuid(mdev);
1612b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
161331890f4ab299c4116cf0a104ca9ce4f9ca2c5da0Philipp Reisner		if (mdev->tconn->agreed_pro_version < 95 && mdev->rs_total == 0) {
1614af85e8e83d160f72a10e4467852646ac08614260Lars Ellenberg			/* This still has a race (about when exactly the peers
1615af85e8e83d160f72a10e4467852646ac08614260Lars Ellenberg			 * detect connection loss) that can lead to a full sync
1616af85e8e83d160f72a10e4467852646ac08614260Lars Ellenberg			 * on next handshake. In 8.3.9 we fixed this with explicit
1617af85e8e83d160f72a10e4467852646ac08614260Lars Ellenberg			 * resync-finished notifications, but the fix
1618af85e8e83d160f72a10e4467852646ac08614260Lars Ellenberg			 * introduces a protocol change.  Sleeping for some
1619af85e8e83d160f72a10e4467852646ac08614260Lars Ellenberg			 * time longer than the ping interval + timeout on the
1620af85e8e83d160f72a10e4467852646ac08614260Lars Ellenberg			 * SyncSource, to give the SyncTarget the chance to
1621af85e8e83d160f72a10e4467852646ac08614260Lars Ellenberg			 * detect connection loss, then waiting for a ping
1622af85e8e83d160f72a10e4467852646ac08614260Lars Ellenberg			 * response (implicit in drbd_resync_finished) reduces
1623af85e8e83d160f72a10e4467852646ac08614260Lars Ellenberg			 * the race considerably, but does not solve it. */
1624af85e8e83d160f72a10e4467852646ac08614260Lars Ellenberg			if (side == C_SYNC_SOURCE)
1625af85e8e83d160f72a10e4467852646ac08614260Lars Ellenberg				schedule_timeout_interruptible(
162689e58e755e37137135c28a90c93be1b28faff485Philipp Reisner					mdev->tconn->net_conf->ping_int * HZ +
162789e58e755e37137135c28a90c93be1b28faff485Philipp Reisner					mdev->tconn->net_conf->ping_timeo*HZ/9);
1628b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			drbd_resync_finished(mdev);
1629b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
1630b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
16319bd28d3c90c80c7ec46085de281b38f67331da41Lars Ellenberg		drbd_rs_controller_reset(mdev);
1632b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		/* ns.conn may already be != mdev->state.conn,
1633b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		 * we may have been paused in between, or become paused until
1634b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		 * the timer triggers.
1635b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		 * No matter, that is handled in resync_timer_fn() */
1636b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (ns.conn == C_SYNC_TARGET)
1637b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			mod_timer(&mdev->resync_timer, jiffies);
1638b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1639b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		drbd_md_sync(mdev);
1640b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
16415a22db8968a69bec835d1ed9a96ab3381719e0c0Lars Ellenberg	put_ldev(mdev);
1642d0c3f60f3611ceac9b1e4fdffd1497337568e7cbPhilipp Reisner	drbd_state_unlock(mdev);
1643b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
1644b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
164519393e105f9702a014d3ce08bce92b3ad9cf96b5Philipp Reisnerstatic int _worker_dying(int vnr, void *p, void *data)
164619393e105f9702a014d3ce08bce92b3ad9cf96b5Philipp Reisner{
164719393e105f9702a014d3ce08bce92b3ad9cf96b5Philipp Reisner	struct drbd_conf *mdev = (struct drbd_conf *)p;
164819393e105f9702a014d3ce08bce92b3ad9cf96b5Philipp Reisner
164919393e105f9702a014d3ce08bce92b3ad9cf96b5Philipp Reisner	D_ASSERT(mdev->state.disk == D_DISKLESS && mdev->state.conn == C_STANDALONE);
165019393e105f9702a014d3ce08bce92b3ad9cf96b5Philipp Reisner	/* _drbd_set_state only uses stop_nowait.
165119393e105f9702a014d3ce08bce92b3ad9cf96b5Philipp Reisner	 * wait here for the exiting receiver. */
165219393e105f9702a014d3ce08bce92b3ad9cf96b5Philipp Reisner	drbd_thread_stop(&mdev->tconn->receiver);
165319393e105f9702a014d3ce08bce92b3ad9cf96b5Philipp Reisner	drbd_mdev_cleanup(mdev);
165419393e105f9702a014d3ce08bce92b3ad9cf96b5Philipp Reisner
165519393e105f9702a014d3ce08bce92b3ad9cf96b5Philipp Reisner	clear_bit(DEVICE_DYING, &mdev->flags);
165619393e105f9702a014d3ce08bce92b3ad9cf96b5Philipp Reisner	clear_bit(CONFIG_PENDING, &mdev->flags);
165719393e105f9702a014d3ce08bce92b3ad9cf96b5Philipp Reisner	wake_up(&mdev->state_wait);
165819393e105f9702a014d3ce08bce92b3ad9cf96b5Philipp Reisner
165919393e105f9702a014d3ce08bce92b3ad9cf96b5Philipp Reisner	return 0;
166019393e105f9702a014d3ce08bce92b3ad9cf96b5Philipp Reisner}
166119393e105f9702a014d3ce08bce92b3ad9cf96b5Philipp Reisner
1662b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisnerint drbd_worker(struct drbd_thread *thi)
1663b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
1664392c8801922f51466045ece2f1f2884b8c9cd9a2Philipp Reisner	struct drbd_tconn *tconn = thi->tconn;
1665b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	struct drbd_work *w = NULL;
1666b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	LIST_HEAD(work_list);
166719393e105f9702a014d3ce08bce92b3ad9cf96b5Philipp Reisner	int intr = 0;
1668b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1669e77a0a5cc1e6961f485b5623ef42f3b910969675Andreas Gruenbacher	while (get_t_state(thi) == RUNNING) {
1670808222845d62e551630699a1381bbf8a1fd4a286Philipp Reisner		drbd_thread_current_set_cpu(thi);
1671b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
167219393e105f9702a014d3ce08bce92b3ad9cf96b5Philipp Reisner		if (down_trylock(&tconn->data.work.s)) {
167319393e105f9702a014d3ce08bce92b3ad9cf96b5Philipp Reisner			mutex_lock(&tconn->data.mutex);
167419393e105f9702a014d3ce08bce92b3ad9cf96b5Philipp Reisner			if (tconn->data.socket && !tconn->net_conf->no_cork)
167519393e105f9702a014d3ce08bce92b3ad9cf96b5Philipp Reisner				drbd_tcp_uncork(tconn->data.socket);
167619393e105f9702a014d3ce08bce92b3ad9cf96b5Philipp Reisner			mutex_unlock(&tconn->data.mutex);
1677b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
167819393e105f9702a014d3ce08bce92b3ad9cf96b5Philipp Reisner			intr = down_interruptible(&tconn->data.work.s);
1679b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
168019393e105f9702a014d3ce08bce92b3ad9cf96b5Philipp Reisner			mutex_lock(&tconn->data.mutex);
168119393e105f9702a014d3ce08bce92b3ad9cf96b5Philipp Reisner			if (tconn->data.socket  && !tconn->net_conf->no_cork)
168219393e105f9702a014d3ce08bce92b3ad9cf96b5Philipp Reisner				drbd_tcp_cork(tconn->data.socket);
168319393e105f9702a014d3ce08bce92b3ad9cf96b5Philipp Reisner			mutex_unlock(&tconn->data.mutex);
1684b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
1685b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1686b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (intr) {
1687b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			flush_signals(current);
168819393e105f9702a014d3ce08bce92b3ad9cf96b5Philipp Reisner			if (get_t_state(thi) == RUNNING) {
168919393e105f9702a014d3ce08bce92b3ad9cf96b5Philipp Reisner				conn_warn(tconn, "Worker got an unexpected signal\n");
1690b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner				continue;
169119393e105f9702a014d3ce08bce92b3ad9cf96b5Philipp Reisner			}
1692b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			break;
1693b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
1694b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1695e77a0a5cc1e6961f485b5623ef42f3b910969675Andreas Gruenbacher		if (get_t_state(thi) != RUNNING)
1696b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			break;
1697b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		/* With this break, we have done a down() but not consumed
1698b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		   the entry from the list. The cleanup code takes care of
1699b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		   this...   */
1700b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1701b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		w = NULL;
170219393e105f9702a014d3ce08bce92b3ad9cf96b5Philipp Reisner		spin_lock_irq(&tconn->data.work.q_lock);
170319393e105f9702a014d3ce08bce92b3ad9cf96b5Philipp Reisner		if (list_empty(&tconn->data.work.q)) {
1704b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			/* something terribly wrong in our logic.
1705b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			 * we were able to down() the semaphore,
1706b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			 * but the list is empty... doh.
1707b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			 *
1708b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			 * what is the best thing to do now?
1709b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			 * try again from scratch, restarting the receiver,
1710b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			 * asender, whatnot? could break even more ugly,
1711b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			 * e.g. when we are primary, but no good local data.
1712b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			 *
1713b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			 * I'll try to get away just starting over this loop.
1714b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			 */
171519393e105f9702a014d3ce08bce92b3ad9cf96b5Philipp Reisner			conn_warn(tconn, "Work list unexpectedly empty\n");
171619393e105f9702a014d3ce08bce92b3ad9cf96b5Philipp Reisner			spin_unlock_irq(&tconn->data.work.q_lock);
1717b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			continue;
1718b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
171919393e105f9702a014d3ce08bce92b3ad9cf96b5Philipp Reisner		w = list_entry(tconn->data.work.q.next, struct drbd_work, list);
1720b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		list_del_init(&w->list);
172119393e105f9702a014d3ce08bce92b3ad9cf96b5Philipp Reisner		spin_unlock_irq(&tconn->data.work.q_lock);
1722b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1723bbeb641c3e4982d6bba21188545a7fd44ab0a715Philipp Reisner		if (!w->cb(w, tconn->cstate < C_WF_REPORT_PARAMS)) {
1724b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			/* dev_warn(DEV, "worker: a callback failed! \n"); */
1725bbeb641c3e4982d6bba21188545a7fd44ab0a715Philipp Reisner			if (tconn->cstate >= C_WF_REPORT_PARAMS)
1726bbeb641c3e4982d6bba21188545a7fd44ab0a715Philipp Reisner				conn_request_state(tconn, NS(conn, C_NETWORK_FAILURE), CS_HARD);
1727b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
1728b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
1729b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
173019393e105f9702a014d3ce08bce92b3ad9cf96b5Philipp Reisner	spin_lock_irq(&tconn->data.work.q_lock);
173119393e105f9702a014d3ce08bce92b3ad9cf96b5Philipp Reisner	while (!list_empty(&tconn->data.work.q)) {
173219393e105f9702a014d3ce08bce92b3ad9cf96b5Philipp Reisner		list_splice_init(&tconn->data.work.q, &work_list);
173319393e105f9702a014d3ce08bce92b3ad9cf96b5Philipp Reisner		spin_unlock_irq(&tconn->data.work.q_lock);
1734b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1735b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		while (!list_empty(&work_list)) {
1736b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			w = list_entry(work_list.next, struct drbd_work, list);
1737b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			list_del_init(&w->list);
173800d56944ff086f895e9ad184a7785ca1eece4a3bPhilipp Reisner			w->cb(w, 1);
1739b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
1740b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
174119393e105f9702a014d3ce08bce92b3ad9cf96b5Philipp Reisner		spin_lock_irq(&tconn->data.work.q_lock);
1742b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
174319393e105f9702a014d3ce08bce92b3ad9cf96b5Philipp Reisner	sema_init(&tconn->data.work.s, 0);
1744b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	/* DANGEROUS race: if someone did queue his work within the spinlock,
1745b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	 * but up() ed outside the spinlock, we could get an up() on the
1746b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	 * semaphore without corresponding list entry.
1747b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	 * So don't do that.
1748b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	 */
174919393e105f9702a014d3ce08bce92b3ad9cf96b5Philipp Reisner	spin_unlock_irq(&tconn->data.work.q_lock);
1750b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
175119393e105f9702a014d3ce08bce92b3ad9cf96b5Philipp Reisner	idr_for_each(&tconn->volumes, _worker_dying, NULL);
1752b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1753b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	return 0;
1754b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
1755