blob: c674f17773a67aeb7c44d5b7671c88a1f2308067 [file] [log] [blame]
Philipp Reisnerb411b362009-09-25 16:07:19 -07001/*
2 drbd_worker.c
3
4 This file is part of DRBD by Philipp Reisner and Lars Ellenberg.
5
6 Copyright (C) 2001-2008, LINBIT Information Technologies GmbH.
7 Copyright (C) 1999-2008, Philipp Reisner <philipp.reisner@linbit.com>.
8 Copyright (C) 2002-2008, Lars Ellenberg <lars.ellenberg@linbit.com>.
9
10 drbd is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2, or (at your option)
13 any later version.
14
15 drbd is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with drbd; see the file COPYING. If not, write to
22 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23
24 */
25
Philipp Reisnerb411b362009-09-25 16:07:19 -070026#include <linux/module.h>
Philipp Reisnerb411b362009-09-25 16:07:19 -070027#include <linux/drbd.h>
28#include <linux/sched.h>
Philipp Reisnerb411b362009-09-25 16:07:19 -070029#include <linux/wait.h>
30#include <linux/mm.h>
31#include <linux/memcontrol.h>
32#include <linux/mm_inline.h>
33#include <linux/slab.h>
34#include <linux/random.h>
Philipp Reisnerb411b362009-09-25 16:07:19 -070035#include <linux/string.h>
36#include <linux/scatterlist.h>
37
38#include "drbd_int.h"
39#include "drbd_req.h"
Philipp Reisnerb411b362009-09-25 16:07:19 -070040
Philipp Reisner00d56942011-02-09 18:09:48 +010041static int w_make_ov_request(struct drbd_work *w, int cancel);
Philipp Reisnerb411b362009-09-25 16:07:19 -070042
43
Andreas Gruenbacherc5a91612011-01-25 17:33:38 +010044/* endio handlers:
45 * drbd_md_io_complete (defined here)
Andreas Gruenbacherfcefa622011-02-17 16:46:59 +010046 * drbd_request_endio (defined here)
47 * drbd_peer_request_endio (defined here)
Andreas Gruenbacherc5a91612011-01-25 17:33:38 +010048 * bm_async_io_complete (defined in drbd_bitmap.c)
49 *
Philipp Reisnerb411b362009-09-25 16:07:19 -070050 * For all these callbacks, note the following:
51 * The callbacks will be called in irq context by the IDE drivers,
52 * and in Softirqs/Tasklets/BH context by the SCSI drivers.
53 * Try to get the locking right :)
54 *
55 */
56
57
58/* About the global_state_lock
59 Each state transition on an device holds a read lock. In case we have
Andreas Gruenbacher95f8efd2011-05-12 11:15:34 +020060 to evaluate the resync after dependencies, we grab a write lock, because
Philipp Reisnerb411b362009-09-25 16:07:19 -070061 we need stable states on all devices for that. */
62rwlock_t global_state_lock;
63
64/* used for synchronous meta data and bitmap IO
65 * submitted by drbd_md_sync_page_io()
66 */
67void drbd_md_io_complete(struct bio *bio, int error)
68{
69 struct drbd_md_io *md_io;
Philipp Reisnercdfda632011-07-05 15:38:59 +020070 struct drbd_conf *mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -070071
72 md_io = (struct drbd_md_io *)bio->bi_private;
Philipp Reisnercdfda632011-07-05 15:38:59 +020073 mdev = container_of(md_io, struct drbd_conf, md_io);
74
Philipp Reisnerb411b362009-09-25 16:07:19 -070075 md_io->error = error;
76
Philipp Reisner0cfac5d2011-11-10 12:12:52 +010077 /* We grabbed an extra reference in _drbd_md_sync_page_io() to be able
78 * to timeout on the lower level device, and eventually detach from it.
79 * If this io completion runs after that timeout expired, this
80 * drbd_md_put_buffer() may allow us to finally try and re-attach.
81 * During normal operation, this only puts that extra reference
82 * down to 1 again.
83 * Make sure we first drop the reference, and only then signal
84 * completion, or we may (in drbd_al_read_log()) cycle so fast into the
85 * next drbd_md_sync_page_io(), that we trigger the
86 * ASSERT(atomic_read(&mdev->md_io_in_use) == 1) there.
87 */
88 drbd_md_put_buffer(mdev);
Philipp Reisnercdfda632011-07-05 15:38:59 +020089 md_io->done = 1;
90 wake_up(&mdev->misc_wait);
91 bio_put(bio);
Philipp Reisnercdfda632011-07-05 15:38:59 +020092 put_ldev(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -070093}
94
95/* reads on behalf of the partner,
96 * "submitted" by the receiver
97 */
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +010098void drbd_endio_read_sec_final(struct drbd_peer_request *peer_req) __releases(local)
Philipp Reisnerb411b362009-09-25 16:07:19 -070099{
100 unsigned long flags = 0;
Philipp Reisnera21e9292011-02-08 15:08:49 +0100101 struct drbd_conf *mdev = peer_req->w.mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700102
Philipp Reisner87eeee42011-01-19 14:16:30 +0100103 spin_lock_irqsave(&mdev->tconn->req_lock, flags);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100104 mdev->read_cnt += peer_req->i.size >> 9;
105 list_del(&peer_req->w.list);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700106 if (list_empty(&mdev->read_ee))
107 wake_up(&mdev->ee_wait);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100108 if (test_bit(__EE_WAS_ERROR, &peer_req->flags))
Lars Ellenberg0c849662012-07-30 09:07:28 +0200109 __drbd_chk_io_error(mdev, DRBD_IO_ERROR);
Philipp Reisner87eeee42011-01-19 14:16:30 +0100110 spin_unlock_irqrestore(&mdev->tconn->req_lock, flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700111
Lars Ellenbergd5b27b02011-11-14 15:42:37 +0100112 drbd_queue_work(&mdev->tconn->sender_work, &peer_req->w);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700113 put_ldev(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700114}
115
116/* writes on behalf of the partner, or resync writes,
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200117 * "submitted" by the receiver, final stage. */
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100118static void drbd_endio_write_sec_final(struct drbd_peer_request *peer_req) __releases(local)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700119{
120 unsigned long flags = 0;
Philipp Reisnera21e9292011-02-08 15:08:49 +0100121 struct drbd_conf *mdev = peer_req->w.mdev;
Lars Ellenberg181286a2011-03-31 15:18:56 +0200122 struct drbd_interval i;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700123 int do_wake;
Andreas Gruenbacher579b57e2011-01-13 18:40:57 +0100124 u64 block_id;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700125 int do_al_complete_io;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700126
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100127 /* after we moved peer_req to done_ee,
Philipp Reisnerb411b362009-09-25 16:07:19 -0700128 * we may no longer access it,
129 * it may be freed/reused already!
130 * (as soon as we release the req_lock) */
Lars Ellenberg181286a2011-03-31 15:18:56 +0200131 i = peer_req->i;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100132 do_al_complete_io = peer_req->flags & EE_CALL_AL_COMPLETE_IO;
133 block_id = peer_req->block_id;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700134
Philipp Reisner87eeee42011-01-19 14:16:30 +0100135 spin_lock_irqsave(&mdev->tconn->req_lock, flags);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100136 mdev->writ_cnt += peer_req->i.size >> 9;
137 list_del(&peer_req->w.list); /* has been on active_ee or sync_ee */
138 list_add_tail(&peer_req->w.list, &mdev->done_ee);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700139
Andreas Gruenbacherbb3bfe92011-01-21 15:59:23 +0100140 /*
Andreas Gruenbacher5e472262011-01-27 14:42:51 +0100141 * Do not remove from the write_requests tree here: we did not send the
Andreas Gruenbacherbb3bfe92011-01-21 15:59:23 +0100142 * Ack yet and did not wake possibly waiting conflicting requests.
143 * Removed from the tree from "drbd_process_done_ee" within the
144 * appropriate w.cb (e_end_block/e_end_resync_block) or from
145 * _drbd_clear_done_ee.
146 */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700147
Andreas Gruenbacher579b57e2011-01-13 18:40:57 +0100148 do_wake = list_empty(block_id == ID_SYNCER ? &mdev->sync_ee : &mdev->active_ee);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700149
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100150 if (test_bit(__EE_WAS_ERROR, &peer_req->flags))
Lars Ellenberg0c849662012-07-30 09:07:28 +0200151 __drbd_chk_io_error(mdev, DRBD_IO_ERROR);
Philipp Reisner87eeee42011-01-19 14:16:30 +0100152 spin_unlock_irqrestore(&mdev->tconn->req_lock, flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700153
Andreas Gruenbacher579b57e2011-01-13 18:40:57 +0100154 if (block_id == ID_SYNCER)
Lars Ellenberg181286a2011-03-31 15:18:56 +0200155 drbd_rs_complete_io(mdev, i.sector);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700156
157 if (do_wake)
158 wake_up(&mdev->ee_wait);
159
160 if (do_al_complete_io)
Lars Ellenberg181286a2011-03-31 15:18:56 +0200161 drbd_al_complete_io(mdev, &i);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700162
Philipp Reisner0625ac12011-02-07 14:49:19 +0100163 wake_asender(mdev->tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700164 put_ldev(mdev);
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200165}
Philipp Reisnerb411b362009-09-25 16:07:19 -0700166
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200167/* writes on behalf of the partner, or resync writes,
168 * "submitted" by the receiver.
169 */
Andreas Gruenbacherfcefa622011-02-17 16:46:59 +0100170void drbd_peer_request_endio(struct bio *bio, int error)
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200171{
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100172 struct drbd_peer_request *peer_req = bio->bi_private;
Philipp Reisnera21e9292011-02-08 15:08:49 +0100173 struct drbd_conf *mdev = peer_req->w.mdev;
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200174 int uptodate = bio_flagged(bio, BIO_UPTODATE);
175 int is_write = bio_data_dir(bio) == WRITE;
176
Lars Ellenberg07194272010-12-20 15:38:07 +0100177 if (error && __ratelimit(&drbd_ratelimit_state))
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200178 dev_warn(DEV, "%s: error=%d s=%llus\n",
179 is_write ? "write" : "read", error,
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100180 (unsigned long long)peer_req->i.sector);
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200181 if (!error && !uptodate) {
Lars Ellenberg07194272010-12-20 15:38:07 +0100182 if (__ratelimit(&drbd_ratelimit_state))
183 dev_warn(DEV, "%s: setting error to -EIO s=%llus\n",
184 is_write ? "write" : "read",
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100185 (unsigned long long)peer_req->i.sector);
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200186 /* strange behavior of some lower level drivers...
187 * fail the request by clearing the uptodate flag,
188 * but do not return any error?! */
189 error = -EIO;
190 }
191
192 if (error)
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100193 set_bit(__EE_WAS_ERROR, &peer_req->flags);
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200194
195 bio_put(bio); /* no need for the bio anymore */
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100196 if (atomic_dec_and_test(&peer_req->pending_bios)) {
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200197 if (is_write)
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100198 drbd_endio_write_sec_final(peer_req);
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200199 else
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100200 drbd_endio_read_sec_final(peer_req);
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200201 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700202}
203
204/* read, readA or write requests on R_PRIMARY coming from drbd_make_request
205 */
Andreas Gruenbacherfcefa622011-02-17 16:46:59 +0100206void drbd_request_endio(struct bio *bio, int error)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700207{
Lars Ellenberga1154132010-11-13 20:42:29 +0100208 unsigned long flags;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700209 struct drbd_request *req = bio->bi_private;
Philipp Reisnera21e9292011-02-08 15:08:49 +0100210 struct drbd_conf *mdev = req->w.mdev;
Lars Ellenberga1154132010-11-13 20:42:29 +0100211 struct bio_and_error m;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700212 enum drbd_req_event what;
213 int uptodate = bio_flagged(bio, BIO_UPTODATE);
214
Philipp Reisnerb411b362009-09-25 16:07:19 -0700215 if (!error && !uptodate) {
216 dev_warn(DEV, "p %s: setting error to -EIO\n",
217 bio_data_dir(bio) == WRITE ? "write" : "read");
218 /* strange behavior of some lower level drivers...
219 * fail the request by clearing the uptodate flag,
220 * but do not return any error?! */
221 error = -EIO;
222 }
223
Philipp Reisnerb411b362009-09-25 16:07:19 -0700224 /* to avoid recursion in __req_mod */
225 if (unlikely(error)) {
226 what = (bio_data_dir(bio) == WRITE)
Andreas Gruenbacher8554df12011-01-25 15:37:43 +0100227 ? WRITE_COMPLETED_WITH_ERROR
Lars Ellenberg5c3c7e62010-04-10 02:10:09 +0200228 : (bio_rw(bio) == READ)
Andreas Gruenbacher8554df12011-01-25 15:37:43 +0100229 ? READ_COMPLETED_WITH_ERROR
230 : READ_AHEAD_COMPLETED_WITH_ERROR;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700231 } else
Andreas Gruenbacher8554df12011-01-25 15:37:43 +0100232 what = COMPLETED_OK;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700233
234 bio_put(req->private_bio);
235 req->private_bio = ERR_PTR(error);
236
Lars Ellenberga1154132010-11-13 20:42:29 +0100237 /* not req_mod(), we need irqsave here! */
Philipp Reisner87eeee42011-01-19 14:16:30 +0100238 spin_lock_irqsave(&mdev->tconn->req_lock, flags);
Lars Ellenberga1154132010-11-13 20:42:29 +0100239 __req_mod(req, what, &m);
Philipp Reisner87eeee42011-01-19 14:16:30 +0100240 spin_unlock_irqrestore(&mdev->tconn->req_lock, flags);
Lars Ellenberg24153082012-03-26 17:06:29 +0200241 put_ldev(mdev);
Lars Ellenberga1154132010-11-13 20:42:29 +0100242
243 if (m.bio)
244 complete_master_bio(mdev, &m);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700245}
246
Andreas Gruenbacherf6ffca92011-02-04 15:30:34 +0100247void drbd_csum_ee(struct drbd_conf *mdev, struct crypto_hash *tfm,
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100248 struct drbd_peer_request *peer_req, void *digest)
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200249{
250 struct hash_desc desc;
251 struct scatterlist sg;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100252 struct page *page = peer_req->pages;
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200253 struct page *tmp;
254 unsigned len;
255
256 desc.tfm = tfm;
257 desc.flags = 0;
258
259 sg_init_table(&sg, 1);
260 crypto_hash_init(&desc);
261
262 while ((tmp = page_chain_next(page))) {
263 /* all but the last page will be fully used */
264 sg_set_page(&sg, page, PAGE_SIZE, 0);
265 crypto_hash_update(&desc, &sg, sg.length);
266 page = tmp;
267 }
268 /* and now the last, possibly only partially used page */
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100269 len = peer_req->i.size & (PAGE_SIZE - 1);
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200270 sg_set_page(&sg, page, len ?: PAGE_SIZE, 0);
271 crypto_hash_update(&desc, &sg, sg.length);
272 crypto_hash_final(&desc, digest);
273}
274
275void drbd_csum_bio(struct drbd_conf *mdev, struct crypto_hash *tfm, struct bio *bio, void *digest)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700276{
277 struct hash_desc desc;
278 struct scatterlist sg;
279 struct bio_vec *bvec;
280 int i;
281
282 desc.tfm = tfm;
283 desc.flags = 0;
284
285 sg_init_table(&sg, 1);
286 crypto_hash_init(&desc);
287
Lars Ellenberg4b8514e2012-03-26 16:12:49 +0200288 bio_for_each_segment(bvec, bio, i) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700289 sg_set_page(&sg, bvec->bv_page, bvec->bv_len, bvec->bv_offset);
290 crypto_hash_update(&desc, &sg, sg.length);
291 }
292 crypto_hash_final(&desc, digest);
293}
294
Lars Ellenberg9676c762011-02-22 14:02:31 +0100295/* MAYBE merge common code with w_e_end_ov_req */
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +0100296static int w_e_send_csum(struct drbd_work *w, int cancel)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700297{
Philipp Reisner00d56942011-02-09 18:09:48 +0100298 struct drbd_peer_request *peer_req = container_of(w, struct drbd_peer_request, w);
299 struct drbd_conf *mdev = w->mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700300 int digest_size;
301 void *digest;
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +0100302 int err = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700303
Lars Ellenberg53ea4332011-03-08 17:11:40 +0100304 if (unlikely(cancel))
305 goto out;
306
Lars Ellenberg9676c762011-02-22 14:02:31 +0100307 if (unlikely((peer_req->flags & EE_WAS_ERROR) != 0))
Lars Ellenberg53ea4332011-03-08 17:11:40 +0100308 goto out;
309
Lars Ellenbergf3990022011-03-23 14:31:09 +0100310 digest_size = crypto_hash_digestsize(mdev->tconn->csums_tfm);
Lars Ellenberg53ea4332011-03-08 17:11:40 +0100311 digest = kmalloc(digest_size, GFP_NOIO);
312 if (digest) {
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100313 sector_t sector = peer_req->i.sector;
314 unsigned int size = peer_req->i.size;
Lars Ellenbergf3990022011-03-23 14:31:09 +0100315 drbd_csum_ee(mdev, mdev->tconn->csums_tfm, peer_req, digest);
Lars Ellenberg9676c762011-02-22 14:02:31 +0100316 /* Free peer_req and pages before send.
Lars Ellenberg53ea4332011-03-08 17:11:40 +0100317 * In case we block on congestion, we could otherwise run into
318 * some distributed deadlock, if the other side blocks on
319 * congestion as well, because our receiver blocks in
Andreas Gruenbacherc37c8ec2011-04-07 21:02:09 +0200320 * drbd_alloc_pages due to pp_in_use > max_buffers. */
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +0200321 drbd_free_peer_req(mdev, peer_req);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100322 peer_req = NULL;
Lars Ellenberg53ea4332011-03-08 17:11:40 +0100323 inc_rs_pending(mdev);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +0100324 err = drbd_send_drequest_csum(mdev, sector, size,
Andreas Gruenbacherdb1b0b72011-03-16 01:37:21 +0100325 digest, digest_size,
326 P_CSUM_RS_REQUEST);
Lars Ellenberg53ea4332011-03-08 17:11:40 +0100327 kfree(digest);
328 } else {
329 dev_err(DEV, "kmalloc() of digest failed.\n");
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +0100330 err = -ENOMEM;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700331 }
332
Lars Ellenberg53ea4332011-03-08 17:11:40 +0100333out:
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100334 if (peer_req)
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +0200335 drbd_free_peer_req(mdev, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700336
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +0100337 if (unlikely(err))
Philipp Reisnerb411b362009-09-25 16:07:19 -0700338 dev_err(DEV, "drbd_send_drequest(..., csum) failed\n");
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +0100339 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700340}
341
342#define GFP_TRY (__GFP_HIGHMEM | __GFP_NOWARN)
343
344static int read_for_csum(struct drbd_conf *mdev, sector_t sector, int size)
345{
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100346 struct drbd_peer_request *peer_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700347
348 if (!get_ldev(mdev))
Lars Ellenberg80a40e42010-08-11 23:28:00 +0200349 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700350
Philipp Reisnere3555d82010-11-07 15:56:29 +0100351 if (drbd_rs_should_slow_down(mdev, sector))
Lars Ellenberg0f0601f2010-08-11 23:40:24 +0200352 goto defer;
353
Philipp Reisnerb411b362009-09-25 16:07:19 -0700354 /* GFP_TRY, because if there is no memory available right now, this may
355 * be rescheduled for later. It is "only" background resync, after all. */
Andreas Gruenbacher0db55362011-04-06 16:09:15 +0200356 peer_req = drbd_alloc_peer_req(mdev, ID_SYNCER /* unused */, sector,
357 size, GFP_TRY);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100358 if (!peer_req)
Lars Ellenberg80a40e42010-08-11 23:28:00 +0200359 goto defer;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700360
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100361 peer_req->w.cb = w_e_send_csum;
Philipp Reisner87eeee42011-01-19 14:16:30 +0100362 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100363 list_add(&peer_req->w.list, &mdev->read_ee);
Philipp Reisner87eeee42011-01-19 14:16:30 +0100364 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700365
Lars Ellenberg0f0601f2010-08-11 23:40:24 +0200366 atomic_add(size >> 9, &mdev->rs_sect_ev);
Andreas Gruenbacherfbe29de2011-02-17 16:38:35 +0100367 if (drbd_submit_peer_request(mdev, peer_req, READ, DRBD_FAULT_RS_RD) == 0)
Lars Ellenberg80a40e42010-08-11 23:28:00 +0200368 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700369
Lars Ellenberg10f6d9922011-01-24 14:47:09 +0100370 /* If it failed because of ENOMEM, retry should help. If it failed
371 * because bio_add_page failed (probably broken lower level driver),
372 * retry may or may not help.
373 * If it does not, you may need to force disconnect. */
Philipp Reisner87eeee42011-01-19 14:16:30 +0100374 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100375 list_del(&peer_req->w.list);
Philipp Reisner87eeee42011-01-19 14:16:30 +0100376 spin_unlock_irq(&mdev->tconn->req_lock);
Lars Ellenberg22cc37a2010-09-14 20:40:41 +0200377
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +0200378 drbd_free_peer_req(mdev, peer_req);
Lars Ellenberg80a40e42010-08-11 23:28:00 +0200379defer:
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200380 put_ldev(mdev);
Lars Ellenberg80a40e42010-08-11 23:28:00 +0200381 return -EAGAIN;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700382}
383
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +0100384int w_resync_timer(struct drbd_work *w, int cancel)
Philipp Reisner794abb72010-12-27 11:51:23 +0100385{
Philipp Reisner00d56942011-02-09 18:09:48 +0100386 struct drbd_conf *mdev = w->mdev;
Philipp Reisner794abb72010-12-27 11:51:23 +0100387 switch (mdev->state.conn) {
388 case C_VERIFY_S:
Philipp Reisner00d56942011-02-09 18:09:48 +0100389 w_make_ov_request(w, cancel);
Philipp Reisner794abb72010-12-27 11:51:23 +0100390 break;
391 case C_SYNC_TARGET:
Philipp Reisner00d56942011-02-09 18:09:48 +0100392 w_make_resync_request(w, cancel);
Philipp Reisner794abb72010-12-27 11:51:23 +0100393 break;
394 }
395
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +0100396 return 0;
Philipp Reisner794abb72010-12-27 11:51:23 +0100397}
398
Philipp Reisnerb411b362009-09-25 16:07:19 -0700399void resync_timer_fn(unsigned long data)
400{
Philipp Reisnerb411b362009-09-25 16:07:19 -0700401 struct drbd_conf *mdev = (struct drbd_conf *) data;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700402
Philipp Reisner794abb72010-12-27 11:51:23 +0100403 if (list_empty(&mdev->resync_work.list))
Lars Ellenbergd5b27b02011-11-14 15:42:37 +0100404 drbd_queue_work(&mdev->tconn->sender_work, &mdev->resync_work);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700405}
406
Philipp Reisner778f2712010-07-06 11:14:00 +0200407static void fifo_set(struct fifo_buffer *fb, int value)
408{
409 int i;
410
411 for (i = 0; i < fb->size; i++)
Philipp Reisnerf10f2622010-10-05 16:50:17 +0200412 fb->values[i] = value;
Philipp Reisner778f2712010-07-06 11:14:00 +0200413}
414
415static int fifo_push(struct fifo_buffer *fb, int value)
416{
417 int ov;
418
419 ov = fb->values[fb->head_index];
420 fb->values[fb->head_index++] = value;
421
422 if (fb->head_index >= fb->size)
423 fb->head_index = 0;
424
425 return ov;
426}
427
428static void fifo_add_val(struct fifo_buffer *fb, int value)
429{
430 int i;
431
432 for (i = 0; i < fb->size; i++)
433 fb->values[i] += value;
434}
435
Philipp Reisner9958c852011-05-03 16:19:31 +0200436struct fifo_buffer *fifo_alloc(int fifo_size)
437{
438 struct fifo_buffer *fb;
439
440 fb = kzalloc(sizeof(struct fifo_buffer) + sizeof(int) * fifo_size, GFP_KERNEL);
441 if (!fb)
442 return NULL;
443
444 fb->head_index = 0;
445 fb->size = fifo_size;
446 fb->total = 0;
447
448 return fb;
449}
450
Philipp Reisner9d77a5f2010-11-07 18:02:56 +0100451static int drbd_rs_controller(struct drbd_conf *mdev)
Philipp Reisner778f2712010-07-06 11:14:00 +0200452{
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +0200453 struct disk_conf *dc;
Philipp Reisner778f2712010-07-06 11:14:00 +0200454 unsigned int sect_in; /* Number of sectors that came in since the last turn */
455 unsigned int want; /* The number of sectors we want in the proxy */
456 int req_sect; /* Number of sectors to request in this turn */
457 int correction; /* Number of sectors more we need in the proxy*/
458 int cps; /* correction per invocation of drbd_rs_controller() */
459 int steps; /* Number of time steps to plan ahead */
460 int curr_corr;
461 int max_sect;
Philipp Reisner813472c2011-05-03 16:47:02 +0200462 struct fifo_buffer *plan;
Philipp Reisner778f2712010-07-06 11:14:00 +0200463
464 sect_in = atomic_xchg(&mdev->rs_sect_in, 0); /* Number of sectors that came in */
465 mdev->rs_in_flight -= sect_in;
466
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +0200467 dc = rcu_dereference(mdev->ldev->disk_conf);
Philipp Reisner813472c2011-05-03 16:47:02 +0200468 plan = rcu_dereference(mdev->rs_plan_s);
Philipp Reisner778f2712010-07-06 11:14:00 +0200469
Philipp Reisner813472c2011-05-03 16:47:02 +0200470 steps = plan->size; /* (dc->c_plan_ahead * 10 * SLEEP_TIME) / HZ; */
Philipp Reisner778f2712010-07-06 11:14:00 +0200471
472 if (mdev->rs_in_flight + sect_in == 0) { /* At start of resync */
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +0200473 want = ((dc->resync_rate * 2 * SLEEP_TIME) / HZ) * steps;
Philipp Reisner778f2712010-07-06 11:14:00 +0200474 } else { /* normal path */
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +0200475 want = dc->c_fill_target ? dc->c_fill_target :
476 sect_in * dc->c_delay_target * HZ / (SLEEP_TIME * 10);
Philipp Reisner778f2712010-07-06 11:14:00 +0200477 }
478
Philipp Reisner813472c2011-05-03 16:47:02 +0200479 correction = want - mdev->rs_in_flight - plan->total;
Philipp Reisner778f2712010-07-06 11:14:00 +0200480
481 /* Plan ahead */
482 cps = correction / steps;
Philipp Reisner813472c2011-05-03 16:47:02 +0200483 fifo_add_val(plan, cps);
484 plan->total += cps * steps;
Philipp Reisner778f2712010-07-06 11:14:00 +0200485
486 /* What we do in this step */
Philipp Reisner813472c2011-05-03 16:47:02 +0200487 curr_corr = fifo_push(plan, 0);
488 plan->total -= curr_corr;
Philipp Reisner778f2712010-07-06 11:14:00 +0200489
490 req_sect = sect_in + curr_corr;
491 if (req_sect < 0)
492 req_sect = 0;
493
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +0200494 max_sect = (dc->c_max_rate * 2 * SLEEP_TIME) / HZ;
Philipp Reisner778f2712010-07-06 11:14:00 +0200495 if (req_sect > max_sect)
496 req_sect = max_sect;
497
498 /*
499 dev_warn(DEV, "si=%u if=%d wa=%u co=%d st=%d cps=%d pl=%d cc=%d rs=%d\n",
500 sect_in, mdev->rs_in_flight, want, correction,
501 steps, cps, mdev->rs_planed, curr_corr, req_sect);
502 */
503
504 return req_sect;
505}
506
Philipp Reisner9d77a5f2010-11-07 18:02:56 +0100507static int drbd_rs_number_requests(struct drbd_conf *mdev)
Lars Ellenberge65f4402010-11-05 10:04:07 +0100508{
509 int number;
Philipp Reisner813472c2011-05-03 16:47:02 +0200510
511 rcu_read_lock();
512 if (rcu_dereference(mdev->rs_plan_s)->size) {
Lars Ellenberge65f4402010-11-05 10:04:07 +0100513 number = drbd_rs_controller(mdev) >> (BM_BLOCK_SHIFT - 9);
514 mdev->c_sync_rate = number * HZ * (BM_BLOCK_SIZE / 1024) / SLEEP_TIME;
515 } else {
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +0200516 mdev->c_sync_rate = rcu_dereference(mdev->ldev->disk_conf)->resync_rate;
Lars Ellenberge65f4402010-11-05 10:04:07 +0100517 number = SLEEP_TIME * mdev->c_sync_rate / ((BM_BLOCK_SIZE / 1024) * HZ);
518 }
Philipp Reisner813472c2011-05-03 16:47:02 +0200519 rcu_read_unlock();
Lars Ellenberge65f4402010-11-05 10:04:07 +0100520
Lars Ellenberge65f4402010-11-05 10:04:07 +0100521 /* ignore the amount of pending requests, the resync controller should
522 * throttle down to incoming reply rate soon enough anyways. */
523 return number;
524}
525
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +0100526int w_make_resync_request(struct drbd_work *w, int cancel)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700527{
Philipp Reisner00d56942011-02-09 18:09:48 +0100528 struct drbd_conf *mdev = w->mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700529 unsigned long bit;
530 sector_t sector;
531 const sector_t capacity = drbd_get_capacity(mdev->this_bdev);
Lars Ellenberg1816a2b2010-11-11 15:19:07 +0100532 int max_bio_size;
Lars Ellenberge65f4402010-11-05 10:04:07 +0100533 int number, rollback_i, size;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700534 int align, queued, sndbuf;
Lars Ellenberg0f0601f2010-08-11 23:40:24 +0200535 int i = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700536
537 if (unlikely(cancel))
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +0100538 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700539
Lars Ellenbergaf85e8e2010-10-07 16:07:55 +0200540 if (mdev->rs_total == 0) {
541 /* empty resync? */
542 drbd_resync_finished(mdev);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +0100543 return 0;
Lars Ellenbergaf85e8e2010-10-07 16:07:55 +0200544 }
545
Philipp Reisnerb411b362009-09-25 16:07:19 -0700546 if (!get_ldev(mdev)) {
547 /* Since we only need to access mdev->rsync a
548 get_ldev_if_state(mdev,D_FAILED) would be sufficient, but
549 to continue resync with a broken disk makes no sense at
550 all */
551 dev_err(DEV, "Disk broke down during resync!\n");
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +0100552 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700553 }
554
Philipp Reisner0cfdd242011-05-25 11:14:35 +0200555 max_bio_size = queue_max_hw_sectors(mdev->rq_queue) << 9;
Lars Ellenberge65f4402010-11-05 10:04:07 +0100556 number = drbd_rs_number_requests(mdev);
557 if (number == 0)
Lars Ellenberg0f0601f2010-08-11 23:40:24 +0200558 goto requeue;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700559
Philipp Reisnerb411b362009-09-25 16:07:19 -0700560 for (i = 0; i < number; i++) {
561 /* Stop generating RS requests, when half of the send buffer is filled */
Philipp Reisnere42325a2011-01-19 13:55:45 +0100562 mutex_lock(&mdev->tconn->data.mutex);
563 if (mdev->tconn->data.socket) {
564 queued = mdev->tconn->data.socket->sk->sk_wmem_queued;
565 sndbuf = mdev->tconn->data.socket->sk->sk_sndbuf;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700566 } else {
567 queued = 1;
568 sndbuf = 0;
569 }
Philipp Reisnere42325a2011-01-19 13:55:45 +0100570 mutex_unlock(&mdev->tconn->data.mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700571 if (queued > sndbuf / 2)
572 goto requeue;
573
574next_sector:
575 size = BM_BLOCK_SIZE;
576 bit = drbd_bm_find_next(mdev, mdev->bm_resync_fo);
577
Lars Ellenberg4b0715f2010-12-14 15:13:04 +0100578 if (bit == DRBD_END_OF_BITMAP) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700579 mdev->bm_resync_fo = drbd_bm_bits(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700580 put_ldev(mdev);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +0100581 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700582 }
583
584 sector = BM_BIT_TO_SECT(bit);
585
Philipp Reisnere3555d82010-11-07 15:56:29 +0100586 if (drbd_rs_should_slow_down(mdev, sector) ||
587 drbd_try_rs_begin_io(mdev, sector)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700588 mdev->bm_resync_fo = bit;
589 goto requeue;
590 }
591 mdev->bm_resync_fo = bit + 1;
592
593 if (unlikely(drbd_bm_test_bit(mdev, bit) == 0)) {
594 drbd_rs_complete_io(mdev, sector);
595 goto next_sector;
596 }
597
Lars Ellenberg1816a2b2010-11-11 15:19:07 +0100598#if DRBD_MAX_BIO_SIZE > BM_BLOCK_SIZE
Philipp Reisnerb411b362009-09-25 16:07:19 -0700599 /* try to find some adjacent bits.
600 * we stop if we have already the maximum req size.
601 *
602 * Additionally always align bigger requests, in order to
603 * be prepared for all stripe sizes of software RAIDs.
Philipp Reisnerb411b362009-09-25 16:07:19 -0700604 */
605 align = 1;
Philipp Reisnerd2074502010-07-22 15:27:27 +0200606 rollback_i = i;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700607 for (;;) {
Lars Ellenberg1816a2b2010-11-11 15:19:07 +0100608 if (size + BM_BLOCK_SIZE > max_bio_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700609 break;
610
611 /* Be always aligned */
612 if (sector & ((1<<(align+3))-1))
613 break;
614
615 /* do not cross extent boundaries */
616 if (((bit+1) & BM_BLOCKS_PER_BM_EXT_MASK) == 0)
617 break;
618 /* now, is it actually dirty, after all?
619 * caution, drbd_bm_test_bit is tri-state for some
620 * obscure reason; ( b == 0 ) would get the out-of-band
621 * only accidentally right because of the "oddly sized"
622 * adjustment below */
623 if (drbd_bm_test_bit(mdev, bit+1) != 1)
624 break;
625 bit++;
626 size += BM_BLOCK_SIZE;
627 if ((BM_BLOCK_SIZE << align) <= size)
628 align++;
629 i++;
630 }
631 /* if we merged some,
632 * reset the offset to start the next drbd_bm_find_next from */
633 if (size > BM_BLOCK_SIZE)
634 mdev->bm_resync_fo = bit + 1;
635#endif
636
637 /* adjust very last sectors, in case we are oddly sized */
638 if (sector + (size>>9) > capacity)
639 size = (capacity-sector)<<9;
Lars Ellenbergf3990022011-03-23 14:31:09 +0100640 if (mdev->tconn->agreed_pro_version >= 89 && mdev->tconn->csums_tfm) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700641 switch (read_for_csum(mdev, sector, size)) {
Lars Ellenberg80a40e42010-08-11 23:28:00 +0200642 case -EIO: /* Disk failure */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700643 put_ldev(mdev);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +0100644 return -EIO;
Lars Ellenberg80a40e42010-08-11 23:28:00 +0200645 case -EAGAIN: /* allocation failed, or ldev busy */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700646 drbd_rs_complete_io(mdev, sector);
647 mdev->bm_resync_fo = BM_SECT_TO_BIT(sector);
Philipp Reisnerd2074502010-07-22 15:27:27 +0200648 i = rollback_i;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700649 goto requeue;
Lars Ellenberg80a40e42010-08-11 23:28:00 +0200650 case 0:
651 /* everything ok */
652 break;
653 default:
654 BUG();
Philipp Reisnerb411b362009-09-25 16:07:19 -0700655 }
656 } else {
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +0100657 int err;
658
Philipp Reisnerb411b362009-09-25 16:07:19 -0700659 inc_rs_pending(mdev);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +0100660 err = drbd_send_drequest(mdev, P_RS_DATA_REQUEST,
661 sector, size, ID_SYNCER);
662 if (err) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700663 dev_err(DEV, "drbd_send_drequest() failed, aborting...\n");
664 dec_rs_pending(mdev);
665 put_ldev(mdev);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +0100666 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700667 }
668 }
669 }
670
671 if (mdev->bm_resync_fo >= drbd_bm_bits(mdev)) {
672 /* last syncer _request_ was sent,
673 * but the P_RS_DATA_REPLY not yet received. sync will end (and
674 * next sync group will resume), as soon as we receive the last
675 * resync data block, and the last bit is cleared.
676 * until then resync "work" is "inactive" ...
677 */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700678 put_ldev(mdev);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +0100679 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700680 }
681
682 requeue:
Philipp Reisner778f2712010-07-06 11:14:00 +0200683 mdev->rs_in_flight += (i << (BM_BLOCK_SHIFT - 9));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700684 mod_timer(&mdev->resync_timer, jiffies + SLEEP_TIME);
685 put_ldev(mdev);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +0100686 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700687}
688
Philipp Reisner00d56942011-02-09 18:09:48 +0100689static int w_make_ov_request(struct drbd_work *w, int cancel)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700690{
Philipp Reisner00d56942011-02-09 18:09:48 +0100691 struct drbd_conf *mdev = w->mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700692 int number, i, size;
693 sector_t sector;
694 const sector_t capacity = drbd_get_capacity(mdev->this_bdev);
Lars Ellenberg58ffa582012-07-26 14:09:49 +0200695 bool stop_sector_reached = false;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700696
697 if (unlikely(cancel))
698 return 1;
699
Lars Ellenberg2649f082010-11-05 10:05:47 +0100700 number = drbd_rs_number_requests(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700701
702 sector = mdev->ov_position;
703 for (i = 0; i < number; i++) {
Lars Ellenberg58ffa582012-07-26 14:09:49 +0200704 if (sector >= capacity)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700705 return 1;
Lars Ellenberg58ffa582012-07-26 14:09:49 +0200706
707 /* We check for "finished" only in the reply path:
708 * w_e_end_ov_reply().
709 * We need to send at least one request out. */
710 stop_sector_reached = i > 0
711 && verify_can_do_stop_sector(mdev)
712 && sector >= mdev->ov_stop_sector;
713 if (stop_sector_reached)
714 break;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700715
716 size = BM_BLOCK_SIZE;
717
Philipp Reisnere3555d82010-11-07 15:56:29 +0100718 if (drbd_rs_should_slow_down(mdev, sector) ||
719 drbd_try_rs_begin_io(mdev, sector)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700720 mdev->ov_position = sector;
721 goto requeue;
722 }
723
724 if (sector + (size>>9) > capacity)
725 size = (capacity-sector)<<9;
726
727 inc_rs_pending(mdev);
Andreas Gruenbacher5b9f4992011-03-16 01:31:39 +0100728 if (drbd_send_ov_request(mdev, sector, size)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700729 dec_rs_pending(mdev);
730 return 0;
731 }
732 sector += BM_SECT_PER_BIT;
733 }
734 mdev->ov_position = sector;
735
736 requeue:
Lars Ellenberg2649f082010-11-05 10:05:47 +0100737 mdev->rs_in_flight += (i << (BM_BLOCK_SHIFT - 9));
Lars Ellenberg58ffa582012-07-26 14:09:49 +0200738 if (i == 0 || !stop_sector_reached)
739 mod_timer(&mdev->resync_timer, jiffies + SLEEP_TIME);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700740 return 1;
741}
742
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +0100743int w_ov_finished(struct drbd_work *w, int cancel)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700744{
Philipp Reisner00d56942011-02-09 18:09:48 +0100745 struct drbd_conf *mdev = w->mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700746 kfree(w);
Andreas Gruenbacher8f7bed72010-12-19 23:53:14 +0100747 ov_out_of_sync_print(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700748 drbd_resync_finished(mdev);
749
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +0100750 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700751}
752
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +0100753static int w_resync_finished(struct drbd_work *w, int cancel)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700754{
Philipp Reisner00d56942011-02-09 18:09:48 +0100755 struct drbd_conf *mdev = w->mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700756 kfree(w);
757
758 drbd_resync_finished(mdev);
759
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +0100760 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700761}
762
Lars Ellenbergaf85e8e2010-10-07 16:07:55 +0200763static void ping_peer(struct drbd_conf *mdev)
764{
Philipp Reisner2a67d8b2011-02-09 14:10:32 +0100765 struct drbd_tconn *tconn = mdev->tconn;
766
767 clear_bit(GOT_PING_ACK, &tconn->flags);
768 request_ping(tconn);
769 wait_event(tconn->ping_wait,
770 test_bit(GOT_PING_ACK, &tconn->flags) || mdev->state.conn < C_CONNECTED);
Lars Ellenbergaf85e8e2010-10-07 16:07:55 +0200771}
772
Philipp Reisnerb411b362009-09-25 16:07:19 -0700773int drbd_resync_finished(struct drbd_conf *mdev)
774{
775 unsigned long db, dt, dbdt;
776 unsigned long n_oos;
777 union drbd_state os, ns;
778 struct drbd_work *w;
779 char *khelper_cmd = NULL;
Lars Ellenberg26525612010-11-05 09:56:33 +0100780 int verify_done = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700781
782 /* Remove all elements from the resync LRU. Since future actions
783 * might set bits in the (main) bitmap, then the entries in the
784 * resync LRU would be wrong. */
785 if (drbd_rs_del_all(mdev)) {
786 /* In case this is not possible now, most probably because
787 * there are P_RS_DATA_REPLY Packets lingering on the worker's
788 * queue (or even the read operations for those packets
789 * is not finished by now). Retry in 100ms. */
790
Philipp Reisner20ee6392011-01-18 15:28:59 +0100791 schedule_timeout_interruptible(HZ / 10);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700792 w = kmalloc(sizeof(struct drbd_work), GFP_ATOMIC);
793 if (w) {
794 w->cb = w_resync_finished;
Philipp Reisner9b743da2011-07-15 18:15:45 +0200795 w->mdev = mdev;
Lars Ellenbergd5b27b02011-11-14 15:42:37 +0100796 drbd_queue_work(&mdev->tconn->sender_work, w);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700797 return 1;
798 }
799 dev_err(DEV, "Warn failed to drbd_rs_del_all() and to kmalloc(w).\n");
800 }
801
802 dt = (jiffies - mdev->rs_start - mdev->rs_paused) / HZ;
803 if (dt <= 0)
804 dt = 1;
Lars Ellenberg58ffa582012-07-26 14:09:49 +0200805
Philipp Reisnerb411b362009-09-25 16:07:19 -0700806 db = mdev->rs_total;
Lars Ellenberg58ffa582012-07-26 14:09:49 +0200807 /* adjust for verify start and stop sectors, respective reached position */
808 if (mdev->state.conn == C_VERIFY_S || mdev->state.conn == C_VERIFY_T)
809 db -= mdev->ov_left;
810
Philipp Reisnerb411b362009-09-25 16:07:19 -0700811 dbdt = Bit2KB(db/dt);
812 mdev->rs_paused /= HZ;
813
814 if (!get_ldev(mdev))
815 goto out;
816
Lars Ellenbergaf85e8e2010-10-07 16:07:55 +0200817 ping_peer(mdev);
818
Philipp Reisner87eeee42011-01-19 14:16:30 +0100819 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisner78bae592011-03-28 15:40:12 +0200820 os = drbd_read_state(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700821
Lars Ellenberg26525612010-11-05 09:56:33 +0100822 verify_done = (os.conn == C_VERIFY_S || os.conn == C_VERIFY_T);
823
Philipp Reisnerb411b362009-09-25 16:07:19 -0700824 /* This protects us against multiple calls (that can happen in the presence
825 of application IO), and against connectivity loss just before we arrive here. */
826 if (os.conn <= C_CONNECTED)
827 goto out_unlock;
828
829 ns = os;
830 ns.conn = C_CONNECTED;
831
832 dev_info(DEV, "%s done (total %lu sec; paused %lu sec; %lu K/sec)\n",
Lars Ellenberg58ffa582012-07-26 14:09:49 +0200833 verify_done ? "Online verify" : "Resync",
Philipp Reisnerb411b362009-09-25 16:07:19 -0700834 dt + mdev->rs_paused, mdev->rs_paused, dbdt);
835
836 n_oos = drbd_bm_total_weight(mdev);
837
838 if (os.conn == C_VERIFY_S || os.conn == C_VERIFY_T) {
839 if (n_oos) {
840 dev_alert(DEV, "Online verify found %lu %dk block out of sync!\n",
841 n_oos, Bit2KB(1));
842 khelper_cmd = "out-of-sync";
843 }
844 } else {
845 D_ASSERT((n_oos - mdev->rs_failed) == 0);
846
847 if (os.conn == C_SYNC_TARGET || os.conn == C_PAUSED_SYNC_T)
848 khelper_cmd = "after-resync-target";
849
Lars Ellenbergf3990022011-03-23 14:31:09 +0100850 if (mdev->tconn->csums_tfm && mdev->rs_total) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700851 const unsigned long s = mdev->rs_same_csum;
852 const unsigned long t = mdev->rs_total;
853 const int ratio =
854 (t == 0) ? 0 :
855 (t < 100000) ? ((s*100)/t) : (s/(t/100));
Bart Van Assche24c48302011-05-21 18:32:29 +0200856 dev_info(DEV, "%u %% had equal checksums, eliminated: %luK; "
Philipp Reisnerb411b362009-09-25 16:07:19 -0700857 "transferred %luK total %luK\n",
858 ratio,
859 Bit2KB(mdev->rs_same_csum),
860 Bit2KB(mdev->rs_total - mdev->rs_same_csum),
861 Bit2KB(mdev->rs_total));
862 }
863 }
864
865 if (mdev->rs_failed) {
866 dev_info(DEV, " %lu failed blocks\n", mdev->rs_failed);
867
868 if (os.conn == C_SYNC_TARGET || os.conn == C_PAUSED_SYNC_T) {
869 ns.disk = D_INCONSISTENT;
870 ns.pdsk = D_UP_TO_DATE;
871 } else {
872 ns.disk = D_UP_TO_DATE;
873 ns.pdsk = D_INCONSISTENT;
874 }
875 } else {
876 ns.disk = D_UP_TO_DATE;
877 ns.pdsk = D_UP_TO_DATE;
878
879 if (os.conn == C_SYNC_TARGET || os.conn == C_PAUSED_SYNC_T) {
880 if (mdev->p_uuid) {
881 int i;
882 for (i = UI_BITMAP ; i <= UI_HISTORY_END ; i++)
883 _drbd_uuid_set(mdev, i, mdev->p_uuid[i]);
884 drbd_uuid_set(mdev, UI_BITMAP, mdev->ldev->md.uuid[UI_CURRENT]);
885 _drbd_uuid_set(mdev, UI_CURRENT, mdev->p_uuid[UI_CURRENT]);
886 } else {
887 dev_err(DEV, "mdev->p_uuid is NULL! BUG\n");
888 }
889 }
890
Lars Ellenberg62b0da32011-01-20 13:25:21 +0100891 if (!(os.conn == C_VERIFY_S || os.conn == C_VERIFY_T)) {
892 /* for verify runs, we don't update uuids here,
893 * so there would be nothing to report. */
894 drbd_uuid_set_bm(mdev, 0UL);
895 drbd_print_uuids(mdev, "updated UUIDs");
896 if (mdev->p_uuid) {
897 /* Now the two UUID sets are equal, update what we
898 * know of the peer. */
899 int i;
900 for (i = UI_CURRENT ; i <= UI_HISTORY_END ; i++)
901 mdev->p_uuid[i] = mdev->ldev->md.uuid[i];
902 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700903 }
904 }
905
906 _drbd_set_state(mdev, ns, CS_VERBOSE, NULL);
907out_unlock:
Philipp Reisner87eeee42011-01-19 14:16:30 +0100908 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700909 put_ldev(mdev);
910out:
911 mdev->rs_total = 0;
912 mdev->rs_failed = 0;
913 mdev->rs_paused = 0;
Lars Ellenberg58ffa582012-07-26 14:09:49 +0200914
915 /* reset start sector, if we reached end of device */
916 if (verify_done && mdev->ov_left == 0)
Lars Ellenberg26525612010-11-05 09:56:33 +0100917 mdev->ov_start_sector = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700918
Lars Ellenberg13d42682010-10-13 17:37:54 +0200919 drbd_md_sync(mdev);
920
Philipp Reisnerb411b362009-09-25 16:07:19 -0700921 if (khelper_cmd)
922 drbd_khelper(mdev, khelper_cmd);
923
924 return 1;
925}
926
927/* helper */
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100928static void move_to_net_ee_or_free(struct drbd_conf *mdev, struct drbd_peer_request *peer_req)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700929{
Andreas Gruenbacher045417f2011-04-07 21:34:24 +0200930 if (drbd_peer_req_has_active_page(peer_req)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700931 /* This might happen if sendpage() has not finished */
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100932 int i = (peer_req->i.size + PAGE_SIZE -1) >> PAGE_SHIFT;
Lars Ellenberg435f0742010-09-06 12:30:25 +0200933 atomic_add(i, &mdev->pp_in_use_by_net);
934 atomic_sub(i, &mdev->pp_in_use);
Philipp Reisner87eeee42011-01-19 14:16:30 +0100935 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100936 list_add_tail(&peer_req->w.list, &mdev->net_ee);
Philipp Reisner87eeee42011-01-19 14:16:30 +0100937 spin_unlock_irq(&mdev->tconn->req_lock);
Lars Ellenberg435f0742010-09-06 12:30:25 +0200938 wake_up(&drbd_pp_wait);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700939 } else
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +0200940 drbd_free_peer_req(mdev, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700941}
942
943/**
944 * w_e_end_data_req() - Worker callback, to send a P_DATA_REPLY packet in response to a P_DATA_REQUEST
945 * @mdev: DRBD device.
946 * @w: work object.
947 * @cancel: The connection will be closed anyways
948 */
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +0100949int w_e_end_data_req(struct drbd_work *w, int cancel)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700950{
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100951 struct drbd_peer_request *peer_req = container_of(w, struct drbd_peer_request, w);
Philipp Reisner00d56942011-02-09 18:09:48 +0100952 struct drbd_conf *mdev = w->mdev;
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +0100953 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700954
955 if (unlikely(cancel)) {
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +0200956 drbd_free_peer_req(mdev, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700957 dec_unacked(mdev);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +0100958 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700959 }
960
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100961 if (likely((peer_req->flags & EE_WAS_ERROR) == 0)) {
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +0100962 err = drbd_send_block(mdev, P_DATA_REPLY, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700963 } else {
964 if (__ratelimit(&drbd_ratelimit_state))
965 dev_err(DEV, "Sending NegDReply. sector=%llus.\n",
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100966 (unsigned long long)peer_req->i.sector);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700967
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +0100968 err = drbd_send_ack(mdev, P_NEG_DREPLY, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700969 }
970
971 dec_unacked(mdev);
972
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100973 move_to_net_ee_or_free(mdev, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700974
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +0100975 if (unlikely(err))
Philipp Reisnerb411b362009-09-25 16:07:19 -0700976 dev_err(DEV, "drbd_send_block() failed\n");
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +0100977 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700978}
979
980/**
Andreas Gruenbachera209b4a2011-08-17 12:43:25 +0200981 * w_e_end_rsdata_req() - Worker callback to send a P_RS_DATA_REPLY packet in response to a P_RS_DATA_REQUEST
Philipp Reisnerb411b362009-09-25 16:07:19 -0700982 * @mdev: DRBD device.
983 * @w: work object.
984 * @cancel: The connection will be closed anyways
985 */
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +0100986int w_e_end_rsdata_req(struct drbd_work *w, int cancel)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700987{
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100988 struct drbd_peer_request *peer_req = container_of(w, struct drbd_peer_request, w);
Philipp Reisner00d56942011-02-09 18:09:48 +0100989 struct drbd_conf *mdev = w->mdev;
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +0100990 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700991
992 if (unlikely(cancel)) {
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +0200993 drbd_free_peer_req(mdev, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700994 dec_unacked(mdev);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +0100995 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700996 }
997
998 if (get_ldev_if_state(mdev, D_FAILED)) {
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100999 drbd_rs_complete_io(mdev, peer_req->i.sector);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001000 put_ldev(mdev);
1001 }
1002
Philipp Reisnerd612d302010-12-27 10:53:28 +01001003 if (mdev->state.conn == C_AHEAD) {
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001004 err = drbd_send_ack(mdev, P_RS_CANCEL, peer_req);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001005 } else if (likely((peer_req->flags & EE_WAS_ERROR) == 0)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001006 if (likely(mdev->state.pdsk >= D_INCONSISTENT)) {
1007 inc_rs_pending(mdev);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001008 err = drbd_send_block(mdev, P_RS_DATA_REPLY, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001009 } else {
1010 if (__ratelimit(&drbd_ratelimit_state))
1011 dev_err(DEV, "Not sending RSDataReply, "
1012 "partner DISKLESS!\n");
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001013 err = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001014 }
1015 } else {
1016 if (__ratelimit(&drbd_ratelimit_state))
1017 dev_err(DEV, "Sending NegRSDReply. sector %llus.\n",
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001018 (unsigned long long)peer_req->i.sector);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001019
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001020 err = drbd_send_ack(mdev, P_NEG_RS_DREPLY, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001021
1022 /* update resync data with failure */
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001023 drbd_rs_failed_io(mdev, peer_req->i.sector, peer_req->i.size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001024 }
1025
1026 dec_unacked(mdev);
1027
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001028 move_to_net_ee_or_free(mdev, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001029
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001030 if (unlikely(err))
Philipp Reisnerb411b362009-09-25 16:07:19 -07001031 dev_err(DEV, "drbd_send_block() failed\n");
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001032 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001033}
1034
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001035int w_e_end_csum_rs_req(struct drbd_work *w, int cancel)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001036{
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001037 struct drbd_peer_request *peer_req = container_of(w, struct drbd_peer_request, w);
Philipp Reisner00d56942011-02-09 18:09:48 +01001038 struct drbd_conf *mdev = w->mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001039 struct digest_info *di;
1040 int digest_size;
1041 void *digest = NULL;
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001042 int err, eq = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001043
1044 if (unlikely(cancel)) {
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +02001045 drbd_free_peer_req(mdev, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001046 dec_unacked(mdev);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001047 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001048 }
1049
Lars Ellenberg1d53f092010-09-05 01:13:24 +02001050 if (get_ldev(mdev)) {
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001051 drbd_rs_complete_io(mdev, peer_req->i.sector);
Lars Ellenberg1d53f092010-09-05 01:13:24 +02001052 put_ldev(mdev);
1053 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001054
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001055 di = peer_req->digest;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001056
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001057 if (likely((peer_req->flags & EE_WAS_ERROR) == 0)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001058 /* quick hack to try to avoid a race against reconfiguration.
1059 * a real fix would be much more involved,
1060 * introducing more locking mechanisms */
Lars Ellenbergf3990022011-03-23 14:31:09 +01001061 if (mdev->tconn->csums_tfm) {
1062 digest_size = crypto_hash_digestsize(mdev->tconn->csums_tfm);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001063 D_ASSERT(digest_size == di->digest_size);
1064 digest = kmalloc(digest_size, GFP_NOIO);
1065 }
1066 if (digest) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01001067 drbd_csum_ee(mdev, mdev->tconn->csums_tfm, peer_req, digest);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001068 eq = !memcmp(digest, di->digest, digest_size);
1069 kfree(digest);
1070 }
1071
1072 if (eq) {
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001073 drbd_set_in_sync(mdev, peer_req->i.sector, peer_req->i.size);
Lars Ellenberg676396d2010-03-03 02:08:22 +01001074 /* rs_same_csums unit is BM_BLOCK_SIZE */
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001075 mdev->rs_same_csum += peer_req->i.size >> BM_BLOCK_SHIFT;
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001076 err = drbd_send_ack(mdev, P_RS_IS_IN_SYNC, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001077 } else {
1078 inc_rs_pending(mdev);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001079 peer_req->block_id = ID_SYNCER; /* By setting block_id, digest pointer becomes invalid! */
1080 peer_req->flags &= ~EE_HAS_DIGEST; /* This peer request no longer has a digest pointer */
Philipp Reisner204bba92010-08-23 16:17:13 +02001081 kfree(di);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001082 err = drbd_send_block(mdev, P_RS_DATA_REPLY, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001083 }
1084 } else {
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001085 err = drbd_send_ack(mdev, P_NEG_RS_DREPLY, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001086 if (__ratelimit(&drbd_ratelimit_state))
1087 dev_err(DEV, "Sending NegDReply. I guess it gets messy.\n");
1088 }
1089
1090 dec_unacked(mdev);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001091 move_to_net_ee_or_free(mdev, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001092
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001093 if (unlikely(err))
Philipp Reisnerb411b362009-09-25 16:07:19 -07001094 dev_err(DEV, "drbd_send_block/ack() failed\n");
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001095 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001096}
1097
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001098int w_e_end_ov_req(struct drbd_work *w, int cancel)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001099{
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001100 struct drbd_peer_request *peer_req = container_of(w, struct drbd_peer_request, w);
Philipp Reisner00d56942011-02-09 18:09:48 +01001101 struct drbd_conf *mdev = w->mdev;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001102 sector_t sector = peer_req->i.sector;
1103 unsigned int size = peer_req->i.size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001104 int digest_size;
1105 void *digest;
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001106 int err = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001107
1108 if (unlikely(cancel))
1109 goto out;
1110
Lars Ellenbergf3990022011-03-23 14:31:09 +01001111 digest_size = crypto_hash_digestsize(mdev->tconn->verify_tfm);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001112 digest = kmalloc(digest_size, GFP_NOIO);
Philipp Reisner8f214202011-03-01 15:52:35 +01001113 if (!digest) {
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001114 err = 1; /* terminate the connection in case the allocation failed */
Philipp Reisner8f214202011-03-01 15:52:35 +01001115 goto out;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001116 }
1117
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001118 if (likely(!(peer_req->flags & EE_WAS_ERROR)))
Lars Ellenbergf3990022011-03-23 14:31:09 +01001119 drbd_csum_ee(mdev, mdev->tconn->verify_tfm, peer_req, digest);
Philipp Reisner8f214202011-03-01 15:52:35 +01001120 else
1121 memset(digest, 0, digest_size);
1122
Lars Ellenberg53ea4332011-03-08 17:11:40 +01001123 /* Free e and pages before send.
1124 * In case we block on congestion, we could otherwise run into
1125 * some distributed deadlock, if the other side blocks on
1126 * congestion as well, because our receiver blocks in
Andreas Gruenbacherc37c8ec2011-04-07 21:02:09 +02001127 * drbd_alloc_pages due to pp_in_use > max_buffers. */
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +02001128 drbd_free_peer_req(mdev, peer_req);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001129 peer_req = NULL;
Philipp Reisner8f214202011-03-01 15:52:35 +01001130 inc_rs_pending(mdev);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001131 err = drbd_send_drequest_csum(mdev, sector, size, digest, digest_size, P_OV_REPLY);
1132 if (err)
Philipp Reisner8f214202011-03-01 15:52:35 +01001133 dec_rs_pending(mdev);
1134 kfree(digest);
1135
Philipp Reisnerb411b362009-09-25 16:07:19 -07001136out:
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001137 if (peer_req)
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +02001138 drbd_free_peer_req(mdev, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001139 dec_unacked(mdev);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001140 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001141}
1142
Andreas Gruenbacher8f7bed72010-12-19 23:53:14 +01001143void drbd_ov_out_of_sync_found(struct drbd_conf *mdev, sector_t sector, int size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001144{
1145 if (mdev->ov_last_oos_start + mdev->ov_last_oos_size == sector) {
1146 mdev->ov_last_oos_size += size>>9;
1147 } else {
1148 mdev->ov_last_oos_start = sector;
1149 mdev->ov_last_oos_size = size>>9;
1150 }
1151 drbd_set_out_of_sync(mdev, sector, size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001152}
1153
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001154int w_e_end_ov_reply(struct drbd_work *w, int cancel)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001155{
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001156 struct drbd_peer_request *peer_req = container_of(w, struct drbd_peer_request, w);
Philipp Reisner00d56942011-02-09 18:09:48 +01001157 struct drbd_conf *mdev = w->mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001158 struct digest_info *di;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001159 void *digest;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001160 sector_t sector = peer_req->i.sector;
1161 unsigned int size = peer_req->i.size;
Lars Ellenberg53ea4332011-03-08 17:11:40 +01001162 int digest_size;
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001163 int err, eq = 0;
Lars Ellenberg58ffa582012-07-26 14:09:49 +02001164 bool stop_sector_reached = false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001165
1166 if (unlikely(cancel)) {
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +02001167 drbd_free_peer_req(mdev, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001168 dec_unacked(mdev);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001169 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001170 }
1171
1172 /* after "cancel", because after drbd_disconnect/drbd_rs_cancel_all
1173 * the resync lru has been cleaned up already */
Lars Ellenberg1d53f092010-09-05 01:13:24 +02001174 if (get_ldev(mdev)) {
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001175 drbd_rs_complete_io(mdev, peer_req->i.sector);
Lars Ellenberg1d53f092010-09-05 01:13:24 +02001176 put_ldev(mdev);
1177 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001178
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001179 di = peer_req->digest;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001180
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001181 if (likely((peer_req->flags & EE_WAS_ERROR) == 0)) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01001182 digest_size = crypto_hash_digestsize(mdev->tconn->verify_tfm);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001183 digest = kmalloc(digest_size, GFP_NOIO);
1184 if (digest) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01001185 drbd_csum_ee(mdev, mdev->tconn->verify_tfm, peer_req, digest);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001186
1187 D_ASSERT(digest_size == di->digest_size);
1188 eq = !memcmp(digest, di->digest, digest_size);
1189 kfree(digest);
1190 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001191 }
1192
Lars Ellenberg9676c762011-02-22 14:02:31 +01001193 /* Free peer_req and pages before send.
1194 * In case we block on congestion, we could otherwise run into
1195 * some distributed deadlock, if the other side blocks on
1196 * congestion as well, because our receiver blocks in
Andreas Gruenbacherc37c8ec2011-04-07 21:02:09 +02001197 * drbd_alloc_pages due to pp_in_use > max_buffers. */
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +02001198 drbd_free_peer_req(mdev, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001199 if (!eq)
Andreas Gruenbacher8f7bed72010-12-19 23:53:14 +01001200 drbd_ov_out_of_sync_found(mdev, sector, size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001201 else
Andreas Gruenbacher8f7bed72010-12-19 23:53:14 +01001202 ov_out_of_sync_print(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001203
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001204 err = drbd_send_ack_ex(mdev, P_OV_RESULT, sector, size,
Andreas Gruenbacherfa79abd2011-03-16 01:31:39 +01001205 eq ? ID_IN_SYNC : ID_OUT_OF_SYNC);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001206
Lars Ellenberg53ea4332011-03-08 17:11:40 +01001207 dec_unacked(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001208
Lars Ellenbergea5442a2010-11-05 09:48:01 +01001209 --mdev->ov_left;
1210
1211 /* let's advance progress step marks only for every other megabyte */
1212 if ((mdev->ov_left & 0x200) == 0x200)
1213 drbd_advance_rs_marks(mdev, mdev->ov_left);
1214
Lars Ellenberg58ffa582012-07-26 14:09:49 +02001215 stop_sector_reached = verify_can_do_stop_sector(mdev) &&
1216 (sector + (size>>9)) >= mdev->ov_stop_sector;
1217
1218 if (mdev->ov_left == 0 || stop_sector_reached) {
Andreas Gruenbacher8f7bed72010-12-19 23:53:14 +01001219 ov_out_of_sync_print(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001220 drbd_resync_finished(mdev);
1221 }
1222
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001223 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001224}
1225
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001226int w_prev_work_done(struct drbd_work *w, int cancel)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001227{
1228 struct drbd_wq_barrier *b = container_of(w, struct drbd_wq_barrier, w);
Philipp Reisner00d56942011-02-09 18:09:48 +01001229
Philipp Reisnerb411b362009-09-25 16:07:19 -07001230 complete(&b->done);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001231 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001232}
1233
Lars Ellenbergb6dd1a82011-11-28 15:04:49 +01001234/* FIXME
1235 * We need to track the number of pending barrier acks,
1236 * and to be able to wait for them.
1237 * See also comment in drbd_adm_attach before drbd_suspend_io.
1238 */
1239int drbd_send_barrier(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001240{
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02001241 struct p_barrier *p;
Lars Ellenbergb6dd1a82011-11-28 15:04:49 +01001242 struct drbd_socket *sock;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001243
Lars Ellenbergb6dd1a82011-11-28 15:04:49 +01001244 sock = &tconn->data;
1245 p = conn_prepare_command(tconn, sock);
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02001246 if (!p)
1247 return -EIO;
Lars Ellenbergb6dd1a82011-11-28 15:04:49 +01001248 p->barrier = tconn->send.current_epoch_nr;
1249 p->pad = 0;
1250 tconn->send.current_epoch_writes = 0;
1251
1252 return conn_send_command(tconn, sock, P_BARRIER, sizeof(*p), NULL, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001253}
1254
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001255int w_send_write_hint(struct drbd_work *w, int cancel)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001256{
Philipp Reisner00d56942011-02-09 18:09:48 +01001257 struct drbd_conf *mdev = w->mdev;
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02001258 struct drbd_socket *sock;
1259
Philipp Reisnerb411b362009-09-25 16:07:19 -07001260 if (cancel)
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001261 return 0;
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02001262 sock = &mdev->tconn->data;
1263 if (!drbd_prepare_command(mdev, sock))
1264 return -EIO;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001265 return drbd_send_command(mdev, sock, P_UNPLUG_REMOTE, 0, NULL, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001266}
1267
Lars Ellenberg4eb9b3c2012-08-20 11:05:23 +02001268static void re_init_if_first_write(struct drbd_tconn *tconn, unsigned int epoch)
1269{
1270 if (!tconn->send.seen_any_write_yet) {
1271 tconn->send.seen_any_write_yet = true;
1272 tconn->send.current_epoch_nr = epoch;
1273 tconn->send.current_epoch_writes = 0;
1274 }
1275}
1276
1277static void maybe_send_barrier(struct drbd_tconn *tconn, unsigned int epoch)
1278{
1279 /* re-init if first write on this connection */
1280 if (!tconn->send.seen_any_write_yet)
1281 return;
1282 if (tconn->send.current_epoch_nr != epoch) {
1283 if (tconn->send.current_epoch_writes)
1284 drbd_send_barrier(tconn);
1285 tconn->send.current_epoch_nr = epoch;
1286 }
1287}
1288
Andreas Gruenbacher8f7bed72010-12-19 23:53:14 +01001289int w_send_out_of_sync(struct drbd_work *w, int cancel)
Philipp Reisner73a01a12010-10-27 14:33:00 +02001290{
1291 struct drbd_request *req = container_of(w, struct drbd_request, w);
Philipp Reisner00d56942011-02-09 18:09:48 +01001292 struct drbd_conf *mdev = w->mdev;
Lars Ellenbergb6dd1a82011-11-28 15:04:49 +01001293 struct drbd_tconn *tconn = mdev->tconn;
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001294 int err;
Philipp Reisner73a01a12010-10-27 14:33:00 +02001295
1296 if (unlikely(cancel)) {
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01001297 req_mod(req, SEND_CANCELED);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001298 return 0;
Philipp Reisner73a01a12010-10-27 14:33:00 +02001299 }
1300
Lars Ellenbergb6dd1a82011-11-28 15:04:49 +01001301 /* this time, no tconn->send.current_epoch_writes++;
1302 * If it was sent, it was the closing barrier for the last
1303 * replicated epoch, before we went into AHEAD mode.
1304 * No more barriers will be sent, until we leave AHEAD mode again. */
Lars Ellenberg4eb9b3c2012-08-20 11:05:23 +02001305 maybe_send_barrier(tconn, req->epoch);
Lars Ellenbergb6dd1a82011-11-28 15:04:49 +01001306
Andreas Gruenbacher8f7bed72010-12-19 23:53:14 +01001307 err = drbd_send_out_of_sync(mdev, req);
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01001308 req_mod(req, OOS_HANDED_TO_NETWORK);
Philipp Reisner73a01a12010-10-27 14:33:00 +02001309
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001310 return err;
Philipp Reisner73a01a12010-10-27 14:33:00 +02001311}
1312
Philipp Reisnerb411b362009-09-25 16:07:19 -07001313/**
1314 * w_send_dblock() - Worker callback to send a P_DATA packet in order to mirror a write request
1315 * @mdev: DRBD device.
1316 * @w: work object.
1317 * @cancel: The connection will be closed anyways
1318 */
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001319int w_send_dblock(struct drbd_work *w, int cancel)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001320{
1321 struct drbd_request *req = container_of(w, struct drbd_request, w);
Philipp Reisner00d56942011-02-09 18:09:48 +01001322 struct drbd_conf *mdev = w->mdev;
Lars Ellenbergb6dd1a82011-11-28 15:04:49 +01001323 struct drbd_tconn *tconn = mdev->tconn;
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001324 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001325
1326 if (unlikely(cancel)) {
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01001327 req_mod(req, SEND_CANCELED);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001328 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001329 }
1330
Lars Ellenberg4eb9b3c2012-08-20 11:05:23 +02001331 re_init_if_first_write(tconn, req->epoch);
1332 maybe_send_barrier(tconn, req->epoch);
Lars Ellenbergb6dd1a82011-11-28 15:04:49 +01001333 tconn->send.current_epoch_writes++;
1334
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001335 err = drbd_send_dblock(mdev, req);
1336 req_mod(req, err ? SEND_FAILED : HANDED_OVER_TO_NETWORK);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001337
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001338 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001339}
1340
1341/**
1342 * w_send_read_req() - Worker callback to send a read request (P_DATA_REQUEST) packet
1343 * @mdev: DRBD device.
1344 * @w: work object.
1345 * @cancel: The connection will be closed anyways
1346 */
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001347int w_send_read_req(struct drbd_work *w, int cancel)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001348{
1349 struct drbd_request *req = container_of(w, struct drbd_request, w);
Philipp Reisner00d56942011-02-09 18:09:48 +01001350 struct drbd_conf *mdev = w->mdev;
Lars Ellenbergb6dd1a82011-11-28 15:04:49 +01001351 struct drbd_tconn *tconn = mdev->tconn;
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001352 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001353
1354 if (unlikely(cancel)) {
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01001355 req_mod(req, SEND_CANCELED);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001356 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001357 }
1358
Lars Ellenbergb6dd1a82011-11-28 15:04:49 +01001359 /* Even read requests may close a write epoch,
1360 * if there was any yet. */
Lars Ellenberg4eb9b3c2012-08-20 11:05:23 +02001361 maybe_send_barrier(tconn, req->epoch);
Lars Ellenbergb6dd1a82011-11-28 15:04:49 +01001362
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001363 err = drbd_send_drequest(mdev, P_DATA_REQUEST, req->i.sector, req->i.size,
Andreas Gruenbacher6c1005e2011-03-16 01:34:24 +01001364 (unsigned long)req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001365
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001366 req_mod(req, err ? SEND_FAILED : HANDED_OVER_TO_NETWORK);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001367
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001368 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001369}
1370
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001371int w_restart_disk_io(struct drbd_work *w, int cancel)
Philipp Reisner265be2d2010-05-31 10:14:17 +02001372{
1373 struct drbd_request *req = container_of(w, struct drbd_request, w);
Philipp Reisner00d56942011-02-09 18:09:48 +01001374 struct drbd_conf *mdev = w->mdev;
Philipp Reisner265be2d2010-05-31 10:14:17 +02001375
Philipp Reisner07782862010-08-31 12:00:50 +02001376 if (bio_data_dir(req->master_bio) == WRITE && req->rq_state & RQ_IN_ACT_LOG)
Lars Ellenberg181286a2011-03-31 15:18:56 +02001377 drbd_al_begin_io(mdev, &req->i);
Philipp Reisner265be2d2010-05-31 10:14:17 +02001378
1379 drbd_req_make_private_bio(req, req->master_bio);
1380 req->private_bio->bi_bdev = mdev->ldev->backing_bdev;
1381 generic_make_request(req->private_bio);
1382
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001383 return 0;
Philipp Reisner265be2d2010-05-31 10:14:17 +02001384}
1385
Philipp Reisnerb411b362009-09-25 16:07:19 -07001386static int _drbd_may_sync_now(struct drbd_conf *mdev)
1387{
1388 struct drbd_conf *odev = mdev;
Andreas Gruenbacher95f8efd2011-05-12 11:15:34 +02001389 int resync_after;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001390
1391 while (1) {
Philipp Reisner438c8372011-03-28 14:48:01 +02001392 if (!odev->ldev)
1393 return 1;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001394 rcu_read_lock();
Andreas Gruenbacher95f8efd2011-05-12 11:15:34 +02001395 resync_after = rcu_dereference(odev->ldev->disk_conf)->resync_after;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001396 rcu_read_unlock();
Andreas Gruenbacher95f8efd2011-05-12 11:15:34 +02001397 if (resync_after == -1)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001398 return 1;
Andreas Gruenbacher95f8efd2011-05-12 11:15:34 +02001399 odev = minor_to_mdev(resync_after);
Andreas Gruenbacher841ce242010-12-15 19:31:20 +01001400 if (!expect(odev))
1401 return 1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001402 if ((odev->state.conn >= C_SYNC_SOURCE &&
1403 odev->state.conn <= C_PAUSED_SYNC_T) ||
1404 odev->state.aftr_isp || odev->state.peer_isp ||
1405 odev->state.user_isp)
1406 return 0;
1407 }
1408}
1409
1410/**
1411 * _drbd_pause_after() - Pause resync on all devices that may not resync now
1412 * @mdev: DRBD device.
1413 *
1414 * Called from process context only (admin command and after_state_ch).
1415 */
1416static int _drbd_pause_after(struct drbd_conf *mdev)
1417{
1418 struct drbd_conf *odev;
1419 int i, rv = 0;
1420
Philipp Reisner695d08f2011-04-11 22:53:32 -07001421 rcu_read_lock();
Philipp Reisner81a5d602011-02-22 19:53:16 -05001422 idr_for_each_entry(&minors, odev, i) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001423 if (odev->state.conn == C_STANDALONE && odev->state.disk == D_DISKLESS)
1424 continue;
1425 if (!_drbd_may_sync_now(odev))
1426 rv |= (__drbd_set_state(_NS(odev, aftr_isp, 1), CS_HARD, NULL)
1427 != SS_NOTHING_TO_DO);
1428 }
Philipp Reisner695d08f2011-04-11 22:53:32 -07001429 rcu_read_unlock();
Philipp Reisnerb411b362009-09-25 16:07:19 -07001430
1431 return rv;
1432}
1433
1434/**
1435 * _drbd_resume_next() - Resume resync on all devices that may resync now
1436 * @mdev: DRBD device.
1437 *
1438 * Called from process context only (admin command and worker).
1439 */
1440static int _drbd_resume_next(struct drbd_conf *mdev)
1441{
1442 struct drbd_conf *odev;
1443 int i, rv = 0;
1444
Philipp Reisner695d08f2011-04-11 22:53:32 -07001445 rcu_read_lock();
Philipp Reisner81a5d602011-02-22 19:53:16 -05001446 idr_for_each_entry(&minors, odev, i) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001447 if (odev->state.conn == C_STANDALONE && odev->state.disk == D_DISKLESS)
1448 continue;
1449 if (odev->state.aftr_isp) {
1450 if (_drbd_may_sync_now(odev))
1451 rv |= (__drbd_set_state(_NS(odev, aftr_isp, 0),
1452 CS_HARD, NULL)
1453 != SS_NOTHING_TO_DO) ;
1454 }
1455 }
Philipp Reisner695d08f2011-04-11 22:53:32 -07001456 rcu_read_unlock();
Philipp Reisnerb411b362009-09-25 16:07:19 -07001457 return rv;
1458}
1459
1460void resume_next_sg(struct drbd_conf *mdev)
1461{
1462 write_lock_irq(&global_state_lock);
1463 _drbd_resume_next(mdev);
1464 write_unlock_irq(&global_state_lock);
1465}
1466
1467void suspend_other_sg(struct drbd_conf *mdev)
1468{
1469 write_lock_irq(&global_state_lock);
1470 _drbd_pause_after(mdev);
1471 write_unlock_irq(&global_state_lock);
1472}
1473
Philipp Reisnerdc97b702011-05-03 14:27:15 +02001474/* caller must hold global_state_lock */
Andreas Gruenbacher95f8efd2011-05-12 11:15:34 +02001475enum drbd_ret_code drbd_resync_after_valid(struct drbd_conf *mdev, int o_minor)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001476{
1477 struct drbd_conf *odev;
Andreas Gruenbacher95f8efd2011-05-12 11:15:34 +02001478 int resync_after;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001479
1480 if (o_minor == -1)
1481 return NO_ERROR;
1482 if (o_minor < -1 || minor_to_mdev(o_minor) == NULL)
Andreas Gruenbacher95f8efd2011-05-12 11:15:34 +02001483 return ERR_RESYNC_AFTER;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001484
1485 /* check for loops */
1486 odev = minor_to_mdev(o_minor);
1487 while (1) {
1488 if (odev == mdev)
Andreas Gruenbacher95f8efd2011-05-12 11:15:34 +02001489 return ERR_RESYNC_AFTER_CYCLE;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001490
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001491 rcu_read_lock();
Andreas Gruenbacher95f8efd2011-05-12 11:15:34 +02001492 resync_after = rcu_dereference(odev->ldev->disk_conf)->resync_after;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001493 rcu_read_unlock();
Philipp Reisnerb411b362009-09-25 16:07:19 -07001494 /* dependency chain ends here, no cycles. */
Andreas Gruenbacher95f8efd2011-05-12 11:15:34 +02001495 if (resync_after == -1)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001496 return NO_ERROR;
1497
1498 /* follow the dependency chain */
Andreas Gruenbacher95f8efd2011-05-12 11:15:34 +02001499 odev = minor_to_mdev(resync_after);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001500 }
1501}
1502
Philipp Reisnerdc97b702011-05-03 14:27:15 +02001503/* caller must hold global_state_lock */
Andreas Gruenbacher95f8efd2011-05-12 11:15:34 +02001504void drbd_resync_after_changed(struct drbd_conf *mdev)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001505{
1506 int changes;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001507
Philipp Reisnerdc97b702011-05-03 14:27:15 +02001508 do {
1509 changes = _drbd_pause_after(mdev);
1510 changes |= _drbd_resume_next(mdev);
1511 } while (changes);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001512}
1513
Lars Ellenberg9bd28d32010-11-05 09:55:18 +01001514void drbd_rs_controller_reset(struct drbd_conf *mdev)
1515{
Philipp Reisner813472c2011-05-03 16:47:02 +02001516 struct fifo_buffer *plan;
1517
Lars Ellenberg9bd28d32010-11-05 09:55:18 +01001518 atomic_set(&mdev->rs_sect_in, 0);
1519 atomic_set(&mdev->rs_sect_ev, 0);
1520 mdev->rs_in_flight = 0;
Philipp Reisner813472c2011-05-03 16:47:02 +02001521
1522 /* Updating the RCU protected object in place is necessary since
1523 this function gets called from atomic context.
1524 It is valid since all other updates also lead to an completely
1525 empty fifo */
1526 rcu_read_lock();
1527 plan = rcu_dereference(mdev->rs_plan_s);
1528 plan->total = 0;
1529 fifo_set(plan, 0);
1530 rcu_read_unlock();
Lars Ellenberg9bd28d32010-11-05 09:55:18 +01001531}
1532
Philipp Reisner1f04af32011-02-07 11:33:59 +01001533void start_resync_timer_fn(unsigned long data)
1534{
1535 struct drbd_conf *mdev = (struct drbd_conf *) data;
1536
Lars Ellenbergd5b27b02011-11-14 15:42:37 +01001537 drbd_queue_work(&mdev->tconn->sender_work, &mdev->start_resync_work);
Philipp Reisner1f04af32011-02-07 11:33:59 +01001538}
1539
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001540int w_start_resync(struct drbd_work *w, int cancel)
Philipp Reisner1f04af32011-02-07 11:33:59 +01001541{
Philipp Reisner00d56942011-02-09 18:09:48 +01001542 struct drbd_conf *mdev = w->mdev;
1543
Philipp Reisner1f04af32011-02-07 11:33:59 +01001544 if (atomic_read(&mdev->unacked_cnt) || atomic_read(&mdev->rs_pending_cnt)) {
1545 dev_warn(DEV, "w_start_resync later...\n");
1546 mdev->start_resync_timer.expires = jiffies + HZ/10;
1547 add_timer(&mdev->start_resync_timer);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001548 return 0;
Philipp Reisner1f04af32011-02-07 11:33:59 +01001549 }
1550
1551 drbd_start_resync(mdev, C_SYNC_SOURCE);
Philipp Reisner36baf612011-11-10 14:27:34 +01001552 clear_bit(AHEAD_TO_SYNC_SOURCE, &mdev->flags);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001553 return 0;
Philipp Reisner1f04af32011-02-07 11:33:59 +01001554}
1555
Philipp Reisnerb411b362009-09-25 16:07:19 -07001556/**
1557 * drbd_start_resync() - Start the resync process
1558 * @mdev: DRBD device.
1559 * @side: Either C_SYNC_SOURCE or C_SYNC_TARGET
1560 *
1561 * This function might bring you directly into one of the
1562 * C_PAUSED_SYNC_* states.
1563 */
1564void drbd_start_resync(struct drbd_conf *mdev, enum drbd_conns side)
1565{
1566 union drbd_state ns;
1567 int r;
1568
Philipp Reisnerc4752ef2010-10-27 17:32:36 +02001569 if (mdev->state.conn >= C_SYNC_SOURCE && mdev->state.conn < C_AHEAD) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001570 dev_err(DEV, "Resync already running!\n");
1571 return;
1572 }
1573
Philipp Reisnere64a3292011-02-05 17:34:11 +01001574 if (!test_bit(B_RS_H_DONE, &mdev->flags)) {
1575 if (side == C_SYNC_TARGET) {
1576 /* Since application IO was locked out during C_WF_BITMAP_T and
1577 C_WF_SYNC_UUID we are still unmodified. Before going to C_SYNC_TARGET
1578 we check that we might make the data inconsistent. */
1579 r = drbd_khelper(mdev, "before-resync-target");
1580 r = (r >> 8) & 0xff;
1581 if (r > 0) {
1582 dev_info(DEV, "before-resync-target handler returned %d, "
Philipp Reisner09b9e792010-12-03 16:04:24 +01001583 "dropping connection.\n", r);
Philipp Reisner38fa9982011-03-15 18:24:49 +01001584 conn_request_state(mdev->tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Philipp Reisner09b9e792010-12-03 16:04:24 +01001585 return;
1586 }
Philipp Reisnere64a3292011-02-05 17:34:11 +01001587 } else /* C_SYNC_SOURCE */ {
1588 r = drbd_khelper(mdev, "before-resync-source");
1589 r = (r >> 8) & 0xff;
1590 if (r > 0) {
1591 if (r == 3) {
1592 dev_info(DEV, "before-resync-source handler returned %d, "
1593 "ignoring. Old userland tools?", r);
1594 } else {
1595 dev_info(DEV, "before-resync-source handler returned %d, "
1596 "dropping connection.\n", r);
Philipp Reisner38fa9982011-03-15 18:24:49 +01001597 conn_request_state(mdev->tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Philipp Reisnere64a3292011-02-05 17:34:11 +01001598 return;
1599 }
1600 }
Philipp Reisner09b9e792010-12-03 16:04:24 +01001601 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001602 }
1603
Philipp Reisnere64a3292011-02-05 17:34:11 +01001604 if (current == mdev->tconn->worker.task) {
Philipp Reisnerdad20552011-02-11 19:43:55 +01001605 /* The worker should not sleep waiting for state_mutex,
Philipp Reisnere64a3292011-02-05 17:34:11 +01001606 that can take long */
Philipp Reisner8410da82011-02-11 20:11:10 +01001607 if (!mutex_trylock(mdev->state_mutex)) {
Philipp Reisnere64a3292011-02-05 17:34:11 +01001608 set_bit(B_RS_H_DONE, &mdev->flags);
1609 mdev->start_resync_timer.expires = jiffies + HZ/5;
1610 add_timer(&mdev->start_resync_timer);
1611 return;
1612 }
1613 } else {
Philipp Reisner8410da82011-02-11 20:11:10 +01001614 mutex_lock(mdev->state_mutex);
Philipp Reisnere64a3292011-02-05 17:34:11 +01001615 }
1616 clear_bit(B_RS_H_DONE, &mdev->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001617
Philipp Reisner0cfac5d2011-11-10 12:12:52 +01001618 write_lock_irq(&global_state_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001619 if (!get_ldev_if_state(mdev, D_NEGOTIATING)) {
Philipp Reisner0cfac5d2011-11-10 12:12:52 +01001620 write_unlock_irq(&global_state_lock);
Philipp Reisner8410da82011-02-11 20:11:10 +01001621 mutex_unlock(mdev->state_mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001622 return;
1623 }
1624
Philipp Reisner78bae592011-03-28 15:40:12 +02001625 ns = drbd_read_state(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001626
1627 ns.aftr_isp = !_drbd_may_sync_now(mdev);
1628
1629 ns.conn = side;
1630
1631 if (side == C_SYNC_TARGET)
1632 ns.disk = D_INCONSISTENT;
1633 else /* side == C_SYNC_SOURCE */
1634 ns.pdsk = D_INCONSISTENT;
1635
1636 r = __drbd_set_state(mdev, ns, CS_VERBOSE, NULL);
Philipp Reisner78bae592011-03-28 15:40:12 +02001637 ns = drbd_read_state(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001638
1639 if (ns.conn < C_CONNECTED)
1640 r = SS_UNKNOWN_ERROR;
1641
1642 if (r == SS_SUCCESS) {
Lars Ellenberg1d7734a2010-08-11 21:21:50 +02001643 unsigned long tw = drbd_bm_total_weight(mdev);
1644 unsigned long now = jiffies;
1645 int i;
1646
Philipp Reisnerb411b362009-09-25 16:07:19 -07001647 mdev->rs_failed = 0;
1648 mdev->rs_paused = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001649 mdev->rs_same_csum = 0;
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02001650 mdev->rs_last_events = 0;
1651 mdev->rs_last_sect_ev = 0;
Lars Ellenberg1d7734a2010-08-11 21:21:50 +02001652 mdev->rs_total = tw;
1653 mdev->rs_start = now;
1654 for (i = 0; i < DRBD_SYNC_MARKS; i++) {
1655 mdev->rs_mark_left[i] = tw;
1656 mdev->rs_mark_time[i] = now;
1657 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001658 _drbd_pause_after(mdev);
1659 }
1660 write_unlock_irq(&global_state_lock);
Lars Ellenberg5a22db82010-12-17 21:14:23 +01001661
Philipp Reisnerb411b362009-09-25 16:07:19 -07001662 if (r == SS_SUCCESS) {
1663 dev_info(DEV, "Began resync as %s (will sync %lu KB [%lu bits set]).\n",
1664 drbd_conn_str(ns.conn),
1665 (unsigned long) mdev->rs_total << (BM_BLOCK_SHIFT-10),
1666 (unsigned long) mdev->rs_total);
Lars Ellenberg6c922ed2011-01-12 11:51:13 +01001667 if (side == C_SYNC_TARGET)
1668 mdev->bm_resync_fo = 0;
1669
1670 /* Since protocol 96, we must serialize drbd_gen_and_send_sync_uuid
1671 * with w_send_oos, or the sync target will get confused as to
1672 * how much bits to resync. We cannot do that always, because for an
1673 * empty resync and protocol < 95, we need to do it here, as we call
1674 * drbd_resync_finished from here in that case.
1675 * We drbd_gen_and_send_sync_uuid here for protocol < 96,
1676 * and from after_state_ch otherwise. */
Philipp Reisner31890f42011-01-19 14:12:51 +01001677 if (side == C_SYNC_SOURCE && mdev->tconn->agreed_pro_version < 96)
Lars Ellenberg6c922ed2011-01-12 11:51:13 +01001678 drbd_gen_and_send_sync_uuid(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001679
Philipp Reisner31890f42011-01-19 14:12:51 +01001680 if (mdev->tconn->agreed_pro_version < 95 && mdev->rs_total == 0) {
Lars Ellenbergaf85e8e2010-10-07 16:07:55 +02001681 /* This still has a race (about when exactly the peers
1682 * detect connection loss) that can lead to a full sync
1683 * on next handshake. In 8.3.9 we fixed this with explicit
1684 * resync-finished notifications, but the fix
1685 * introduces a protocol change. Sleeping for some
1686 * time longer than the ping interval + timeout on the
1687 * SyncSource, to give the SyncTarget the chance to
1688 * detect connection loss, then waiting for a ping
1689 * response (implicit in drbd_resync_finished) reduces
1690 * the race considerably, but does not solve it. */
Philipp Reisner44ed1672011-04-19 17:10:19 +02001691 if (side == C_SYNC_SOURCE) {
1692 struct net_conf *nc;
1693 int timeo;
1694
1695 rcu_read_lock();
1696 nc = rcu_dereference(mdev->tconn->net_conf);
1697 timeo = nc->ping_int * HZ + nc->ping_timeo * HZ / 9;
1698 rcu_read_unlock();
1699 schedule_timeout_interruptible(timeo);
1700 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001701 drbd_resync_finished(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001702 }
1703
Lars Ellenberg9bd28d32010-11-05 09:55:18 +01001704 drbd_rs_controller_reset(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001705 /* ns.conn may already be != mdev->state.conn,
1706 * we may have been paused in between, or become paused until
1707 * the timer triggers.
1708 * No matter, that is handled in resync_timer_fn() */
1709 if (ns.conn == C_SYNC_TARGET)
1710 mod_timer(&mdev->resync_timer, jiffies);
1711
1712 drbd_md_sync(mdev);
1713 }
Lars Ellenberg5a22db82010-12-17 21:14:23 +01001714 put_ldev(mdev);
Philipp Reisner8410da82011-02-11 20:11:10 +01001715 mutex_unlock(mdev->state_mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001716}
1717
Lars Ellenbergb6dd1a82011-11-28 15:04:49 +01001718/* If the resource already closed the current epoch, but we did not
1719 * (because we have not yet seen new requests), we should send the
1720 * corresponding barrier now. Must be checked within the same spinlock
1721 * that is used to check for new requests. */
1722bool need_to_send_barrier(struct drbd_tconn *connection)
1723{
1724 if (!connection->send.seen_any_write_yet)
1725 return false;
1726
1727 /* Skip barriers that do not contain any writes.
1728 * This may happen during AHEAD mode. */
1729 if (!connection->send.current_epoch_writes)
1730 return false;
1731
1732 /* ->req_lock is held when requests are queued on
1733 * connection->sender_work, and put into ->transfer_log.
1734 * It is also held when ->current_tle_nr is increased.
1735 * So either there are already new requests queued,
1736 * and corresponding barriers will be send there.
1737 * Or nothing new is queued yet, so the difference will be 1.
1738 */
1739 if (atomic_read(&connection->current_tle_nr) !=
1740 connection->send.current_epoch_nr + 1)
1741 return false;
1742
1743 return true;
1744}
1745
Lars Ellenberg8c0785a2011-10-19 11:50:57 +02001746bool dequeue_work_batch(struct drbd_work_queue *queue, struct list_head *work_list)
1747{
1748 spin_lock_irq(&queue->q_lock);
1749 list_splice_init(&queue->q, work_list);
1750 spin_unlock_irq(&queue->q_lock);
1751 return !list_empty(work_list);
1752}
1753
1754bool dequeue_work_item(struct drbd_work_queue *queue, struct list_head *work_list)
1755{
1756 spin_lock_irq(&queue->q_lock);
1757 if (!list_empty(&queue->q))
1758 list_move(queue->q.next, work_list);
1759 spin_unlock_irq(&queue->q_lock);
1760 return !list_empty(work_list);
1761}
1762
Lars Ellenbergb6dd1a82011-11-28 15:04:49 +01001763void wait_for_work(struct drbd_tconn *connection, struct list_head *work_list)
1764{
1765 DEFINE_WAIT(wait);
1766 struct net_conf *nc;
1767 int uncork, cork;
1768
1769 dequeue_work_item(&connection->sender_work, work_list);
1770 if (!list_empty(work_list))
1771 return;
1772
1773 /* Still nothing to do?
1774 * Maybe we still need to close the current epoch,
1775 * even if no new requests are queued yet.
1776 *
1777 * Also, poke TCP, just in case.
1778 * Then wait for new work (or signal). */
1779 rcu_read_lock();
1780 nc = rcu_dereference(connection->net_conf);
1781 uncork = nc ? nc->tcp_cork : 0;
1782 rcu_read_unlock();
1783 if (uncork) {
1784 mutex_lock(&connection->data.mutex);
1785 if (connection->data.socket)
1786 drbd_tcp_uncork(connection->data.socket);
1787 mutex_unlock(&connection->data.mutex);
1788 }
1789
1790 for (;;) {
1791 int send_barrier;
1792 prepare_to_wait(&connection->sender_work.q_wait, &wait, TASK_INTERRUPTIBLE);
1793 spin_lock_irq(&connection->req_lock);
1794 spin_lock(&connection->sender_work.q_lock); /* FIXME get rid of this one? */
1795 list_splice_init(&connection->sender_work.q, work_list);
1796 spin_unlock(&connection->sender_work.q_lock); /* FIXME get rid of this one? */
1797 if (!list_empty(work_list) || signal_pending(current)) {
1798 spin_unlock_irq(&connection->req_lock);
1799 break;
1800 }
1801 send_barrier = need_to_send_barrier(connection);
1802 spin_unlock_irq(&connection->req_lock);
1803 if (send_barrier) {
1804 drbd_send_barrier(connection);
1805 connection->send.current_epoch_nr++;
1806 }
1807 schedule();
1808 /* may be woken up for other things but new work, too,
1809 * e.g. if the current epoch got closed.
1810 * In which case we send the barrier above. */
1811 }
1812 finish_wait(&connection->sender_work.q_wait, &wait);
1813
1814 /* someone may have changed the config while we have been waiting above. */
1815 rcu_read_lock();
1816 nc = rcu_dereference(connection->net_conf);
1817 cork = nc ? nc->tcp_cork : 0;
1818 rcu_read_unlock();
1819 mutex_lock(&connection->data.mutex);
1820 if (connection->data.socket) {
1821 if (cork)
1822 drbd_tcp_cork(connection->data.socket);
1823 else if (!uncork)
1824 drbd_tcp_uncork(connection->data.socket);
1825 }
1826 mutex_unlock(&connection->data.mutex);
1827}
1828
Philipp Reisnerb411b362009-09-25 16:07:19 -07001829int drbd_worker(struct drbd_thread *thi)
1830{
Philipp Reisner392c8802011-02-09 10:33:31 +01001831 struct drbd_tconn *tconn = thi->tconn;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001832 struct drbd_work *w = NULL;
Philipp Reisner0e29d162011-02-18 14:23:11 +01001833 struct drbd_conf *mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001834 LIST_HEAD(work_list);
Lars Ellenberg8c0785a2011-10-19 11:50:57 +02001835 int vnr;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001836
Andreas Gruenbachere77a0a52011-01-25 15:43:39 +01001837 while (get_t_state(thi) == RUNNING) {
Philipp Reisner80822282011-02-08 12:46:30 +01001838 drbd_thread_current_set_cpu(thi);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001839
Lars Ellenberg8c0785a2011-10-19 11:50:57 +02001840 /* as long as we use drbd_queue_work_front(),
1841 * we may only dequeue single work items here, not batches. */
1842 if (list_empty(&work_list))
Lars Ellenbergb6dd1a82011-11-28 15:04:49 +01001843 wait_for_work(tconn, &work_list);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001844
Lars Ellenberg8c0785a2011-10-19 11:50:57 +02001845 if (signal_pending(current)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001846 flush_signals(current);
Philipp Reisner19393e12011-02-09 10:09:07 +01001847 if (get_t_state(thi) == RUNNING) {
1848 conn_warn(tconn, "Worker got an unexpected signal\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07001849 continue;
Philipp Reisner19393e12011-02-09 10:09:07 +01001850 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001851 break;
1852 }
1853
Andreas Gruenbachere77a0a52011-01-25 15:43:39 +01001854 if (get_t_state(thi) != RUNNING)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001855 break;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001856
Lars Ellenberg8c0785a2011-10-19 11:50:57 +02001857 while (!list_empty(&work_list)) {
1858 w = list_first_entry(&work_list, struct drbd_work, list);
1859 list_del_init(&w->list);
1860 if (w->cb(w, tconn->cstate < C_WF_REPORT_PARAMS) == 0)
1861 continue;
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001862 if (tconn->cstate >= C_WF_REPORT_PARAMS)
1863 conn_request_state(tconn, NS(conn, C_NETWORK_FAILURE), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001864 }
1865 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001866
Lars Ellenberg8c0785a2011-10-19 11:50:57 +02001867 do {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001868 while (!list_empty(&work_list)) {
Lars Ellenberg8c0785a2011-10-19 11:50:57 +02001869 w = list_first_entry(&work_list, struct drbd_work, list);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001870 list_del_init(&w->list);
Philipp Reisner00d56942011-02-09 18:09:48 +01001871 w->cb(w, 1);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001872 }
Lars Ellenbergd5b27b02011-11-14 15:42:37 +01001873 dequeue_work_batch(&tconn->sender_work, &work_list);
Lars Ellenberg8c0785a2011-10-19 11:50:57 +02001874 } while (!list_empty(&work_list));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001875
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02001876 rcu_read_lock();
Lars Ellenbergf3990022011-03-23 14:31:09 +01001877 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
Philipp Reisner0e29d162011-02-18 14:23:11 +01001878 D_ASSERT(mdev->state.disk == D_DISKLESS && mdev->state.conn == C_STANDALONE);
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02001879 kref_get(&mdev->kref);
1880 rcu_read_unlock();
Philipp Reisner0e29d162011-02-18 14:23:11 +01001881 drbd_mdev_cleanup(mdev);
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02001882 kref_put(&mdev->kref, &drbd_minor_destroy);
1883 rcu_read_lock();
Philipp Reisner0e29d162011-02-18 14:23:11 +01001884 }
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02001885 rcu_read_unlock();
Philipp Reisnerb411b362009-09-25 16:07:19 -07001886
1887 return 0;
1888}