drbd_worker.c revision 87eeee41f8740451b61a1e7d37a494333a906861
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
41b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisnerstatic int w_make_ov_request(struct drbd_conf *mdev, struct drbd_work *w, int cancel);
429d77a5fee9d2a1ea4cd9a841d27b107df5913b33Philipp Reisnerstatic int w_make_resync_request(struct drbd_conf *mdev,
439d77a5fee9d2a1ea4cd9a841d27b107df5913b33Philipp Reisner				 struct drbd_work *w, int cancel);
44b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
45b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
46b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
47c5a91619793d444e5103ec5841045bf878718398Andreas Gruenbacher/* endio handlers:
48c5a91619793d444e5103ec5841045bf878718398Andreas Gruenbacher *   drbd_md_io_complete (defined here)
49c5a91619793d444e5103ec5841045bf878718398Andreas Gruenbacher *   drbd_endio_pri (defined here)
50c5a91619793d444e5103ec5841045bf878718398Andreas Gruenbacher *   drbd_endio_sec (defined here)
51c5a91619793d444e5103ec5841045bf878718398Andreas Gruenbacher *   bm_async_io_complete (defined in drbd_bitmap.c)
52c5a91619793d444e5103ec5841045bf878718398Andreas Gruenbacher *
53b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * For all these callbacks, note the following:
54b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * The callbacks will be called in irq context by the IDE drivers,
55b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * and in Softirqs/Tasklets/BH context by the SCSI drivers.
56b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * Try to get the locking right :)
57b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner *
58b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner */
59b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
60b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
61b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner/* About the global_state_lock
62b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner   Each state transition on an device holds a read lock. In case we have
63b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner   to evaluate the sync after dependencies, we grab a write lock, because
64b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner   we need stable states on all devices for that.  */
65b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisnerrwlock_t global_state_lock;
66b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
67b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner/* used for synchronous meta data and bitmap IO
68b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * submitted by drbd_md_sync_page_io()
69b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner */
70b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisnervoid drbd_md_io_complete(struct bio *bio, int error)
71b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
72b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	struct drbd_md_io *md_io;
73b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
74b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	md_io = (struct drbd_md_io *)bio->bi_private;
75b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	md_io->error = error;
76b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
77b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	complete(&md_io->event);
78b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
79b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
80b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner/* reads on behalf of the partner,
81b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * "submitted" by the receiver
82b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner */
8345bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenbergvoid drbd_endio_read_sec_final(struct drbd_epoch_entry *e) __releases(local)
84b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
85b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	unsigned long flags = 0;
8645bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	struct drbd_conf *mdev = e->mdev;
87b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
8887eeee41f8740451b61a1e7d37a494333a906861Philipp Reisner	spin_lock_irqsave(&mdev->tconn->req_lock, flags);
89010f6e678ffddbf3134863038c5b2f6509f1eed3Andreas Gruenbacher	mdev->read_cnt += e->i.size >> 9;
90b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	list_del(&e->w.list);
91b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (list_empty(&mdev->read_ee))
92b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		wake_up(&mdev->ee_wait);
9345bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	if (test_bit(__EE_WAS_ERROR, &e->flags))
9481e84650c200de0695372461964dd960365696dbAndreas Gruenbacher		__drbd_chk_io_error(mdev, false);
9587eeee41f8740451b61a1e7d37a494333a906861Philipp Reisner	spin_unlock_irqrestore(&mdev->tconn->req_lock, flags);
96b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
97e42325a57606396539807ff55c24febda39f8d01Philipp Reisner	drbd_queue_work(&mdev->tconn->data.work, &e->w);
98b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	put_ldev(mdev);
99b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
100b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
101b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner/* writes on behalf of the partner, or resync writes,
10245bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg * "submitted" by the receiver, final stage.  */
10345bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenbergstatic void drbd_endio_write_sec_final(struct drbd_epoch_entry *e) __releases(local)
104b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
105b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	unsigned long flags = 0;
10645bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	struct drbd_conf *mdev = e->mdev;
107b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	sector_t e_sector;
108b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	int do_wake;
109579b57ed730819970a3542b4bbcc2d4176f25c72Andreas Gruenbacher	u64 block_id;
110b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	int do_al_complete_io;
111b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
112b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	/* after we moved e to done_ee,
113b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	 * we may no longer access it,
114b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	 * it may be freed/reused already!
115b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	 * (as soon as we release the req_lock) */
116010f6e678ffddbf3134863038c5b2f6509f1eed3Andreas Gruenbacher	e_sector = e->i.sector;
117b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	do_al_complete_io = e->flags & EE_CALL_AL_COMPLETE_IO;
118579b57ed730819970a3542b4bbcc2d4176f25c72Andreas Gruenbacher	block_id = e->block_id;
119b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
12087eeee41f8740451b61a1e7d37a494333a906861Philipp Reisner	spin_lock_irqsave(&mdev->tconn->req_lock, flags);
121010f6e678ffddbf3134863038c5b2f6509f1eed3Andreas Gruenbacher	mdev->writ_cnt += e->i.size >> 9;
122b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	list_del(&e->w.list); /* has been on active_ee or sync_ee */
123b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	list_add_tail(&e->w.list, &mdev->done_ee);
124b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
125bb3bfe96144a4535d47ccfea444bc1ef8e02f4e3Andreas Gruenbacher	/*
126bb3bfe96144a4535d47ccfea444bc1ef8e02f4e3Andreas Gruenbacher	 * Do not remove from the epoch_entries tree here: we did not send the
127bb3bfe96144a4535d47ccfea444bc1ef8e02f4e3Andreas Gruenbacher	 * Ack yet and did not wake possibly waiting conflicting requests.
128bb3bfe96144a4535d47ccfea444bc1ef8e02f4e3Andreas Gruenbacher	 * Removed from the tree from "drbd_process_done_ee" within the
129bb3bfe96144a4535d47ccfea444bc1ef8e02f4e3Andreas Gruenbacher	 * appropriate w.cb (e_end_block/e_end_resync_block) or from
130bb3bfe96144a4535d47ccfea444bc1ef8e02f4e3Andreas Gruenbacher	 * _drbd_clear_done_ee.
131bb3bfe96144a4535d47ccfea444bc1ef8e02f4e3Andreas Gruenbacher	 */
132b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
133579b57ed730819970a3542b4bbcc2d4176f25c72Andreas Gruenbacher	do_wake = list_empty(block_id == ID_SYNCER ? &mdev->sync_ee : &mdev->active_ee);
134b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
13545bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	if (test_bit(__EE_WAS_ERROR, &e->flags))
13681e84650c200de0695372461964dd960365696dbAndreas Gruenbacher		__drbd_chk_io_error(mdev, false);
13787eeee41f8740451b61a1e7d37a494333a906861Philipp Reisner	spin_unlock_irqrestore(&mdev->tconn->req_lock, flags);
138b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
139579b57ed730819970a3542b4bbcc2d4176f25c72Andreas Gruenbacher	if (block_id == ID_SYNCER)
140b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		drbd_rs_complete_io(mdev, e_sector);
141b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
142b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (do_wake)
143b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		wake_up(&mdev->ee_wait);
144b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
145b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (do_al_complete_io)
146b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		drbd_al_complete_io(mdev, e_sector);
147b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
148b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	wake_asender(mdev);
149b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	put_ldev(mdev);
15045bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg}
151b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
15245bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg/* writes on behalf of the partner, or resync writes,
15345bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg * "submitted" by the receiver.
15445bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg */
15545bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenbergvoid drbd_endio_sec(struct bio *bio, int error)
15645bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg{
15745bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	struct drbd_epoch_entry *e = bio->bi_private;
15845bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	struct drbd_conf *mdev = e->mdev;
15945bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	int uptodate = bio_flagged(bio, BIO_UPTODATE);
16045bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	int is_write = bio_data_dir(bio) == WRITE;
16145bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg
162071942727824bab03b1a3f6b6eeb5b269697b333Lars Ellenberg	if (error && __ratelimit(&drbd_ratelimit_state))
16345bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg		dev_warn(DEV, "%s: error=%d s=%llus\n",
16445bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg				is_write ? "write" : "read", error,
165010f6e678ffddbf3134863038c5b2f6509f1eed3Andreas Gruenbacher				(unsigned long long)e->i.sector);
16645bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	if (!error && !uptodate) {
167071942727824bab03b1a3f6b6eeb5b269697b333Lars Ellenberg		if (__ratelimit(&drbd_ratelimit_state))
168071942727824bab03b1a3f6b6eeb5b269697b333Lars Ellenberg			dev_warn(DEV, "%s: setting error to -EIO s=%llus\n",
169071942727824bab03b1a3f6b6eeb5b269697b333Lars Ellenberg					is_write ? "write" : "read",
170010f6e678ffddbf3134863038c5b2f6509f1eed3Andreas Gruenbacher					(unsigned long long)e->i.sector);
17145bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg		/* strange behavior of some lower level drivers...
17245bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg		 * fail the request by clearing the uptodate flag,
17345bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg		 * but do not return any error?! */
17445bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg		error = -EIO;
17545bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	}
17645bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg
17745bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	if (error)
17845bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg		set_bit(__EE_WAS_ERROR, &e->flags);
17945bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg
18045bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	bio_put(bio); /* no need for the bio anymore */
18145bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	if (atomic_dec_and_test(&e->pending_bios)) {
18245bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg		if (is_write)
18345bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg			drbd_endio_write_sec_final(e);
18445bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg		else
18545bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg			drbd_endio_read_sec_final(e);
18645bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	}
187b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
188b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
189b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner/* read, readA or write requests on R_PRIMARY coming from drbd_make_request
190b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner */
191b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisnervoid drbd_endio_pri(struct bio *bio, int error)
192b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
193a115413de13ae6beb0cbfc198afe385a261ab284Lars Ellenberg	unsigned long flags;
194b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	struct drbd_request *req = bio->bi_private;
195b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	struct drbd_conf *mdev = req->mdev;
196a115413de13ae6beb0cbfc198afe385a261ab284Lars Ellenberg	struct bio_and_error m;
197b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	enum drbd_req_event what;
198b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	int uptodate = bio_flagged(bio, BIO_UPTODATE);
199b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
200b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (!error && !uptodate) {
201b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		dev_warn(DEV, "p %s: setting error to -EIO\n",
202b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			 bio_data_dir(bio) == WRITE ? "write" : "read");
203b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		/* strange behavior of some lower level drivers...
204b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		 * fail the request by clearing the uptodate flag,
205b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		 * but do not return any error?! */
206b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		error = -EIO;
207b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
208b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
209b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	/* to avoid recursion in __req_mod */
210b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (unlikely(error)) {
211b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		what = (bio_data_dir(bio) == WRITE)
2128554df1c6d3bb7686b39ed775772f507fa857c19Andreas Gruenbacher			? WRITE_COMPLETED_WITH_ERROR
2135c3c7e64bbcb60db39d0d55c8fab39ef41d41c30Lars Ellenberg			: (bio_rw(bio) == READ)
2148554df1c6d3bb7686b39ed775772f507fa857c19Andreas Gruenbacher			  ? READ_COMPLETED_WITH_ERROR
2158554df1c6d3bb7686b39ed775772f507fa857c19Andreas Gruenbacher			  : READ_AHEAD_COMPLETED_WITH_ERROR;
216b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	} else
2178554df1c6d3bb7686b39ed775772f507fa857c19Andreas Gruenbacher		what = COMPLETED_OK;
218b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
219b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	bio_put(req->private_bio);
220b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	req->private_bio = ERR_PTR(error);
221b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
222a115413de13ae6beb0cbfc198afe385a261ab284Lars Ellenberg	/* not req_mod(), we need irqsave here! */
22387eeee41f8740451b61a1e7d37a494333a906861Philipp Reisner	spin_lock_irqsave(&mdev->tconn->req_lock, flags);
224a115413de13ae6beb0cbfc198afe385a261ab284Lars Ellenberg	__req_mod(req, what, &m);
22587eeee41f8740451b61a1e7d37a494333a906861Philipp Reisner	spin_unlock_irqrestore(&mdev->tconn->req_lock, flags);
226a115413de13ae6beb0cbfc198afe385a261ab284Lars Ellenberg
227a115413de13ae6beb0cbfc198afe385a261ab284Lars Ellenberg	if (m.bio)
228a115413de13ae6beb0cbfc198afe385a261ab284Lars Ellenberg		complete_master_bio(mdev, &m);
229b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
230b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
231b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisnerint w_read_retry_remote(struct drbd_conf *mdev, struct drbd_work *w, int cancel)
232b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
233b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	struct drbd_request *req = container_of(w, struct drbd_request, w);
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
247b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	return w_send_read_req(mdev, w, 0);
248b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
249b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
25045bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenbergvoid drbd_csum_ee(struct drbd_conf *mdev, struct crypto_hash *tfm, struct drbd_epoch_entry *e, void *digest)
25145bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg{
25245bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	struct hash_desc desc;
25345bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	struct scatterlist sg;
25445bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	struct page *page = e->pages;
25545bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	struct page *tmp;
25645bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	unsigned len;
25745bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg
25845bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	desc.tfm = tfm;
25945bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	desc.flags = 0;
26045bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg
26145bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	sg_init_table(&sg, 1);
26245bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	crypto_hash_init(&desc);
26345bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg
26445bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	while ((tmp = page_chain_next(page))) {
26545bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg		/* all but the last page will be fully used */
26645bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg		sg_set_page(&sg, page, PAGE_SIZE, 0);
26745bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg		crypto_hash_update(&desc, &sg, sg.length);
26845bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg		page = tmp;
26945bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	}
27045bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	/* and now the last, possibly only partially used page */
271010f6e678ffddbf3134863038c5b2f6509f1eed3Andreas Gruenbacher	len = e->i.size & (PAGE_SIZE - 1);
27245bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	sg_set_page(&sg, page, len ?: PAGE_SIZE, 0);
27345bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	crypto_hash_update(&desc, &sg, sg.length);
27445bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	crypto_hash_final(&desc, digest);
27545bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg}
27645bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg
27745bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenbergvoid drbd_csum_bio(struct drbd_conf *mdev, struct crypto_hash *tfm, struct bio *bio, void *digest)
278b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
279b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	struct hash_desc desc;
280b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	struct scatterlist sg;
281b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	struct bio_vec *bvec;
282b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	int i;
283b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
284b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	desc.tfm = tfm;
285b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	desc.flags = 0;
286b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
287b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	sg_init_table(&sg, 1);
288b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	crypto_hash_init(&desc);
289b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
290b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	__bio_for_each_segment(bvec, bio, i, 0) {
291b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		sg_set_page(&sg, bvec->bv_page, bvec->bv_len, bvec->bv_offset);
292b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		crypto_hash_update(&desc, &sg, sg.length);
293b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
294b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	crypto_hash_final(&desc, digest);
295b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
296b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
29753ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenberg/* TODO merge common code with w_e_end_ov_req */
29853ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenbergint w_e_send_csum(struct drbd_conf *mdev, struct drbd_work *w, int cancel)
299b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
300b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	struct drbd_epoch_entry *e = container_of(w, struct drbd_epoch_entry, w);
301b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	int digest_size;
302b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	void *digest;
30353ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenberg	int ok = 1;
304b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
30553ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenberg	if (unlikely(cancel))
30653ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenberg		goto out;
307b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
30853ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenberg	if (likely((e->flags & EE_WAS_ERROR) != 0))
30953ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenberg		goto out;
310b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
31153ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenberg	digest_size = crypto_hash_digestsize(mdev->csums_tfm);
31253ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenberg	digest = kmalloc(digest_size, GFP_NOIO);
31353ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenberg	if (digest) {
314010f6e678ffddbf3134863038c5b2f6509f1eed3Andreas Gruenbacher		sector_t sector = e->i.sector;
315010f6e678ffddbf3134863038c5b2f6509f1eed3Andreas Gruenbacher		unsigned int size = e->i.size;
31653ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenberg		drbd_csum_ee(mdev, mdev->csums_tfm, e, digest);
31753ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenberg		/* Free e and pages before send.
31853ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenberg		 * In case we block on congestion, we could otherwise run into
31953ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenberg		 * some distributed deadlock, if the other side blocks on
32053ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenberg		 * congestion as well, because our receiver blocks in
32153ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenberg		 * drbd_pp_alloc due to pp_in_use > max_buffers. */
32253ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenberg		drbd_free_ee(mdev, e);
32353ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenberg		e = NULL;
32453ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenberg		inc_rs_pending(mdev);
32553ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenberg		ok = drbd_send_drequest_csum(mdev, sector, size,
32653ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenberg					     digest, digest_size,
32753ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenberg					     P_CSUM_RS_REQUEST);
32853ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenberg		kfree(digest);
32953ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenberg	} else {
33053ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenberg		dev_err(DEV, "kmalloc() of digest failed.\n");
33153ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenberg		ok = 0;
33253ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenberg	}
333b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
33453ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenbergout:
33553ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenberg	if (e)
33653ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenberg		drbd_free_ee(mdev, e);
337b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
338b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (unlikely(!ok))
339b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		dev_err(DEV, "drbd_send_drequest(..., csum) failed\n");
340b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	return ok;
341b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
342b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
343b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner#define GFP_TRY	(__GFP_HIGHMEM | __GFP_NOWARN)
344b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
345b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisnerstatic int read_for_csum(struct drbd_conf *mdev, sector_t sector, int size)
346b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
347b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	struct drbd_epoch_entry *e;
348b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
349b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (!get_ldev(mdev))
35080a40e439e5a3f30b0a6210a1add6d7c33392e54Lars Ellenberg		return -EIO;
351b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
352e3555d8545976703938d1b59e2db509426dbe02cPhilipp Reisner	if (drbd_rs_should_slow_down(mdev, sector))
3530f0601f4ea2f53cfd8bcae060fb03d9bbde070ecLars Ellenberg		goto defer;
3540f0601f4ea2f53cfd8bcae060fb03d9bbde070ecLars Ellenberg
355b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	/* GFP_TRY, because if there is no memory available right now, this may
356b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	 * be rescheduled for later. It is "only" background resync, after all. */
3579a8e77530fa7059044114bcf1a897a470ec21bc9Andreas Gruenbacher	e = drbd_alloc_ee(mdev, ID_SYNCER /* unused */, sector, size, GFP_TRY);
35845bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	if (!e)
35980a40e439e5a3f30b0a6210a1add6d7c33392e54Lars Ellenberg		goto defer;
360b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
36180a40e439e5a3f30b0a6210a1add6d7c33392e54Lars Ellenberg	e->w.cb = w_e_send_csum;
36287eeee41f8740451b61a1e7d37a494333a906861Philipp Reisner	spin_lock_irq(&mdev->tconn->req_lock);
363b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	list_add(&e->w.list, &mdev->read_ee);
36487eeee41f8740451b61a1e7d37a494333a906861Philipp Reisner	spin_unlock_irq(&mdev->tconn->req_lock);
365b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
3660f0601f4ea2f53cfd8bcae060fb03d9bbde070ecLars Ellenberg	atomic_add(size >> 9, &mdev->rs_sect_ev);
36745bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	if (drbd_submit_ee(mdev, e, READ, DRBD_FAULT_RS_RD) == 0)
36880a40e439e5a3f30b0a6210a1add6d7c33392e54Lars Ellenberg		return 0;
369b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
37010f6d9926cd17afff9dc03c967706419798b4929Lars Ellenberg	/* If it failed because of ENOMEM, retry should help.  If it failed
37110f6d9926cd17afff9dc03c967706419798b4929Lars Ellenberg	 * because bio_add_page failed (probably broken lower level driver),
37210f6d9926cd17afff9dc03c967706419798b4929Lars Ellenberg	 * retry may or may not help.
37310f6d9926cd17afff9dc03c967706419798b4929Lars Ellenberg	 * If it does not, you may need to force disconnect. */
37487eeee41f8740451b61a1e7d37a494333a906861Philipp Reisner	spin_lock_irq(&mdev->tconn->req_lock);
37522cc37a943832c948808884604ec6f5ff2594c1dLars Ellenberg	list_del(&e->w.list);
37687eeee41f8740451b61a1e7d37a494333a906861Philipp Reisner	spin_unlock_irq(&mdev->tconn->req_lock);
37722cc37a943832c948808884604ec6f5ff2594c1dLars Ellenberg
37845bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	drbd_free_ee(mdev, e);
37980a40e439e5a3f30b0a6210a1add6d7c33392e54Lars Ellenbergdefer:
38045bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	put_ldev(mdev);
38180a40e439e5a3f30b0a6210a1add6d7c33392e54Lars Ellenberg	return -EAGAIN;
382b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
383b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
384794abb753e29e85949b3719dbc2ab6a98711a47ePhilipp Reisnerint w_resync_timer(struct drbd_conf *mdev, struct drbd_work *w, int cancel)
385b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
38663106d3c6c769b6219bd04edde513b12abae3f61Philipp Reisner	switch (mdev->state.conn) {
38763106d3c6c769b6219bd04edde513b12abae3f61Philipp Reisner	case C_VERIFY_S:
388794abb753e29e85949b3719dbc2ab6a98711a47ePhilipp Reisner		w_make_ov_request(mdev, w, cancel);
38963106d3c6c769b6219bd04edde513b12abae3f61Philipp Reisner		break;
39063106d3c6c769b6219bd04edde513b12abae3f61Philipp Reisner	case C_SYNC_TARGET:
391794abb753e29e85949b3719dbc2ab6a98711a47ePhilipp Reisner		w_make_resync_request(mdev, w, cancel);
39263106d3c6c769b6219bd04edde513b12abae3f61Philipp Reisner		break;
393b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
394b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
395794abb753e29e85949b3719dbc2ab6a98711a47ePhilipp Reisner	return 1;
396794abb753e29e85949b3719dbc2ab6a98711a47ePhilipp Reisner}
397794abb753e29e85949b3719dbc2ab6a98711a47ePhilipp Reisner
398794abb753e29e85949b3719dbc2ab6a98711a47ePhilipp Reisnervoid resync_timer_fn(unsigned long data)
399794abb753e29e85949b3719dbc2ab6a98711a47ePhilipp Reisner{
400794abb753e29e85949b3719dbc2ab6a98711a47ePhilipp Reisner	struct drbd_conf *mdev = (struct drbd_conf *) data;
401794abb753e29e85949b3719dbc2ab6a98711a47ePhilipp Reisner
402794abb753e29e85949b3719dbc2ab6a98711a47ePhilipp Reisner	if (list_empty(&mdev->resync_work.list))
403e42325a57606396539807ff55c24febda39f8d01Philipp Reisner		drbd_queue_work(&mdev->tconn->data.work, &mdev->resync_work);
404b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
405b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
406778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisnerstatic void fifo_set(struct fifo_buffer *fb, int value)
407778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner{
408778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	int i;
409778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner
410778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	for (i = 0; i < fb->size; i++)
411f10f262349762c96ab247b6108af3a30b52b6f5aPhilipp Reisner		fb->values[i] = value;
412778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner}
413778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner
414778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisnerstatic int fifo_push(struct fifo_buffer *fb, int value)
415778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner{
416778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	int ov;
417778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner
418778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	ov = fb->values[fb->head_index];
419778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	fb->values[fb->head_index++] = value;
420778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner
421778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	if (fb->head_index >= fb->size)
422778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner		fb->head_index = 0;
423778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner
424778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	return ov;
425778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner}
426778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner
427778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisnerstatic void fifo_add_val(struct fifo_buffer *fb, int value)
428778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner{
429778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	int i;
430778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner
431778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	for (i = 0; i < fb->size; i++)
432778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner		fb->values[i] += value;
433778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner}
434778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner
4359d77a5fee9d2a1ea4cd9a841d27b107df5913b33Philipp Reisnerstatic int drbd_rs_controller(struct drbd_conf *mdev)
436778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner{
437778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	unsigned int sect_in;  /* Number of sectors that came in since the last turn */
438778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	unsigned int want;     /* The number of sectors we want in the proxy */
439778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	int req_sect; /* Number of sectors to request in this turn */
440778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	int correction; /* Number of sectors more we need in the proxy*/
441778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	int cps; /* correction per invocation of drbd_rs_controller() */
442778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	int steps; /* Number of time steps to plan ahead */
443778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	int curr_corr;
444778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	int max_sect;
445778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner
446778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	sect_in = atomic_xchg(&mdev->rs_sect_in, 0); /* Number of sectors that came in */
447778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	mdev->rs_in_flight -= sect_in;
448778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner
449778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	spin_lock(&mdev->peer_seq_lock); /* get an atomic view on mdev->rs_plan_s */
450778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner
451778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	steps = mdev->rs_plan_s.size; /* (mdev->sync_conf.c_plan_ahead * 10 * SLEEP_TIME) / HZ; */
452778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner
453778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	if (mdev->rs_in_flight + sect_in == 0) { /* At start of resync */
454778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner		want = ((mdev->sync_conf.rate * 2 * SLEEP_TIME) / HZ) * steps;
455778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	} else { /* normal path */
456778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner		want = mdev->sync_conf.c_fill_target ? mdev->sync_conf.c_fill_target :
457778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner			sect_in * mdev->sync_conf.c_delay_target * HZ / (SLEEP_TIME * 10);
458778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	}
459778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner
460778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	correction = want - mdev->rs_in_flight - mdev->rs_planed;
461778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner
462778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	/* Plan ahead */
463778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	cps = correction / steps;
464778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	fifo_add_val(&mdev->rs_plan_s, cps);
465778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	mdev->rs_planed += cps * steps;
466778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner
467778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	/* What we do in this step */
468778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	curr_corr = fifo_push(&mdev->rs_plan_s, 0);
469778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	spin_unlock(&mdev->peer_seq_lock);
470778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	mdev->rs_planed -= curr_corr;
471778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner
472778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	req_sect = sect_in + curr_corr;
473778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	if (req_sect < 0)
474778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner		req_sect = 0;
475778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner
476778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	max_sect = (mdev->sync_conf.c_max_rate * 2 * SLEEP_TIME) / HZ;
477778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	if (req_sect > max_sect)
478778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner		req_sect = max_sect;
479778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner
480778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	/*
481778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	dev_warn(DEV, "si=%u if=%d wa=%u co=%d st=%d cps=%d pl=%d cc=%d rs=%d\n",
482778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner		 sect_in, mdev->rs_in_flight, want, correction,
483778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner		 steps, cps, mdev->rs_planed, curr_corr, req_sect);
484778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	*/
485778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner
486778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	return req_sect;
487778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner}
488778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner
4899d77a5fee9d2a1ea4cd9a841d27b107df5913b33Philipp Reisnerstatic int drbd_rs_number_requests(struct drbd_conf *mdev)
490e65f440d474d7d6a6fd8a2c844e851d8c96ed9c5Lars Ellenberg{
491e65f440d474d7d6a6fd8a2c844e851d8c96ed9c5Lars Ellenberg	int number;
492e65f440d474d7d6a6fd8a2c844e851d8c96ed9c5Lars Ellenberg	if (mdev->rs_plan_s.size) { /* mdev->sync_conf.c_plan_ahead */
493e65f440d474d7d6a6fd8a2c844e851d8c96ed9c5Lars Ellenberg		number = drbd_rs_controller(mdev) >> (BM_BLOCK_SHIFT - 9);
494e65f440d474d7d6a6fd8a2c844e851d8c96ed9c5Lars Ellenberg		mdev->c_sync_rate = number * HZ * (BM_BLOCK_SIZE / 1024) / SLEEP_TIME;
495e65f440d474d7d6a6fd8a2c844e851d8c96ed9c5Lars Ellenberg	} else {
496e65f440d474d7d6a6fd8a2c844e851d8c96ed9c5Lars Ellenberg		mdev->c_sync_rate = mdev->sync_conf.rate;
497e65f440d474d7d6a6fd8a2c844e851d8c96ed9c5Lars Ellenberg		number = SLEEP_TIME * mdev->c_sync_rate  / ((BM_BLOCK_SIZE / 1024) * HZ);
498e65f440d474d7d6a6fd8a2c844e851d8c96ed9c5Lars Ellenberg	}
499e65f440d474d7d6a6fd8a2c844e851d8c96ed9c5Lars Ellenberg
500e65f440d474d7d6a6fd8a2c844e851d8c96ed9c5Lars Ellenberg	/* ignore the amount of pending requests, the resync controller should
501e65f440d474d7d6a6fd8a2c844e851d8c96ed9c5Lars Ellenberg	 * throttle down to incoming reply rate soon enough anyways. */
502e65f440d474d7d6a6fd8a2c844e851d8c96ed9c5Lars Ellenberg	return number;
503e65f440d474d7d6a6fd8a2c844e851d8c96ed9c5Lars Ellenberg}
504e65f440d474d7d6a6fd8a2c844e851d8c96ed9c5Lars Ellenberg
5059d77a5fee9d2a1ea4cd9a841d27b107df5913b33Philipp Reisnerstatic int w_make_resync_request(struct drbd_conf *mdev,
5069d77a5fee9d2a1ea4cd9a841d27b107df5913b33Philipp Reisner				 struct drbd_work *w, int cancel)
507b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
508b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	unsigned long bit;
509b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	sector_t sector;
510b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	const sector_t capacity = drbd_get_capacity(mdev->this_bdev);
5111816a2b47afae838e53a177d5d166cc7be97d6b5Lars Ellenberg	int max_bio_size;
512e65f440d474d7d6a6fd8a2c844e851d8c96ed9c5Lars Ellenberg	int number, rollback_i, size;
513b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	int align, queued, sndbuf;
5140f0601f4ea2f53cfd8bcae060fb03d9bbde070ecLars Ellenberg	int i = 0;
515b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
516b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (unlikely(cancel))
517b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		return 1;
518b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
519af85e8e83d160f72a10e4467852646ac08614260Lars Ellenberg	if (mdev->rs_total == 0) {
520af85e8e83d160f72a10e4467852646ac08614260Lars Ellenberg		/* empty resync? */
521af85e8e83d160f72a10e4467852646ac08614260Lars Ellenberg		drbd_resync_finished(mdev);
522af85e8e83d160f72a10e4467852646ac08614260Lars Ellenberg		return 1;
523af85e8e83d160f72a10e4467852646ac08614260Lars Ellenberg	}
524af85e8e83d160f72a10e4467852646ac08614260Lars Ellenberg
525b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (!get_ldev(mdev)) {
526b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		/* Since we only need to access mdev->rsync a
527b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		   get_ldev_if_state(mdev,D_FAILED) would be sufficient, but
528b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		   to continue resync with a broken disk makes no sense at
529b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		   all */
530b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		dev_err(DEV, "Disk broke down during resync!\n");
531b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		return 1;
532b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
533b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
5340cfdd247d1779d5ffc8f685b172a526ecdc6773fPhilipp Reisner	max_bio_size = queue_max_hw_sectors(mdev->rq_queue) << 9;
535e65f440d474d7d6a6fd8a2c844e851d8c96ed9c5Lars Ellenberg	number = drbd_rs_number_requests(mdev);
536e65f440d474d7d6a6fd8a2c844e851d8c96ed9c5Lars Ellenberg	if (number == 0)
5370f0601f4ea2f53cfd8bcae060fb03d9bbde070ecLars Ellenberg		goto requeue;
538b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
539b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	for (i = 0; i < number; i++) {
540b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		/* Stop generating RS requests, when half of the send buffer is filled */
541e42325a57606396539807ff55c24febda39f8d01Philipp Reisner		mutex_lock(&mdev->tconn->data.mutex);
542e42325a57606396539807ff55c24febda39f8d01Philipp Reisner		if (mdev->tconn->data.socket) {
543e42325a57606396539807ff55c24febda39f8d01Philipp Reisner			queued = mdev->tconn->data.socket->sk->sk_wmem_queued;
544e42325a57606396539807ff55c24febda39f8d01Philipp Reisner			sndbuf = mdev->tconn->data.socket->sk->sk_sndbuf;
545b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		} else {
546b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			queued = 1;
547b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			sndbuf = 0;
548b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
549e42325a57606396539807ff55c24febda39f8d01Philipp Reisner		mutex_unlock(&mdev->tconn->data.mutex);
550b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (queued > sndbuf / 2)
551b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			goto requeue;
552b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
553b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisnernext_sector:
554b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		size = BM_BLOCK_SIZE;
555b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		bit  = drbd_bm_find_next(mdev, mdev->bm_resync_fo);
556b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
5574b0715f09655e76ca24c35a9e25e7c464c2f7346Lars Ellenberg		if (bit == DRBD_END_OF_BITMAP) {
558b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			mdev->bm_resync_fo = drbd_bm_bits(mdev);
559b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			put_ldev(mdev);
560b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			return 1;
561b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
562b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
563b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		sector = BM_BIT_TO_SECT(bit);
564b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
565e3555d8545976703938d1b59e2db509426dbe02cPhilipp Reisner		if (drbd_rs_should_slow_down(mdev, sector) ||
566e3555d8545976703938d1b59e2db509426dbe02cPhilipp Reisner		    drbd_try_rs_begin_io(mdev, sector)) {
567b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			mdev->bm_resync_fo = bit;
568b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			goto requeue;
569b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
570b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		mdev->bm_resync_fo = bit + 1;
571b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
572b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (unlikely(drbd_bm_test_bit(mdev, bit) == 0)) {
573b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			drbd_rs_complete_io(mdev, sector);
574b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			goto next_sector;
575b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
576b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
5771816a2b47afae838e53a177d5d166cc7be97d6b5Lars Ellenberg#if DRBD_MAX_BIO_SIZE > BM_BLOCK_SIZE
578b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		/* try to find some adjacent bits.
579b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		 * we stop if we have already the maximum req size.
580b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		 *
581b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		 * Additionally always align bigger requests, in order to
582b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		 * be prepared for all stripe sizes of software RAIDs.
583b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		 */
584b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		align = 1;
585d207450cf2731c6a2afa8c78fb31c7206cd35ebaPhilipp Reisner		rollback_i = i;
586b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		for (;;) {
5871816a2b47afae838e53a177d5d166cc7be97d6b5Lars Ellenberg			if (size + BM_BLOCK_SIZE > max_bio_size)
588b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner				break;
589b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
590b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			/* Be always aligned */
591b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			if (sector & ((1<<(align+3))-1))
592b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner				break;
593b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
594b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			/* do not cross extent boundaries */
595b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			if (((bit+1) & BM_BLOCKS_PER_BM_EXT_MASK) == 0)
596b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner				break;
597b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			/* now, is it actually dirty, after all?
598b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			 * caution, drbd_bm_test_bit is tri-state for some
599b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			 * obscure reason; ( b == 0 ) would get the out-of-band
600b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			 * only accidentally right because of the "oddly sized"
601b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			 * adjustment below */
602b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			if (drbd_bm_test_bit(mdev, bit+1) != 1)
603b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner				break;
604b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			bit++;
605b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			size += BM_BLOCK_SIZE;
606b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			if ((BM_BLOCK_SIZE << align) <= size)
607b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner				align++;
608b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			i++;
609b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
610b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		/* if we merged some,
611b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		 * reset the offset to start the next drbd_bm_find_next from */
612b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (size > BM_BLOCK_SIZE)
613b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			mdev->bm_resync_fo = bit + 1;
614b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner#endif
615b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
616b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		/* adjust very last sectors, in case we are oddly sized */
617b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (sector + (size>>9) > capacity)
618b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			size = (capacity-sector)<<9;
61931890f4ab299c4116cf0a104ca9ce4f9ca2c5da0Philipp Reisner		if (mdev->tconn->agreed_pro_version >= 89 && mdev->csums_tfm) {
620b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			switch (read_for_csum(mdev, sector, size)) {
62180a40e439e5a3f30b0a6210a1add6d7c33392e54Lars Ellenberg			case -EIO: /* Disk failure */
622b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner				put_ldev(mdev);
623b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner				return 0;
62480a40e439e5a3f30b0a6210a1add6d7c33392e54Lars Ellenberg			case -EAGAIN: /* allocation failed, or ldev busy */
625b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner				drbd_rs_complete_io(mdev, sector);
626b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner				mdev->bm_resync_fo = BM_SECT_TO_BIT(sector);
627d207450cf2731c6a2afa8c78fb31c7206cd35ebaPhilipp Reisner				i = rollback_i;
628b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner				goto requeue;
62980a40e439e5a3f30b0a6210a1add6d7c33392e54Lars Ellenberg			case 0:
63080a40e439e5a3f30b0a6210a1add6d7c33392e54Lars Ellenberg				/* everything ok */
63180a40e439e5a3f30b0a6210a1add6d7c33392e54Lars Ellenberg				break;
63280a40e439e5a3f30b0a6210a1add6d7c33392e54Lars Ellenberg			default:
63380a40e439e5a3f30b0a6210a1add6d7c33392e54Lars Ellenberg				BUG();
634b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			}
635b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		} else {
636b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			inc_rs_pending(mdev);
637b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			if (!drbd_send_drequest(mdev, P_RS_DATA_REQUEST,
638b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner					       sector, size, ID_SYNCER)) {
639b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner				dev_err(DEV, "drbd_send_drequest() failed, aborting...\n");
640b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner				dec_rs_pending(mdev);
641b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner				put_ldev(mdev);
642b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner				return 0;
643b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			}
644b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
645b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
646b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
647b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (mdev->bm_resync_fo >= drbd_bm_bits(mdev)) {
648b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		/* last syncer _request_ was sent,
649b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		 * but the P_RS_DATA_REPLY not yet received.  sync will end (and
650b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		 * next sync group will resume), as soon as we receive the last
651b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		 * resync data block, and the last bit is cleared.
652b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		 * until then resync "work" is "inactive" ...
653b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		 */
654b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		put_ldev(mdev);
655b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		return 1;
656b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
657b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
658b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner requeue:
659778f271dfe7a7173c0bae2d6cde8d9bd1533e668Philipp Reisner	mdev->rs_in_flight += (i << (BM_BLOCK_SHIFT - 9));
660b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	mod_timer(&mdev->resync_timer, jiffies + SLEEP_TIME);
661b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	put_ldev(mdev);
662b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	return 1;
663b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
664b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
665b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisnerstatic int w_make_ov_request(struct drbd_conf *mdev, struct drbd_work *w, int cancel)
666b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
667b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	int number, i, size;
668b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	sector_t sector;
669b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	const sector_t capacity = drbd_get_capacity(mdev->this_bdev);
670b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
671b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (unlikely(cancel))
672b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		return 1;
673b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
6742649f0809f55e4df98c333a2b85c6fc8fee04804Lars Ellenberg	number = drbd_rs_number_requests(mdev);
675b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
676b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	sector = mdev->ov_position;
677b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	for (i = 0; i < number; i++) {
678b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (sector >= capacity) {
679b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			return 1;
680b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
681b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
682b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		size = BM_BLOCK_SIZE;
683b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
684e3555d8545976703938d1b59e2db509426dbe02cPhilipp Reisner		if (drbd_rs_should_slow_down(mdev, sector) ||
685e3555d8545976703938d1b59e2db509426dbe02cPhilipp Reisner		    drbd_try_rs_begin_io(mdev, sector)) {
686b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			mdev->ov_position = sector;
687b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			goto requeue;
688b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
689b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
690b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (sector + (size>>9) > capacity)
691b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			size = (capacity-sector)<<9;
692b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
693b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		inc_rs_pending(mdev);
694b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (!drbd_send_ov_request(mdev, sector, size)) {
695b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			dec_rs_pending(mdev);
696b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			return 0;
697b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
698b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		sector += BM_SECT_PER_BIT;
699b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
700b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	mdev->ov_position = sector;
701b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
702b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner requeue:
7032649f0809f55e4df98c333a2b85c6fc8fee04804Lars Ellenberg	mdev->rs_in_flight += (i << (BM_BLOCK_SHIFT - 9));
704b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	mod_timer(&mdev->resync_timer, jiffies + SLEEP_TIME);
705b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	return 1;
706b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
707b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
708c4752ef1284519c3baa1c3b19df34a80b4905245Philipp Reisner
709370a43e7982dd497822097e0ae6022947ac2e7d4Philipp Reisnervoid start_resync_timer_fn(unsigned long data)
710370a43e7982dd497822097e0ae6022947ac2e7d4Philipp Reisner{
711370a43e7982dd497822097e0ae6022947ac2e7d4Philipp Reisner	struct drbd_conf *mdev = (struct drbd_conf *) data;
712370a43e7982dd497822097e0ae6022947ac2e7d4Philipp Reisner
713e42325a57606396539807ff55c24febda39f8d01Philipp Reisner	drbd_queue_work(&mdev->tconn->data.work, &mdev->start_resync_work);
714370a43e7982dd497822097e0ae6022947ac2e7d4Philipp Reisner}
715370a43e7982dd497822097e0ae6022947ac2e7d4Philipp Reisner
716c4752ef1284519c3baa1c3b19df34a80b4905245Philipp Reisnerint w_start_resync(struct drbd_conf *mdev, struct drbd_work *w, int cancel)
717c4752ef1284519c3baa1c3b19df34a80b4905245Philipp Reisner{
718370a43e7982dd497822097e0ae6022947ac2e7d4Philipp Reisner	if (atomic_read(&mdev->unacked_cnt) || atomic_read(&mdev->rs_pending_cnt)) {
719370a43e7982dd497822097e0ae6022947ac2e7d4Philipp Reisner		dev_warn(DEV, "w_start_resync later...\n");
720370a43e7982dd497822097e0ae6022947ac2e7d4Philipp Reisner		mdev->start_resync_timer.expires = jiffies + HZ/10;
721370a43e7982dd497822097e0ae6022947ac2e7d4Philipp Reisner		add_timer(&mdev->start_resync_timer);
722370a43e7982dd497822097e0ae6022947ac2e7d4Philipp Reisner		return 1;
723370a43e7982dd497822097e0ae6022947ac2e7d4Philipp Reisner	}
724c4752ef1284519c3baa1c3b19df34a80b4905245Philipp Reisner
725370a43e7982dd497822097e0ae6022947ac2e7d4Philipp Reisner	drbd_start_resync(mdev, C_SYNC_SOURCE);
726370a43e7982dd497822097e0ae6022947ac2e7d4Philipp Reisner	clear_bit(AHEAD_TO_SYNC_SOURCE, &mdev->current_epoch->flags);
727c4752ef1284519c3baa1c3b19df34a80b4905245Philipp Reisner	return 1;
728c4752ef1284519c3baa1c3b19df34a80b4905245Philipp Reisner}
729c4752ef1284519c3baa1c3b19df34a80b4905245Philipp Reisner
730b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisnerint w_ov_finished(struct drbd_conf *mdev, struct drbd_work *w, int cancel)
731b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
732b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	kfree(w);
733b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	ov_oos_print(mdev);
734b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	drbd_resync_finished(mdev);
735b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
736b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	return 1;
737b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
738b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
739b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisnerstatic int w_resync_finished(struct drbd_conf *mdev, struct drbd_work *w, int cancel)
740b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
741b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	kfree(w);
742b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
743b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	drbd_resync_finished(mdev);
744b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
745b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	return 1;
746b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
747b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
748af85e8e83d160f72a10e4467852646ac08614260Lars Ellenbergstatic void ping_peer(struct drbd_conf *mdev)
749af85e8e83d160f72a10e4467852646ac08614260Lars Ellenberg{
750af85e8e83d160f72a10e4467852646ac08614260Lars Ellenberg	clear_bit(GOT_PING_ACK, &mdev->flags);
751af85e8e83d160f72a10e4467852646ac08614260Lars Ellenberg	request_ping(mdev);
752af85e8e83d160f72a10e4467852646ac08614260Lars Ellenberg	wait_event(mdev->misc_wait,
753af85e8e83d160f72a10e4467852646ac08614260Lars Ellenberg		   test_bit(GOT_PING_ACK, &mdev->flags) || mdev->state.conn < C_CONNECTED);
754af85e8e83d160f72a10e4467852646ac08614260Lars Ellenberg}
755af85e8e83d160f72a10e4467852646ac08614260Lars Ellenberg
756b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisnerint drbd_resync_finished(struct drbd_conf *mdev)
757b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
758b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	unsigned long db, dt, dbdt;
759b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	unsigned long n_oos;
760b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	union drbd_state os, ns;
761b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	struct drbd_work *w;
762b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	char *khelper_cmd = NULL;
76326525618863afcc4aab8b2a83451d37c6f513460Lars Ellenberg	int verify_done = 0;
764b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
765b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	/* Remove all elements from the resync LRU. Since future actions
766b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	 * might set bits in the (main) bitmap, then the entries in the
767b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	 * resync LRU would be wrong. */
768b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (drbd_rs_del_all(mdev)) {
769b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		/* In case this is not possible now, most probably because
770b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		 * there are P_RS_DATA_REPLY Packets lingering on the worker's
771b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		 * queue (or even the read operations for those packets
772b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		 * is not finished by now).   Retry in 100ms. */
773b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
77420ee639024e3d33111df0e343050b218c656bf16Philipp Reisner		schedule_timeout_interruptible(HZ / 10);
775b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		w = kmalloc(sizeof(struct drbd_work), GFP_ATOMIC);
776b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (w) {
777b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			w->cb = w_resync_finished;
778e42325a57606396539807ff55c24febda39f8d01Philipp Reisner			drbd_queue_work(&mdev->tconn->data.work, w);
779b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			return 1;
780b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
781b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		dev_err(DEV, "Warn failed to drbd_rs_del_all() and to kmalloc(w).\n");
782b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
783b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
784b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	dt = (jiffies - mdev->rs_start - mdev->rs_paused) / HZ;
785b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (dt <= 0)
786b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		dt = 1;
787b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	db = mdev->rs_total;
788b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	dbdt = Bit2KB(db/dt);
789b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	mdev->rs_paused /= HZ;
790b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
791b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (!get_ldev(mdev))
792b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		goto out;
793b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
794af85e8e83d160f72a10e4467852646ac08614260Lars Ellenberg	ping_peer(mdev);
795af85e8e83d160f72a10e4467852646ac08614260Lars Ellenberg
79687eeee41f8740451b61a1e7d37a494333a906861Philipp Reisner	spin_lock_irq(&mdev->tconn->req_lock);
797b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	os = mdev->state;
798b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
79926525618863afcc4aab8b2a83451d37c6f513460Lars Ellenberg	verify_done = (os.conn == C_VERIFY_S || os.conn == C_VERIFY_T);
80026525618863afcc4aab8b2a83451d37c6f513460Lars Ellenberg
801b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	/* This protects us against multiple calls (that can happen in the presence
802b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	   of application IO), and against connectivity loss just before we arrive here. */
803b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (os.conn <= C_CONNECTED)
804b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		goto out_unlock;
805b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
806b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	ns = os;
807b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	ns.conn = C_CONNECTED;
808b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
809b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	dev_info(DEV, "%s done (total %lu sec; paused %lu sec; %lu K/sec)\n",
81026525618863afcc4aab8b2a83451d37c6f513460Lars Ellenberg	     verify_done ? "Online verify " : "Resync",
811b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	     dt + mdev->rs_paused, mdev->rs_paused, dbdt);
812b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
813b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	n_oos = drbd_bm_total_weight(mdev);
814b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
815b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (os.conn == C_VERIFY_S || os.conn == C_VERIFY_T) {
816b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (n_oos) {
817b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			dev_alert(DEV, "Online verify found %lu %dk block out of sync!\n",
818b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			      n_oos, Bit2KB(1));
819b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			khelper_cmd = "out-of-sync";
820b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
821b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	} else {
822b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		D_ASSERT((n_oos - mdev->rs_failed) == 0);
823b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
824b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (os.conn == C_SYNC_TARGET || os.conn == C_PAUSED_SYNC_T)
825b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			khelper_cmd = "after-resync-target";
826b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
827b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (mdev->csums_tfm && mdev->rs_total) {
828b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			const unsigned long s = mdev->rs_same_csum;
829b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			const unsigned long t = mdev->rs_total;
830b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			const int ratio =
831b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner				(t == 0)     ? 0 :
832b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			(t < 100000) ? ((s*100)/t) : (s/(t/100));
83324c4830c8ec3cbc904d84c213126a35f41a4e455Bart Van Assche			dev_info(DEV, "%u %% had equal checksums, eliminated: %luK; "
834b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			     "transferred %luK total %luK\n",
835b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			     ratio,
836b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			     Bit2KB(mdev->rs_same_csum),
837b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			     Bit2KB(mdev->rs_total - mdev->rs_same_csum),
838b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			     Bit2KB(mdev->rs_total));
839b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
840b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
841b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
842b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (mdev->rs_failed) {
843b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		dev_info(DEV, "            %lu failed blocks\n", mdev->rs_failed);
844b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
845b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (os.conn == C_SYNC_TARGET || os.conn == C_PAUSED_SYNC_T) {
846b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			ns.disk = D_INCONSISTENT;
847b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			ns.pdsk = D_UP_TO_DATE;
848b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		} else {
849b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			ns.disk = D_UP_TO_DATE;
850b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			ns.pdsk = D_INCONSISTENT;
851b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
852b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	} else {
853b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		ns.disk = D_UP_TO_DATE;
854b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		ns.pdsk = D_UP_TO_DATE;
855b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
856b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (os.conn == C_SYNC_TARGET || os.conn == C_PAUSED_SYNC_T) {
857b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			if (mdev->p_uuid) {
858b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner				int i;
859b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner				for (i = UI_BITMAP ; i <= UI_HISTORY_END ; i++)
860b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner					_drbd_uuid_set(mdev, i, mdev->p_uuid[i]);
861b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner				drbd_uuid_set(mdev, UI_BITMAP, mdev->ldev->md.uuid[UI_CURRENT]);
862b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner				_drbd_uuid_set(mdev, UI_CURRENT, mdev->p_uuid[UI_CURRENT]);
863b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			} else {
864b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner				dev_err(DEV, "mdev->p_uuid is NULL! BUG\n");
865b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			}
866b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
867b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
86862b0da3a244ac33d25a77861ef1cc0080103f2ffLars Ellenberg		if (!(os.conn == C_VERIFY_S || os.conn == C_VERIFY_T)) {
86962b0da3a244ac33d25a77861ef1cc0080103f2ffLars Ellenberg			/* for verify runs, we don't update uuids here,
87062b0da3a244ac33d25a77861ef1cc0080103f2ffLars Ellenberg			 * so there would be nothing to report. */
87162b0da3a244ac33d25a77861ef1cc0080103f2ffLars Ellenberg			drbd_uuid_set_bm(mdev, 0UL);
87262b0da3a244ac33d25a77861ef1cc0080103f2ffLars Ellenberg			drbd_print_uuids(mdev, "updated UUIDs");
87362b0da3a244ac33d25a77861ef1cc0080103f2ffLars Ellenberg			if (mdev->p_uuid) {
87462b0da3a244ac33d25a77861ef1cc0080103f2ffLars Ellenberg				/* Now the two UUID sets are equal, update what we
87562b0da3a244ac33d25a77861ef1cc0080103f2ffLars Ellenberg				 * know of the peer. */
87662b0da3a244ac33d25a77861ef1cc0080103f2ffLars Ellenberg				int i;
87762b0da3a244ac33d25a77861ef1cc0080103f2ffLars Ellenberg				for (i = UI_CURRENT ; i <= UI_HISTORY_END ; i++)
87862b0da3a244ac33d25a77861ef1cc0080103f2ffLars Ellenberg					mdev->p_uuid[i] = mdev->ldev->md.uuid[i];
87962b0da3a244ac33d25a77861ef1cc0080103f2ffLars Ellenberg			}
880b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
881b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
882b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
883b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	_drbd_set_state(mdev, ns, CS_VERBOSE, NULL);
884b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisnerout_unlock:
88587eeee41f8740451b61a1e7d37a494333a906861Philipp Reisner	spin_unlock_irq(&mdev->tconn->req_lock);
886b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	put_ldev(mdev);
887b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisnerout:
888b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	mdev->rs_total  = 0;
889b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	mdev->rs_failed = 0;
890b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	mdev->rs_paused = 0;
89126525618863afcc4aab8b2a83451d37c6f513460Lars Ellenberg	if (verify_done)
89226525618863afcc4aab8b2a83451d37c6f513460Lars Ellenberg		mdev->ov_start_sector = 0;
893b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
89413d42685bec1f012dcbc5d187490eb1d15ec8219Lars Ellenberg	drbd_md_sync(mdev);
89513d42685bec1f012dcbc5d187490eb1d15ec8219Lars Ellenberg
896b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (khelper_cmd)
897b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		drbd_khelper(mdev, khelper_cmd);
898b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
899b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	return 1;
900b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
901b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
902b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner/* helper */
903b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisnerstatic void move_to_net_ee_or_free(struct drbd_conf *mdev, struct drbd_epoch_entry *e)
904b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
90545bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	if (drbd_ee_has_active_page(e)) {
906b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		/* This might happen if sendpage() has not finished */
907010f6e678ffddbf3134863038c5b2f6509f1eed3Andreas Gruenbacher		int i = (e->i.size + PAGE_SIZE -1) >> PAGE_SHIFT;
908435f07402b3165b90592073bc0f8c6f8fa160ff9Lars Ellenberg		atomic_add(i, &mdev->pp_in_use_by_net);
909435f07402b3165b90592073bc0f8c6f8fa160ff9Lars Ellenberg		atomic_sub(i, &mdev->pp_in_use);
91087eeee41f8740451b61a1e7d37a494333a906861Philipp Reisner		spin_lock_irq(&mdev->tconn->req_lock);
911b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		list_add_tail(&e->w.list, &mdev->net_ee);
91287eeee41f8740451b61a1e7d37a494333a906861Philipp Reisner		spin_unlock_irq(&mdev->tconn->req_lock);
913435f07402b3165b90592073bc0f8c6f8fa160ff9Lars Ellenberg		wake_up(&drbd_pp_wait);
914b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	} else
915b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		drbd_free_ee(mdev, e);
916b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
917b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
918b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner/**
919b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * w_e_end_data_req() - Worker callback, to send a P_DATA_REPLY packet in response to a P_DATA_REQUEST
920b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * @mdev:	DRBD device.
921b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * @w:		work object.
922b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * @cancel:	The connection will be closed anyways
923b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner */
924b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisnerint w_e_end_data_req(struct drbd_conf *mdev, struct drbd_work *w, int cancel)
925b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
926b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	struct drbd_epoch_entry *e = container_of(w, struct drbd_epoch_entry, w);
927b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	int ok;
928b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
929b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (unlikely(cancel)) {
930b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		drbd_free_ee(mdev, e);
931b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		dec_unacked(mdev);
932b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		return 1;
933b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
934b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
93545bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	if (likely((e->flags & EE_WAS_ERROR) == 0)) {
936b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		ok = drbd_send_block(mdev, P_DATA_REPLY, e);
937b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	} else {
938b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (__ratelimit(&drbd_ratelimit_state))
939b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			dev_err(DEV, "Sending NegDReply. sector=%llus.\n",
940010f6e678ffddbf3134863038c5b2f6509f1eed3Andreas Gruenbacher			    (unsigned long long)e->i.sector);
941b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
942b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		ok = drbd_send_ack(mdev, P_NEG_DREPLY, e);
943b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
944b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
945b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	dec_unacked(mdev);
946b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
947b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	move_to_net_ee_or_free(mdev, e);
948b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
949b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (unlikely(!ok))
950b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		dev_err(DEV, "drbd_send_block() failed\n");
951b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	return ok;
952b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
953b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
954b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner/**
955b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * w_e_end_rsdata_req() - Worker callback to send a P_RS_DATA_REPLY packet in response to a P_RS_DATA_REQUESTRS
956b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * @mdev:	DRBD device.
957b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * @w:		work object.
958b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * @cancel:	The connection will be closed anyways
959b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner */
960b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisnerint w_e_end_rsdata_req(struct drbd_conf *mdev, struct drbd_work *w, int cancel)
961b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
962b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	struct drbd_epoch_entry *e = container_of(w, struct drbd_epoch_entry, w);
963b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	int ok;
964b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
965b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (unlikely(cancel)) {
966b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		drbd_free_ee(mdev, e);
967b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		dec_unacked(mdev);
968b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		return 1;
969b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
970b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
971b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (get_ldev_if_state(mdev, D_FAILED)) {
972010f6e678ffddbf3134863038c5b2f6509f1eed3Andreas Gruenbacher		drbd_rs_complete_io(mdev, e->i.sector);
973b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		put_ldev(mdev);
974b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
975b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
976d612d309e4c8401ad94c531678b59c4a8b7c41cePhilipp Reisner	if (mdev->state.conn == C_AHEAD) {
977d612d309e4c8401ad94c531678b59c4a8b7c41cePhilipp Reisner		ok = drbd_send_ack(mdev, P_RS_CANCEL, e);
978d612d309e4c8401ad94c531678b59c4a8b7c41cePhilipp Reisner	} else if (likely((e->flags & EE_WAS_ERROR) == 0)) {
979b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (likely(mdev->state.pdsk >= D_INCONSISTENT)) {
980b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			inc_rs_pending(mdev);
981b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			ok = drbd_send_block(mdev, P_RS_DATA_REPLY, e);
982b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		} else {
983b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			if (__ratelimit(&drbd_ratelimit_state))
984b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner				dev_err(DEV, "Not sending RSDataReply, "
985b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner				    "partner DISKLESS!\n");
986b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			ok = 1;
987b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
988b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	} else {
989b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (__ratelimit(&drbd_ratelimit_state))
990b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			dev_err(DEV, "Sending NegRSDReply. sector %llus.\n",
991010f6e678ffddbf3134863038c5b2f6509f1eed3Andreas Gruenbacher			    (unsigned long long)e->i.sector);
992b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
993b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		ok = drbd_send_ack(mdev, P_NEG_RS_DREPLY, e);
994b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
995b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		/* update resync data with failure */
996010f6e678ffddbf3134863038c5b2f6509f1eed3Andreas Gruenbacher		drbd_rs_failed_io(mdev, e->i.sector, e->i.size);
997b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
998b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
999b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	dec_unacked(mdev);
1000b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1001b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	move_to_net_ee_or_free(mdev, e);
1002b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1003b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (unlikely(!ok))
1004b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		dev_err(DEV, "drbd_send_block() failed\n");
1005b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	return ok;
1006b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
1007b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1008b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisnerint w_e_end_csum_rs_req(struct drbd_conf *mdev, struct drbd_work *w, int cancel)
1009b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
1010b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	struct drbd_epoch_entry *e = container_of(w, struct drbd_epoch_entry, w);
1011b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	struct digest_info *di;
1012b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	int digest_size;
1013b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	void *digest = NULL;
1014b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	int ok, eq = 0;
1015b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1016b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (unlikely(cancel)) {
1017b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		drbd_free_ee(mdev, e);
1018b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		dec_unacked(mdev);
1019b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		return 1;
1020b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
1021b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
10221d53f09e170e477de67babd7a10e277479260d51Lars Ellenberg	if (get_ldev(mdev)) {
1023010f6e678ffddbf3134863038c5b2f6509f1eed3Andreas Gruenbacher		drbd_rs_complete_io(mdev, e->i.sector);
10241d53f09e170e477de67babd7a10e277479260d51Lars Ellenberg		put_ldev(mdev);
10251d53f09e170e477de67babd7a10e277479260d51Lars Ellenberg	}
1026b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
102785719573dd716bc2ac3e098b44adfed884250babPhilipp Reisner	di = e->digest;
1028b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
102945bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	if (likely((e->flags & EE_WAS_ERROR) == 0)) {
1030b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		/* quick hack to try to avoid a race against reconfiguration.
1031b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		 * a real fix would be much more involved,
1032b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		 * introducing more locking mechanisms */
1033b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (mdev->csums_tfm) {
1034b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			digest_size = crypto_hash_digestsize(mdev->csums_tfm);
1035b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			D_ASSERT(digest_size == di->digest_size);
1036b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			digest = kmalloc(digest_size, GFP_NOIO);
1037b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
1038b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (digest) {
103945bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg			drbd_csum_ee(mdev, mdev->csums_tfm, e, digest);
1040b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			eq = !memcmp(digest, di->digest, digest_size);
1041b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			kfree(digest);
1042b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
1043b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1044b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (eq) {
1045010f6e678ffddbf3134863038c5b2f6509f1eed3Andreas Gruenbacher			drbd_set_in_sync(mdev, e->i.sector, e->i.size);
1046676396d545350a70d922605ec23c2ed26124334aLars Ellenberg			/* rs_same_csums unit is BM_BLOCK_SIZE */
1047010f6e678ffddbf3134863038c5b2f6509f1eed3Andreas Gruenbacher			mdev->rs_same_csum += e->i.size >> BM_BLOCK_SHIFT;
1048b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			ok = drbd_send_ack(mdev, P_RS_IS_IN_SYNC, e);
1049b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		} else {
1050b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			inc_rs_pending(mdev);
1051204bba9965c4cc175bf5bc65ddd19889e9085c72Philipp Reisner			e->block_id = ID_SYNCER; /* By setting block_id, digest pointer becomes invalid! */
1052204bba9965c4cc175bf5bc65ddd19889e9085c72Philipp Reisner			e->flags &= ~EE_HAS_DIGEST; /* This e no longer has a digest pointer */
1053204bba9965c4cc175bf5bc65ddd19889e9085c72Philipp Reisner			kfree(di);
1054b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			ok = drbd_send_block(mdev, P_RS_DATA_REPLY, e);
1055b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
1056b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	} else {
1057b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		ok = drbd_send_ack(mdev, P_NEG_RS_DREPLY, e);
1058b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (__ratelimit(&drbd_ratelimit_state))
1059b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			dev_err(DEV, "Sending NegDReply. I guess it gets messy.\n");
1060b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
1061b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1062b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	dec_unacked(mdev);
1063b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	move_to_net_ee_or_free(mdev, e);
1064b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1065b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (unlikely(!ok))
1066b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		dev_err(DEV, "drbd_send_block/ack() failed\n");
1067b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	return ok;
1068b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
1069b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
107053ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenberg/* TODO merge common code with w_e_send_csum */
1071b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisnerint w_e_end_ov_req(struct drbd_conf *mdev, struct drbd_work *w, int cancel)
1072b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
1073b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	struct drbd_epoch_entry *e = container_of(w, struct drbd_epoch_entry, w);
1074010f6e678ffddbf3134863038c5b2f6509f1eed3Andreas Gruenbacher	sector_t sector = e->i.sector;
1075010f6e678ffddbf3134863038c5b2f6509f1eed3Andreas Gruenbacher	unsigned int size = e->i.size;
1076b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	int digest_size;
1077b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	void *digest;
1078b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	int ok = 1;
1079b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1080b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (unlikely(cancel))
1081b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		goto out;
1082b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1083b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	digest_size = crypto_hash_digestsize(mdev->verify_tfm);
1084b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	digest = kmalloc(digest_size, GFP_NOIO);
10858f21420ebd5ca5a751e2f606b49b0acd2a2af314Philipp Reisner	if (!digest) {
10868f21420ebd5ca5a751e2f606b49b0acd2a2af314Philipp Reisner		ok = 0;	/* terminate the connection in case the allocation failed */
10878f21420ebd5ca5a751e2f606b49b0acd2a2af314Philipp Reisner		goto out;
1088b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
1089b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
10908f21420ebd5ca5a751e2f606b49b0acd2a2af314Philipp Reisner	if (likely(!(e->flags & EE_WAS_ERROR)))
10918f21420ebd5ca5a751e2f606b49b0acd2a2af314Philipp Reisner		drbd_csum_ee(mdev, mdev->verify_tfm, e, digest);
10928f21420ebd5ca5a751e2f606b49b0acd2a2af314Philipp Reisner	else
10938f21420ebd5ca5a751e2f606b49b0acd2a2af314Philipp Reisner		memset(digest, 0, digest_size);
10948f21420ebd5ca5a751e2f606b49b0acd2a2af314Philipp Reisner
109553ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenberg	/* Free e and pages before send.
109653ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenberg	 * In case we block on congestion, we could otherwise run into
109753ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenberg	 * some distributed deadlock, if the other side blocks on
109853ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenberg	 * congestion as well, because our receiver blocks in
109953ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenberg	 * drbd_pp_alloc due to pp_in_use > max_buffers. */
110053ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenberg	drbd_free_ee(mdev, e);
110153ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenberg	e = NULL;
11028f21420ebd5ca5a751e2f606b49b0acd2a2af314Philipp Reisner	inc_rs_pending(mdev);
110353ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenberg	ok = drbd_send_drequest_csum(mdev, sector, size,
110453ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenberg				     digest, digest_size,
110553ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenberg				     P_OV_REPLY);
11068f21420ebd5ca5a751e2f606b49b0acd2a2af314Philipp Reisner	if (!ok)
11078f21420ebd5ca5a751e2f606b49b0acd2a2af314Philipp Reisner		dec_rs_pending(mdev);
11088f21420ebd5ca5a751e2f606b49b0acd2a2af314Philipp Reisner	kfree(digest);
11098f21420ebd5ca5a751e2f606b49b0acd2a2af314Philipp Reisner
1110b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisnerout:
111153ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenberg	if (e)
111253ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenberg		drbd_free_ee(mdev, e);
1113b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	dec_unacked(mdev);
1114b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	return ok;
1115b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
1116b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1117b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisnervoid drbd_ov_oos_found(struct drbd_conf *mdev, sector_t sector, int size)
1118b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
1119b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (mdev->ov_last_oos_start + mdev->ov_last_oos_size == sector) {
1120b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		mdev->ov_last_oos_size += size>>9;
1121b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	} else {
1122b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		mdev->ov_last_oos_start = sector;
1123b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		mdev->ov_last_oos_size = size>>9;
1124b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
1125b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	drbd_set_out_of_sync(mdev, sector, size);
1126b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
1127b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1128b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisnerint w_e_end_ov_reply(struct drbd_conf *mdev, struct drbd_work *w, int cancel)
1129b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
1130b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	struct drbd_epoch_entry *e = container_of(w, struct drbd_epoch_entry, w);
1131b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	struct digest_info *di;
1132b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	void *digest;
1133010f6e678ffddbf3134863038c5b2f6509f1eed3Andreas Gruenbacher	sector_t sector = e->i.sector;
1134010f6e678ffddbf3134863038c5b2f6509f1eed3Andreas Gruenbacher	unsigned int size = e->i.size;
113553ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenberg	int digest_size;
1136b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	int ok, eq = 0;
1137b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1138b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (unlikely(cancel)) {
1139b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		drbd_free_ee(mdev, e);
1140b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		dec_unacked(mdev);
1141b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		return 1;
1142b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
1143b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1144b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	/* after "cancel", because after drbd_disconnect/drbd_rs_cancel_all
1145b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	 * the resync lru has been cleaned up already */
11461d53f09e170e477de67babd7a10e277479260d51Lars Ellenberg	if (get_ldev(mdev)) {
1147010f6e678ffddbf3134863038c5b2f6509f1eed3Andreas Gruenbacher		drbd_rs_complete_io(mdev, e->i.sector);
11481d53f09e170e477de67babd7a10e277479260d51Lars Ellenberg		put_ldev(mdev);
11491d53f09e170e477de67babd7a10e277479260d51Lars Ellenberg	}
1150b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
115185719573dd716bc2ac3e098b44adfed884250babPhilipp Reisner	di = e->digest;
1152b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
115345bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg	if (likely((e->flags & EE_WAS_ERROR) == 0)) {
1154b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		digest_size = crypto_hash_digestsize(mdev->verify_tfm);
1155b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		digest = kmalloc(digest_size, GFP_NOIO);
1156b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (digest) {
115745bb912bd5ea4d2b3a270a93cbdf767a0e2df6f5Lars Ellenberg			drbd_csum_ee(mdev, mdev->verify_tfm, e, digest);
1158b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1159b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			D_ASSERT(digest_size == di->digest_size);
1160b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			eq = !memcmp(digest, di->digest, digest_size);
1161b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			kfree(digest);
1162b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
1163b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
1164b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
116553ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenberg		/* Free e and pages before send.
116653ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenberg		 * In case we block on congestion, we could otherwise run into
116753ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenberg		 * some distributed deadlock, if the other side blocks on
116853ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenberg		 * congestion as well, because our receiver blocks in
116953ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenberg		 * drbd_pp_alloc due to pp_in_use > max_buffers. */
117053ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenberg	drbd_free_ee(mdev, e);
1171b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (!eq)
117253ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenberg		drbd_ov_oos_found(mdev, sector, size);
1173b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	else
1174b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		ov_oos_print(mdev);
1175b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
117653ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenberg	ok = drbd_send_ack_ex(mdev, P_OV_RESULT, sector, size,
1177b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			      eq ? ID_IN_SYNC : ID_OUT_OF_SYNC);
1178b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
117953ea433145d9a56c7ad5e69f21f5662053e00e84Lars Ellenberg	dec_unacked(mdev);
1180b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1181ea5442aff68c559c951373739201721185191748Lars Ellenberg	--mdev->ov_left;
1182ea5442aff68c559c951373739201721185191748Lars Ellenberg
1183ea5442aff68c559c951373739201721185191748Lars Ellenberg	/* let's advance progress step marks only for every other megabyte */
1184ea5442aff68c559c951373739201721185191748Lars Ellenberg	if ((mdev->ov_left & 0x200) == 0x200)
1185ea5442aff68c559c951373739201721185191748Lars Ellenberg		drbd_advance_rs_marks(mdev, mdev->ov_left);
1186ea5442aff68c559c951373739201721185191748Lars Ellenberg
1187ea5442aff68c559c951373739201721185191748Lars Ellenberg	if (mdev->ov_left == 0) {
1188b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		ov_oos_print(mdev);
1189b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		drbd_resync_finished(mdev);
1190b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
1191b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1192b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	return ok;
1193b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
1194b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1195b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisnerint w_prev_work_done(struct drbd_conf *mdev, struct drbd_work *w, int cancel)
1196b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
1197b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	struct drbd_wq_barrier *b = container_of(w, struct drbd_wq_barrier, w);
1198b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	complete(&b->done);
1199b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	return 1;
1200b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
1201b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1202b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisnerint w_send_barrier(struct drbd_conf *mdev, struct drbd_work *w, int cancel)
1203b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
1204b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	struct drbd_tl_epoch *b = container_of(w, struct drbd_tl_epoch, w);
1205e42325a57606396539807ff55c24febda39f8d01Philipp Reisner	struct p_barrier *p = &mdev->tconn->data.sbuf.barrier;
1206b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	int ok = 1;
1207b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1208b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	/* really avoid racing with tl_clear.  w.cb may have been referenced
1209b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	 * just before it was reassigned and re-queued, so double check that.
1210b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	 * actually, this race was harmless, since we only try to send the
1211b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	 * barrier packet here, and otherwise do nothing with the object.
1212b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	 * but compare with the head of w_clear_epoch */
121387eeee41f8740451b61a1e7d37a494333a906861Philipp Reisner	spin_lock_irq(&mdev->tconn->req_lock);
1214b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (w->cb != w_send_barrier || mdev->state.conn < C_CONNECTED)
1215b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		cancel = 1;
121687eeee41f8740451b61a1e7d37a494333a906861Philipp Reisner	spin_unlock_irq(&mdev->tconn->req_lock);
1217b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (cancel)
1218b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		return 1;
1219b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1220b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (!drbd_get_data_sock(mdev))
1221b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		return 0;
1222b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	p->barrier = b->br_number;
1223b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	/* inc_ap_pending was done where this was queued.
1224b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	 * dec_ap_pending will be done in got_BarrierAck
1225b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	 * or (on connection loss) in w_clear_epoch.  */
1226e42325a57606396539807ff55c24febda39f8d01Philipp Reisner	ok = _drbd_send_cmd(mdev, mdev->tconn->data.socket, P_BARRIER,
12270b70a13dac014ec9274640b9e945bde493ba365ePhilipp Reisner				(struct p_header80 *)p, sizeof(*p), 0);
1228b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	drbd_put_data_sock(mdev);
1229b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1230b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	return ok;
1231b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
1232b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1233b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisnerint w_send_write_hint(struct drbd_conf *mdev, struct drbd_work *w, int cancel)
1234b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
1235b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (cancel)
1236b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		return 1;
1237b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	return drbd_send_short_cmd(mdev, P_UNPLUG_REMOTE);
1238b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
1239b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
124073a01a18b9c28a0fab1131ece5b0a9bc00a879b8Philipp Reisnerint w_send_oos(struct drbd_conf *mdev, struct drbd_work *w, int cancel)
124173a01a18b9c28a0fab1131ece5b0a9bc00a879b8Philipp Reisner{
124273a01a18b9c28a0fab1131ece5b0a9bc00a879b8Philipp Reisner	struct drbd_request *req = container_of(w, struct drbd_request, w);
124373a01a18b9c28a0fab1131ece5b0a9bc00a879b8Philipp Reisner	int ok;
124473a01a18b9c28a0fab1131ece5b0a9bc00a879b8Philipp Reisner
124573a01a18b9c28a0fab1131ece5b0a9bc00a879b8Philipp Reisner	if (unlikely(cancel)) {
12468554df1c6d3bb7686b39ed775772f507fa857c19Andreas Gruenbacher		req_mod(req, SEND_CANCELED);
124773a01a18b9c28a0fab1131ece5b0a9bc00a879b8Philipp Reisner		return 1;
124873a01a18b9c28a0fab1131ece5b0a9bc00a879b8Philipp Reisner	}
124973a01a18b9c28a0fab1131ece5b0a9bc00a879b8Philipp Reisner
125073a01a18b9c28a0fab1131ece5b0a9bc00a879b8Philipp Reisner	ok = drbd_send_oos(mdev, req);
12518554df1c6d3bb7686b39ed775772f507fa857c19Andreas Gruenbacher	req_mod(req, OOS_HANDED_TO_NETWORK);
125273a01a18b9c28a0fab1131ece5b0a9bc00a879b8Philipp Reisner
125373a01a18b9c28a0fab1131ece5b0a9bc00a879b8Philipp Reisner	return ok;
125473a01a18b9c28a0fab1131ece5b0a9bc00a879b8Philipp Reisner}
125573a01a18b9c28a0fab1131ece5b0a9bc00a879b8Philipp Reisner
1256b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner/**
1257b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * w_send_dblock() - Worker callback to send a P_DATA packet in order to mirror a write request
1258b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * @mdev:	DRBD device.
1259b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * @w:		work object.
1260b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * @cancel:	The connection will be closed anyways
1261b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner */
1262b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisnerint w_send_dblock(struct drbd_conf *mdev, struct drbd_work *w, int cancel)
1263b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
1264b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	struct drbd_request *req = container_of(w, struct drbd_request, w);
1265b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	int ok;
1266b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1267b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (unlikely(cancel)) {
12688554df1c6d3bb7686b39ed775772f507fa857c19Andreas Gruenbacher		req_mod(req, SEND_CANCELED);
1269b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		return 1;
1270b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
1271b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1272b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	ok = drbd_send_dblock(mdev, req);
12738554df1c6d3bb7686b39ed775772f507fa857c19Andreas Gruenbacher	req_mod(req, ok ? HANDED_OVER_TO_NETWORK : SEND_FAILED);
1274b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1275b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	return ok;
1276b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
1277b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1278b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner/**
1279b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * w_send_read_req() - Worker callback to send a read request (P_DATA_REQUEST) packet
1280b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * @mdev:	DRBD device.
1281b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * @w:		work object.
1282b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * @cancel:	The connection will be closed anyways
1283b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner */
1284b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisnerint w_send_read_req(struct drbd_conf *mdev, struct drbd_work *w, int cancel)
1285b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
1286b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	struct drbd_request *req = container_of(w, struct drbd_request, w);
1287b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	int ok;
1288b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1289b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (unlikely(cancel)) {
12908554df1c6d3bb7686b39ed775772f507fa857c19Andreas Gruenbacher		req_mod(req, SEND_CANCELED);
1291b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		return 1;
1292b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
1293b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1294ace652acf2d7e564dac48c615d9184e7ed575f9cAndreas Gruenbacher	ok = drbd_send_drequest(mdev, P_DATA_REQUEST, req->i.sector, req->i.size,
1295b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner				(unsigned long)req);
1296b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1297b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (!ok) {
1298b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		/* ?? we set C_TIMEOUT or C_BROKEN_PIPE in drbd_send();
1299b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		 * so this is probably redundant */
1300b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (mdev->state.conn >= C_CONNECTED)
1301b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			drbd_force_state(mdev, NS(conn, C_NETWORK_FAILURE));
1302b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
13038554df1c6d3bb7686b39ed775772f507fa857c19Andreas Gruenbacher	req_mod(req, ok ? HANDED_OVER_TO_NETWORK : SEND_FAILED);
1304b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1305b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	return ok;
1306b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
1307b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1308265be2d09853d425ad14a61cda0ca63345613d0cPhilipp Reisnerint w_restart_disk_io(struct drbd_conf *mdev, struct drbd_work *w, int cancel)
1309265be2d09853d425ad14a61cda0ca63345613d0cPhilipp Reisner{
1310265be2d09853d425ad14a61cda0ca63345613d0cPhilipp Reisner	struct drbd_request *req = container_of(w, struct drbd_request, w);
1311265be2d09853d425ad14a61cda0ca63345613d0cPhilipp Reisner
13120778286a133d2d3f81861a4e5db308e359583006Philipp Reisner	if (bio_data_dir(req->master_bio) == WRITE && req->rq_state & RQ_IN_ACT_LOG)
1313ace652acf2d7e564dac48c615d9184e7ed575f9cAndreas Gruenbacher		drbd_al_begin_io(mdev, req->i.sector);
1314265be2d09853d425ad14a61cda0ca63345613d0cPhilipp Reisner	/* Calling drbd_al_begin_io() out of the worker might deadlocks
1315265be2d09853d425ad14a61cda0ca63345613d0cPhilipp Reisner	   theoretically. Practically it can not deadlock, since this is
1316265be2d09853d425ad14a61cda0ca63345613d0cPhilipp Reisner	   only used when unfreezing IOs. All the extents of the requests
1317265be2d09853d425ad14a61cda0ca63345613d0cPhilipp Reisner	   that made it into the TL are already active */
1318265be2d09853d425ad14a61cda0ca63345613d0cPhilipp Reisner
1319265be2d09853d425ad14a61cda0ca63345613d0cPhilipp Reisner	drbd_req_make_private_bio(req, req->master_bio);
1320265be2d09853d425ad14a61cda0ca63345613d0cPhilipp Reisner	req->private_bio->bi_bdev = mdev->ldev->backing_bdev;
1321265be2d09853d425ad14a61cda0ca63345613d0cPhilipp Reisner	generic_make_request(req->private_bio);
1322265be2d09853d425ad14a61cda0ca63345613d0cPhilipp Reisner
1323265be2d09853d425ad14a61cda0ca63345613d0cPhilipp Reisner	return 1;
1324265be2d09853d425ad14a61cda0ca63345613d0cPhilipp Reisner}
1325265be2d09853d425ad14a61cda0ca63345613d0cPhilipp Reisner
1326b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisnerstatic int _drbd_may_sync_now(struct drbd_conf *mdev)
1327b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
1328b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	struct drbd_conf *odev = mdev;
1329b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1330b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	while (1) {
1331b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (odev->sync_conf.after == -1)
1332b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			return 1;
1333b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		odev = minor_to_mdev(odev->sync_conf.after);
1334841ce241fa355048f66172a47e356bb6e9159c9dAndreas Gruenbacher		if (!expect(odev))
1335841ce241fa355048f66172a47e356bb6e9159c9dAndreas Gruenbacher			return 1;
1336b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if ((odev->state.conn >= C_SYNC_SOURCE &&
1337b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		     odev->state.conn <= C_PAUSED_SYNC_T) ||
1338b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		    odev->state.aftr_isp || odev->state.peer_isp ||
1339b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		    odev->state.user_isp)
1340b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			return 0;
1341b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
1342b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
1343b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1344b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner/**
1345b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * _drbd_pause_after() - Pause resync on all devices that may not resync now
1346b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * @mdev:	DRBD device.
1347b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner *
1348b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * Called from process context only (admin command and after_state_ch).
1349b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner */
1350b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisnerstatic int _drbd_pause_after(struct drbd_conf *mdev)
1351b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
1352b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	struct drbd_conf *odev;
1353b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	int i, rv = 0;
1354b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1355b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	for (i = 0; i < minor_count; i++) {
1356b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		odev = minor_to_mdev(i);
1357b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (!odev)
1358b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			continue;
1359b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (odev->state.conn == C_STANDALONE && odev->state.disk == D_DISKLESS)
1360b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			continue;
1361b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (!_drbd_may_sync_now(odev))
1362b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			rv |= (__drbd_set_state(_NS(odev, aftr_isp, 1), CS_HARD, NULL)
1363b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			       != SS_NOTHING_TO_DO);
1364b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
1365b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1366b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	return rv;
1367b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
1368b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1369b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner/**
1370b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * _drbd_resume_next() - Resume resync on all devices that may resync now
1371b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * @mdev:	DRBD device.
1372b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner *
1373b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * Called from process context only (admin command and worker).
1374b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner */
1375b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisnerstatic int _drbd_resume_next(struct drbd_conf *mdev)
1376b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
1377b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	struct drbd_conf *odev;
1378b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	int i, rv = 0;
1379b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1380b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	for (i = 0; i < minor_count; i++) {
1381b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		odev = minor_to_mdev(i);
1382b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (!odev)
1383b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			continue;
1384b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (odev->state.conn == C_STANDALONE && odev->state.disk == D_DISKLESS)
1385b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			continue;
1386b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (odev->state.aftr_isp) {
1387b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			if (_drbd_may_sync_now(odev))
1388b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner				rv |= (__drbd_set_state(_NS(odev, aftr_isp, 0),
1389b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner							CS_HARD, NULL)
1390b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner				       != SS_NOTHING_TO_DO) ;
1391b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
1392b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
1393b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	return rv;
1394b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
1395b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1396b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisnervoid resume_next_sg(struct drbd_conf *mdev)
1397b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
1398b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	write_lock_irq(&global_state_lock);
1399b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	_drbd_resume_next(mdev);
1400b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	write_unlock_irq(&global_state_lock);
1401b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
1402b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1403b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisnervoid suspend_other_sg(struct drbd_conf *mdev)
1404b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
1405b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	write_lock_irq(&global_state_lock);
1406b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	_drbd_pause_after(mdev);
1407b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	write_unlock_irq(&global_state_lock);
1408b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
1409b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1410b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisnerstatic int sync_after_error(struct drbd_conf *mdev, int o_minor)
1411b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
1412b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	struct drbd_conf *odev;
1413b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1414b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (o_minor == -1)
1415b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		return NO_ERROR;
1416b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (o_minor < -1 || minor_to_mdev(o_minor) == NULL)
1417b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		return ERR_SYNC_AFTER;
1418b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1419b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	/* check for loops */
1420b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	odev = minor_to_mdev(o_minor);
1421b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	while (1) {
1422b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (odev == mdev)
1423b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			return ERR_SYNC_AFTER_CYCLE;
1424b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1425b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		/* dependency chain ends here, no cycles. */
1426b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (odev->sync_conf.after == -1)
1427b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			return NO_ERROR;
1428b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1429b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		/* follow the dependency chain */
1430b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		odev = minor_to_mdev(odev->sync_conf.after);
1431b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
1432b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
1433b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1434b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisnerint drbd_alter_sa(struct drbd_conf *mdev, int na)
1435b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
1436b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	int changes;
1437b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	int retcode;
1438b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1439b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	write_lock_irq(&global_state_lock);
1440b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	retcode = sync_after_error(mdev, na);
1441b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (retcode == NO_ERROR) {
1442b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		mdev->sync_conf.after = na;
1443b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		do {
1444b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			changes  = _drbd_pause_after(mdev);
1445b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			changes |= _drbd_resume_next(mdev);
1446b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		} while (changes);
1447b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
1448b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	write_unlock_irq(&global_state_lock);
1449b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	return retcode;
1450b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
1451b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
14529bd28d3c90c80c7ec46085de281b38f67331da41Lars Ellenbergvoid drbd_rs_controller_reset(struct drbd_conf *mdev)
14539bd28d3c90c80c7ec46085de281b38f67331da41Lars Ellenberg{
14549bd28d3c90c80c7ec46085de281b38f67331da41Lars Ellenberg	atomic_set(&mdev->rs_sect_in, 0);
14559bd28d3c90c80c7ec46085de281b38f67331da41Lars Ellenberg	atomic_set(&mdev->rs_sect_ev, 0);
14569bd28d3c90c80c7ec46085de281b38f67331da41Lars Ellenberg	mdev->rs_in_flight = 0;
14579bd28d3c90c80c7ec46085de281b38f67331da41Lars Ellenberg	mdev->rs_planed = 0;
14589bd28d3c90c80c7ec46085de281b38f67331da41Lars Ellenberg	spin_lock(&mdev->peer_seq_lock);
14599bd28d3c90c80c7ec46085de281b38f67331da41Lars Ellenberg	fifo_set(&mdev->rs_plan_s, 0);
14609bd28d3c90c80c7ec46085de281b38f67331da41Lars Ellenberg	spin_unlock(&mdev->peer_seq_lock);
14619bd28d3c90c80c7ec46085de281b38f67331da41Lars Ellenberg}
14629bd28d3c90c80c7ec46085de281b38f67331da41Lars Ellenberg
1463b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner/**
1464b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * drbd_start_resync() - Start the resync process
1465b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * @mdev:	DRBD device.
1466b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * @side:	Either C_SYNC_SOURCE or C_SYNC_TARGET
1467b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner *
1468b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * This function might bring you directly into one of the
1469b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner * C_PAUSED_SYNC_* states.
1470b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner */
1471b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisnervoid drbd_start_resync(struct drbd_conf *mdev, enum drbd_conns side)
1472b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
1473b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	union drbd_state ns;
1474b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	int r;
1475b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1476c4752ef1284519c3baa1c3b19df34a80b4905245Philipp Reisner	if (mdev->state.conn >= C_SYNC_SOURCE && mdev->state.conn < C_AHEAD) {
1477b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		dev_err(DEV, "Resync already running!\n");
1478b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		return;
1479b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
1480b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
148159817f4fab6a165ba83ce399464ba38432db8233Philipp Reisner	if (mdev->state.conn < C_AHEAD) {
148259817f4fab6a165ba83ce399464ba38432db8233Philipp Reisner		/* In case a previous resync run was aborted by an IO error/detach on the peer. */
148359817f4fab6a165ba83ce399464ba38432db8233Philipp Reisner		drbd_rs_cancel_all(mdev);
148459817f4fab6a165ba83ce399464ba38432db8233Philipp Reisner		/* This should be done when we abort the resync. We definitely do not
148559817f4fab6a165ba83ce399464ba38432db8233Philipp Reisner		   want to have this for connections going back and forth between
148659817f4fab6a165ba83ce399464ba38432db8233Philipp Reisner		   Ahead/Behind and SyncSource/SyncTarget */
148759817f4fab6a165ba83ce399464ba38432db8233Philipp Reisner	}
1488b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1489b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (side == C_SYNC_TARGET) {
1490b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		/* Since application IO was locked out during C_WF_BITMAP_T and
1491b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		   C_WF_SYNC_UUID we are still unmodified. Before going to C_SYNC_TARGET
1492b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		   we check that we might make the data inconsistent. */
1493b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		r = drbd_khelper(mdev, "before-resync-target");
1494b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		r = (r >> 8) & 0xff;
1495b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (r > 0) {
1496b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			dev_info(DEV, "before-resync-target handler returned %d, "
1497b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			     "dropping connection.\n", r);
1498b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			drbd_force_state(mdev, NS(conn, C_DISCONNECTING));
1499b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			return;
1500b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
150109b9e7979378fe070784de20e50bb1d42aa643abPhilipp Reisner	} else /* C_SYNC_SOURCE */ {
150209b9e7979378fe070784de20e50bb1d42aa643abPhilipp Reisner		r = drbd_khelper(mdev, "before-resync-source");
150309b9e7979378fe070784de20e50bb1d42aa643abPhilipp Reisner		r = (r >> 8) & 0xff;
150409b9e7979378fe070784de20e50bb1d42aa643abPhilipp Reisner		if (r > 0) {
150509b9e7979378fe070784de20e50bb1d42aa643abPhilipp Reisner			if (r == 3) {
150609b9e7979378fe070784de20e50bb1d42aa643abPhilipp Reisner				dev_info(DEV, "before-resync-source handler returned %d, "
150709b9e7979378fe070784de20e50bb1d42aa643abPhilipp Reisner					 "ignoring. Old userland tools?", r);
150809b9e7979378fe070784de20e50bb1d42aa643abPhilipp Reisner			} else {
150909b9e7979378fe070784de20e50bb1d42aa643abPhilipp Reisner				dev_info(DEV, "before-resync-source handler returned %d, "
151009b9e7979378fe070784de20e50bb1d42aa643abPhilipp Reisner					 "dropping connection.\n", r);
151109b9e7979378fe070784de20e50bb1d42aa643abPhilipp Reisner				drbd_force_state(mdev, NS(conn, C_DISCONNECTING));
151209b9e7979378fe070784de20e50bb1d42aa643abPhilipp Reisner				return;
151309b9e7979378fe070784de20e50bb1d42aa643abPhilipp Reisner			}
151409b9e7979378fe070784de20e50bb1d42aa643abPhilipp Reisner		}
1515b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
1516b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1517b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	drbd_state_lock(mdev);
1518b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1519b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (!get_ldev_if_state(mdev, D_NEGOTIATING)) {
1520b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		drbd_state_unlock(mdev);
1521b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		return;
1522b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
1523b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1524b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	write_lock_irq(&global_state_lock);
1525b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	ns = mdev->state;
1526b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1527b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	ns.aftr_isp = !_drbd_may_sync_now(mdev);
1528b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1529b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	ns.conn = side;
1530b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1531b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (side == C_SYNC_TARGET)
1532b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		ns.disk = D_INCONSISTENT;
1533b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	else /* side == C_SYNC_SOURCE */
1534b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		ns.pdsk = D_INCONSISTENT;
1535b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1536b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	r = __drbd_set_state(mdev, ns, CS_VERBOSE, NULL);
1537b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	ns = mdev->state;
1538b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1539b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (ns.conn < C_CONNECTED)
1540b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		r = SS_UNKNOWN_ERROR;
1541b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1542b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (r == SS_SUCCESS) {
15431d7734a0df02ff5068ff8baa1447c7baee601db1Lars Ellenberg		unsigned long tw = drbd_bm_total_weight(mdev);
15441d7734a0df02ff5068ff8baa1447c7baee601db1Lars Ellenberg		unsigned long now = jiffies;
15451d7734a0df02ff5068ff8baa1447c7baee601db1Lars Ellenberg		int i;
15461d7734a0df02ff5068ff8baa1447c7baee601db1Lars Ellenberg
1547b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		mdev->rs_failed    = 0;
1548b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		mdev->rs_paused    = 0;
1549b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		mdev->rs_same_csum = 0;
15500f0601f4ea2f53cfd8bcae060fb03d9bbde070ecLars Ellenberg		mdev->rs_last_events = 0;
15510f0601f4ea2f53cfd8bcae060fb03d9bbde070ecLars Ellenberg		mdev->rs_last_sect_ev = 0;
15521d7734a0df02ff5068ff8baa1447c7baee601db1Lars Ellenberg		mdev->rs_total     = tw;
15531d7734a0df02ff5068ff8baa1447c7baee601db1Lars Ellenberg		mdev->rs_start     = now;
15541d7734a0df02ff5068ff8baa1447c7baee601db1Lars Ellenberg		for (i = 0; i < DRBD_SYNC_MARKS; i++) {
15551d7734a0df02ff5068ff8baa1447c7baee601db1Lars Ellenberg			mdev->rs_mark_left[i] = tw;
15561d7734a0df02ff5068ff8baa1447c7baee601db1Lars Ellenberg			mdev->rs_mark_time[i] = now;
15571d7734a0df02ff5068ff8baa1447c7baee601db1Lars Ellenberg		}
1558b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		_drbd_pause_after(mdev);
1559b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
1560b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	write_unlock_irq(&global_state_lock);
15615a22db8968a69bec835d1ed9a96ab3381719e0c0Lars Ellenberg
1562b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	if (r == SS_SUCCESS) {
1563b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		dev_info(DEV, "Began resync as %s (will sync %lu KB [%lu bits set]).\n",
1564b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		     drbd_conn_str(ns.conn),
1565b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		     (unsigned long) mdev->rs_total << (BM_BLOCK_SHIFT-10),
1566b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		     (unsigned long) mdev->rs_total);
15676c922ed543bee1bc6685ade07be59f3fa49a7288Lars Ellenberg		if (side == C_SYNC_TARGET)
15686c922ed543bee1bc6685ade07be59f3fa49a7288Lars Ellenberg			mdev->bm_resync_fo = 0;
15696c922ed543bee1bc6685ade07be59f3fa49a7288Lars Ellenberg
15706c922ed543bee1bc6685ade07be59f3fa49a7288Lars Ellenberg		/* Since protocol 96, we must serialize drbd_gen_and_send_sync_uuid
15716c922ed543bee1bc6685ade07be59f3fa49a7288Lars Ellenberg		 * with w_send_oos, or the sync target will get confused as to
15726c922ed543bee1bc6685ade07be59f3fa49a7288Lars Ellenberg		 * how much bits to resync.  We cannot do that always, because for an
15736c922ed543bee1bc6685ade07be59f3fa49a7288Lars Ellenberg		 * empty resync and protocol < 95, we need to do it here, as we call
15746c922ed543bee1bc6685ade07be59f3fa49a7288Lars Ellenberg		 * drbd_resync_finished from here in that case.
15756c922ed543bee1bc6685ade07be59f3fa49a7288Lars Ellenberg		 * We drbd_gen_and_send_sync_uuid here for protocol < 96,
15766c922ed543bee1bc6685ade07be59f3fa49a7288Lars Ellenberg		 * and from after_state_ch otherwise. */
157731890f4ab299c4116cf0a104ca9ce4f9ca2c5da0Philipp Reisner		if (side == C_SYNC_SOURCE && mdev->tconn->agreed_pro_version < 96)
15786c922ed543bee1bc6685ade07be59f3fa49a7288Lars Ellenberg			drbd_gen_and_send_sync_uuid(mdev);
1579b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
158031890f4ab299c4116cf0a104ca9ce4f9ca2c5da0Philipp Reisner		if (mdev->tconn->agreed_pro_version < 95 && mdev->rs_total == 0) {
1581af85e8e83d160f72a10e4467852646ac08614260Lars Ellenberg			/* This still has a race (about when exactly the peers
1582af85e8e83d160f72a10e4467852646ac08614260Lars Ellenberg			 * detect connection loss) that can lead to a full sync
1583af85e8e83d160f72a10e4467852646ac08614260Lars Ellenberg			 * on next handshake. In 8.3.9 we fixed this with explicit
1584af85e8e83d160f72a10e4467852646ac08614260Lars Ellenberg			 * resync-finished notifications, but the fix
1585af85e8e83d160f72a10e4467852646ac08614260Lars Ellenberg			 * introduces a protocol change.  Sleeping for some
1586af85e8e83d160f72a10e4467852646ac08614260Lars Ellenberg			 * time longer than the ping interval + timeout on the
1587af85e8e83d160f72a10e4467852646ac08614260Lars Ellenberg			 * SyncSource, to give the SyncTarget the chance to
1588af85e8e83d160f72a10e4467852646ac08614260Lars Ellenberg			 * detect connection loss, then waiting for a ping
1589af85e8e83d160f72a10e4467852646ac08614260Lars Ellenberg			 * response (implicit in drbd_resync_finished) reduces
1590af85e8e83d160f72a10e4467852646ac08614260Lars Ellenberg			 * the race considerably, but does not solve it. */
1591af85e8e83d160f72a10e4467852646ac08614260Lars Ellenberg			if (side == C_SYNC_SOURCE)
1592af85e8e83d160f72a10e4467852646ac08614260Lars Ellenberg				schedule_timeout_interruptible(
159389e58e755e37137135c28a90c93be1b28faff485Philipp Reisner					mdev->tconn->net_conf->ping_int * HZ +
159489e58e755e37137135c28a90c93be1b28faff485Philipp Reisner					mdev->tconn->net_conf->ping_timeo*HZ/9);
1595b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			drbd_resync_finished(mdev);
1596b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
1597b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
15989bd28d3c90c80c7ec46085de281b38f67331da41Lars Ellenberg		drbd_rs_controller_reset(mdev);
1599b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		/* ns.conn may already be != mdev->state.conn,
1600b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		 * we may have been paused in between, or become paused until
1601b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		 * the timer triggers.
1602b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		 * No matter, that is handled in resync_timer_fn() */
1603b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (ns.conn == C_SYNC_TARGET)
1604b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			mod_timer(&mdev->resync_timer, jiffies);
1605b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1606b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		drbd_md_sync(mdev);
1607b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
16085a22db8968a69bec835d1ed9a96ab3381719e0c0Lars Ellenberg	put_ldev(mdev);
1609d0c3f60f3611ceac9b1e4fdffd1497337568e7cbPhilipp Reisner	drbd_state_unlock(mdev);
1610b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
1611b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1612b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisnerint drbd_worker(struct drbd_thread *thi)
1613b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner{
1614b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	struct drbd_conf *mdev = thi->mdev;
1615b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	struct drbd_work *w = NULL;
1616b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	LIST_HEAD(work_list);
1617b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	int intr = 0, i;
1618b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1619b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	sprintf(current->comm, "drbd%d_worker", mdev_to_minor(mdev));
1620b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1621e77a0a5cc1e6961f485b5623ef42f3b910969675Andreas Gruenbacher	while (get_t_state(thi) == RUNNING) {
1622b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		drbd_thread_current_set_cpu(mdev);
1623b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1624e42325a57606396539807ff55c24febda39f8d01Philipp Reisner		if (down_trylock(&mdev->tconn->data.work.s)) {
1625e42325a57606396539807ff55c24febda39f8d01Philipp Reisner			mutex_lock(&mdev->tconn->data.mutex);
1626e42325a57606396539807ff55c24febda39f8d01Philipp Reisner			if (mdev->tconn->data.socket && !mdev->tconn->net_conf->no_cork)
1627e42325a57606396539807ff55c24febda39f8d01Philipp Reisner				drbd_tcp_uncork(mdev->tconn->data.socket);
1628e42325a57606396539807ff55c24febda39f8d01Philipp Reisner			mutex_unlock(&mdev->tconn->data.mutex);
1629b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1630e42325a57606396539807ff55c24febda39f8d01Philipp Reisner			intr = down_interruptible(&mdev->tconn->data.work.s);
1631b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1632e42325a57606396539807ff55c24febda39f8d01Philipp Reisner			mutex_lock(&mdev->tconn->data.mutex);
1633e42325a57606396539807ff55c24febda39f8d01Philipp Reisner			if (mdev->tconn->data.socket  && !mdev->tconn->net_conf->no_cork)
1634e42325a57606396539807ff55c24febda39f8d01Philipp Reisner				drbd_tcp_cork(mdev->tconn->data.socket);
1635e42325a57606396539807ff55c24febda39f8d01Philipp Reisner			mutex_unlock(&mdev->tconn->data.mutex);
1636b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
1637b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1638b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (intr) {
1639b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			D_ASSERT(intr == -EINTR);
1640b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			flush_signals(current);
1641841ce241fa355048f66172a47e356bb6e9159c9dAndreas Gruenbacher			if (!expect(get_t_state(thi) != RUNNING))
1642b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner				continue;
1643b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			break;
1644b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
1645b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1646e77a0a5cc1e6961f485b5623ef42f3b910969675Andreas Gruenbacher		if (get_t_state(thi) != RUNNING)
1647b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			break;
1648b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		/* With this break, we have done a down() but not consumed
1649b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		   the entry from the list. The cleanup code takes care of
1650b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		   this...   */
1651b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1652b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		w = NULL;
1653e42325a57606396539807ff55c24febda39f8d01Philipp Reisner		spin_lock_irq(&mdev->tconn->data.work.q_lock);
1654e42325a57606396539807ff55c24febda39f8d01Philipp Reisner		if (!expect(!list_empty(&mdev->tconn->data.work.q))) {
1655b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			/* something terribly wrong in our logic.
1656b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			 * we were able to down() the semaphore,
1657b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			 * but the list is empty... doh.
1658b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			 *
1659b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			 * what is the best thing to do now?
1660b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			 * try again from scratch, restarting the receiver,
1661b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			 * asender, whatnot? could break even more ugly,
1662b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			 * e.g. when we are primary, but no good local data.
1663b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			 *
1664b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			 * I'll try to get away just starting over this loop.
1665b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			 */
1666e42325a57606396539807ff55c24febda39f8d01Philipp Reisner			spin_unlock_irq(&mdev->tconn->data.work.q_lock);
1667b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			continue;
1668b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
1669e42325a57606396539807ff55c24febda39f8d01Philipp Reisner		w = list_entry(mdev->tconn->data.work.q.next, struct drbd_work, list);
1670b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		list_del_init(&w->list);
1671e42325a57606396539807ff55c24febda39f8d01Philipp Reisner		spin_unlock_irq(&mdev->tconn->data.work.q_lock);
1672b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1673b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		if (!w->cb(mdev, w, mdev->state.conn < C_CONNECTED)) {
1674b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			/* dev_warn(DEV, "worker: a callback failed! \n"); */
1675b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			if (mdev->state.conn >= C_CONNECTED)
1676b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner				drbd_force_state(mdev,
1677b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner						NS(conn, C_NETWORK_FAILURE));
1678b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
1679b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
1680b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	D_ASSERT(test_bit(DEVICE_DYING, &mdev->flags));
1681b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	D_ASSERT(test_bit(CONFIG_PENDING, &mdev->flags));
1682b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1683e42325a57606396539807ff55c24febda39f8d01Philipp Reisner	spin_lock_irq(&mdev->tconn->data.work.q_lock);
1684b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	i = 0;
1685e42325a57606396539807ff55c24febda39f8d01Philipp Reisner	while (!list_empty(&mdev->tconn->data.work.q)) {
1686e42325a57606396539807ff55c24febda39f8d01Philipp Reisner		list_splice_init(&mdev->tconn->data.work.q, &work_list);
1687e42325a57606396539807ff55c24febda39f8d01Philipp Reisner		spin_unlock_irq(&mdev->tconn->data.work.q_lock);
1688b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1689b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		while (!list_empty(&work_list)) {
1690b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			w = list_entry(work_list.next, struct drbd_work, list);
1691b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			list_del_init(&w->list);
1692b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			w->cb(mdev, w, 1);
1693b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner			i++; /* dead debugging code */
1694b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner		}
1695b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1696e42325a57606396539807ff55c24febda39f8d01Philipp Reisner		spin_lock_irq(&mdev->tconn->data.work.q_lock);
1697b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	}
1698e42325a57606396539807ff55c24febda39f8d01Philipp Reisner	sema_init(&mdev->tconn->data.work.s, 0);
1699b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	/* DANGEROUS race: if someone did queue his work within the spinlock,
1700b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	 * but up() ed outside the spinlock, we could get an up() on the
1701b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	 * semaphore without corresponding list entry.
1702b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	 * So don't do that.
1703b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	 */
1704e42325a57606396539807ff55c24febda39f8d01Philipp Reisner	spin_unlock_irq(&mdev->tconn->data.work.q_lock);
1705b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1706b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	D_ASSERT(mdev->state.disk == D_DISKLESS && mdev->state.conn == C_STANDALONE);
1707b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	/* _drbd_set_state only uses stop_nowait.
1708e6b3ea83bc72e126247b241c1164794a644d6fdcPhilipp Reisner	 * wait here for the exiting receiver. */
1709e6b3ea83bc72e126247b241c1164794a644d6fdcPhilipp Reisner	drbd_thread_stop(&mdev->tconn->receiver);
1710b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	drbd_mdev_cleanup(mdev);
1711b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1712b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	dev_info(DEV, "worker terminated\n");
1713b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1714b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	clear_bit(DEVICE_DYING, &mdev->flags);
1715b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	clear_bit(CONFIG_PENDING, &mdev->flags);
1716b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	wake_up(&mdev->state_wait);
1717b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner
1718b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner	return 0;
1719b411b3637fa71fce9cf2acf0639009500f5892fePhilipp Reisner}
1720