blob: eeda8b8e9d8e49bd407867b872b8c776d7ba5130 [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 Reisner1b6dd252012-09-04 15:16:20 +0200224
225 /* If this request was aborted locally before,
226 * but now was completed "successfully",
227 * chances are that this caused arbitrary data corruption.
228 *
229 * "aborting" requests, or force-detaching the disk, is intended for
230 * completely blocked/hung local backing devices which do no longer
231 * complete requests at all, not even do error completions. In this
232 * situation, usually a hard-reset and failover is the only way out.
233 *
234 * By "aborting", basically faking a local error-completion,
235 * we allow for a more graceful swichover by cleanly migrating services.
236 * Still the affected node has to be rebooted "soon".
237 *
238 * By completing these requests, we allow the upper layers to re-use
239 * the associated data pages.
240 *
241 * If later the local backing device "recovers", and now DMAs some data
242 * from disk into the original request pages, in the best case it will
243 * just put random data into unused pages; but typically it will corrupt
244 * meanwhile completely unrelated data, causing all sorts of damage.
245 *
246 * Which means delayed successful completion,
247 * especially for READ requests,
248 * is a reason to panic().
249 *
250 * We assume that a delayed *error* completion is OK,
251 * though we still will complain noisily about it.
252 */
253 if (unlikely(req->rq_state & RQ_LOCAL_ABORTED)) {
254 if (__ratelimit(&drbd_ratelimit_state))
255 dev_emerg(DEV, "delayed completion of aborted local request; disk-timeout may be too aggressive\n");
256
257 if (!error)
258 panic("possible random memory corruption caused by delayed completion of aborted local request\n");
259 }
260
Philipp Reisnerb411b362009-09-25 16:07:19 -0700261 /* to avoid recursion in __req_mod */
262 if (unlikely(error)) {
263 what = (bio_data_dir(bio) == WRITE)
Andreas Gruenbacher8554df12011-01-25 15:37:43 +0100264 ? WRITE_COMPLETED_WITH_ERROR
Lars Ellenberg5c3c7e62010-04-10 02:10:09 +0200265 : (bio_rw(bio) == READ)
Andreas Gruenbacher8554df12011-01-25 15:37:43 +0100266 ? READ_COMPLETED_WITH_ERROR
267 : READ_AHEAD_COMPLETED_WITH_ERROR;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700268 } else
Andreas Gruenbacher8554df12011-01-25 15:37:43 +0100269 what = COMPLETED_OK;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700270
271 bio_put(req->private_bio);
272 req->private_bio = ERR_PTR(error);
273
Lars Ellenberga1154132010-11-13 20:42:29 +0100274 /* not req_mod(), we need irqsave here! */
Philipp Reisner87eeee42011-01-19 14:16:30 +0100275 spin_lock_irqsave(&mdev->tconn->req_lock, flags);
Lars Ellenberga1154132010-11-13 20:42:29 +0100276 __req_mod(req, what, &m);
Philipp Reisner87eeee42011-01-19 14:16:30 +0100277 spin_unlock_irqrestore(&mdev->tconn->req_lock, flags);
Lars Ellenberg24153082012-03-26 17:06:29 +0200278 put_ldev(mdev);
Lars Ellenberga1154132010-11-13 20:42:29 +0100279
280 if (m.bio)
281 complete_master_bio(mdev, &m);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700282}
283
Andreas Gruenbacherf6ffca92011-02-04 15:30:34 +0100284void drbd_csum_ee(struct drbd_conf *mdev, struct crypto_hash *tfm,
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100285 struct drbd_peer_request *peer_req, void *digest)
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200286{
287 struct hash_desc desc;
288 struct scatterlist sg;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100289 struct page *page = peer_req->pages;
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200290 struct page *tmp;
291 unsigned len;
292
293 desc.tfm = tfm;
294 desc.flags = 0;
295
296 sg_init_table(&sg, 1);
297 crypto_hash_init(&desc);
298
299 while ((tmp = page_chain_next(page))) {
300 /* all but the last page will be fully used */
301 sg_set_page(&sg, page, PAGE_SIZE, 0);
302 crypto_hash_update(&desc, &sg, sg.length);
303 page = tmp;
304 }
305 /* and now the last, possibly only partially used page */
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100306 len = peer_req->i.size & (PAGE_SIZE - 1);
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200307 sg_set_page(&sg, page, len ?: PAGE_SIZE, 0);
308 crypto_hash_update(&desc, &sg, sg.length);
309 crypto_hash_final(&desc, digest);
310}
311
312void drbd_csum_bio(struct drbd_conf *mdev, struct crypto_hash *tfm, struct bio *bio, void *digest)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700313{
314 struct hash_desc desc;
315 struct scatterlist sg;
316 struct bio_vec *bvec;
317 int i;
318
319 desc.tfm = tfm;
320 desc.flags = 0;
321
322 sg_init_table(&sg, 1);
323 crypto_hash_init(&desc);
324
Lars Ellenberg4b8514e2012-03-26 16:12:49 +0200325 bio_for_each_segment(bvec, bio, i) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700326 sg_set_page(&sg, bvec->bv_page, bvec->bv_len, bvec->bv_offset);
327 crypto_hash_update(&desc, &sg, sg.length);
328 }
329 crypto_hash_final(&desc, digest);
330}
331
Lars Ellenberg9676c762011-02-22 14:02:31 +0100332/* MAYBE merge common code with w_e_end_ov_req */
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +0100333static int w_e_send_csum(struct drbd_work *w, int cancel)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700334{
Philipp Reisner00d56942011-02-09 18:09:48 +0100335 struct drbd_peer_request *peer_req = container_of(w, struct drbd_peer_request, w);
336 struct drbd_conf *mdev = w->mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700337 int digest_size;
338 void *digest;
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +0100339 int err = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700340
Lars Ellenberg53ea4332011-03-08 17:11:40 +0100341 if (unlikely(cancel))
342 goto out;
343
Lars Ellenberg9676c762011-02-22 14:02:31 +0100344 if (unlikely((peer_req->flags & EE_WAS_ERROR) != 0))
Lars Ellenberg53ea4332011-03-08 17:11:40 +0100345 goto out;
346
Lars Ellenbergf3990022011-03-23 14:31:09 +0100347 digest_size = crypto_hash_digestsize(mdev->tconn->csums_tfm);
Lars Ellenberg53ea4332011-03-08 17:11:40 +0100348 digest = kmalloc(digest_size, GFP_NOIO);
349 if (digest) {
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100350 sector_t sector = peer_req->i.sector;
351 unsigned int size = peer_req->i.size;
Lars Ellenbergf3990022011-03-23 14:31:09 +0100352 drbd_csum_ee(mdev, mdev->tconn->csums_tfm, peer_req, digest);
Lars Ellenberg9676c762011-02-22 14:02:31 +0100353 /* Free peer_req and pages before send.
Lars Ellenberg53ea4332011-03-08 17:11:40 +0100354 * In case we block on congestion, we could otherwise run into
355 * some distributed deadlock, if the other side blocks on
356 * congestion as well, because our receiver blocks in
Andreas Gruenbacherc37c8ec2011-04-07 21:02:09 +0200357 * drbd_alloc_pages due to pp_in_use > max_buffers. */
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +0200358 drbd_free_peer_req(mdev, peer_req);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100359 peer_req = NULL;
Lars Ellenberg53ea4332011-03-08 17:11:40 +0100360 inc_rs_pending(mdev);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +0100361 err = drbd_send_drequest_csum(mdev, sector, size,
Andreas Gruenbacherdb1b0b72011-03-16 01:37:21 +0100362 digest, digest_size,
363 P_CSUM_RS_REQUEST);
Lars Ellenberg53ea4332011-03-08 17:11:40 +0100364 kfree(digest);
365 } else {
366 dev_err(DEV, "kmalloc() of digest failed.\n");
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +0100367 err = -ENOMEM;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700368 }
369
Lars Ellenberg53ea4332011-03-08 17:11:40 +0100370out:
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100371 if (peer_req)
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +0200372 drbd_free_peer_req(mdev, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700373
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +0100374 if (unlikely(err))
Philipp Reisnerb411b362009-09-25 16:07:19 -0700375 dev_err(DEV, "drbd_send_drequest(..., csum) failed\n");
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +0100376 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700377}
378
379#define GFP_TRY (__GFP_HIGHMEM | __GFP_NOWARN)
380
381static int read_for_csum(struct drbd_conf *mdev, sector_t sector, int size)
382{
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100383 struct drbd_peer_request *peer_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700384
385 if (!get_ldev(mdev))
Lars Ellenberg80a40e42010-08-11 23:28:00 +0200386 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700387
Philipp Reisnere3555d82010-11-07 15:56:29 +0100388 if (drbd_rs_should_slow_down(mdev, sector))
Lars Ellenberg0f0601f2010-08-11 23:40:24 +0200389 goto defer;
390
Philipp Reisnerb411b362009-09-25 16:07:19 -0700391 /* GFP_TRY, because if there is no memory available right now, this may
392 * be rescheduled for later. It is "only" background resync, after all. */
Andreas Gruenbacher0db55362011-04-06 16:09:15 +0200393 peer_req = drbd_alloc_peer_req(mdev, ID_SYNCER /* unused */, sector,
394 size, GFP_TRY);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100395 if (!peer_req)
Lars Ellenberg80a40e42010-08-11 23:28:00 +0200396 goto defer;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700397
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100398 peer_req->w.cb = w_e_send_csum;
Philipp Reisner87eeee42011-01-19 14:16:30 +0100399 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100400 list_add(&peer_req->w.list, &mdev->read_ee);
Philipp Reisner87eeee42011-01-19 14:16:30 +0100401 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700402
Lars Ellenberg0f0601f2010-08-11 23:40:24 +0200403 atomic_add(size >> 9, &mdev->rs_sect_ev);
Andreas Gruenbacherfbe29de2011-02-17 16:38:35 +0100404 if (drbd_submit_peer_request(mdev, peer_req, READ, DRBD_FAULT_RS_RD) == 0)
Lars Ellenberg80a40e42010-08-11 23:28:00 +0200405 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700406
Lars Ellenberg10f6d9922011-01-24 14:47:09 +0100407 /* If it failed because of ENOMEM, retry should help. If it failed
408 * because bio_add_page failed (probably broken lower level driver),
409 * retry may or may not help.
410 * If it does not, you may need to force disconnect. */
Philipp Reisner87eeee42011-01-19 14:16:30 +0100411 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100412 list_del(&peer_req->w.list);
Philipp Reisner87eeee42011-01-19 14:16:30 +0100413 spin_unlock_irq(&mdev->tconn->req_lock);
Lars Ellenberg22cc37a2010-09-14 20:40:41 +0200414
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +0200415 drbd_free_peer_req(mdev, peer_req);
Lars Ellenberg80a40e42010-08-11 23:28:00 +0200416defer:
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200417 put_ldev(mdev);
Lars Ellenberg80a40e42010-08-11 23:28:00 +0200418 return -EAGAIN;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700419}
420
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +0100421int w_resync_timer(struct drbd_work *w, int cancel)
Philipp Reisner794abb72010-12-27 11:51:23 +0100422{
Philipp Reisner00d56942011-02-09 18:09:48 +0100423 struct drbd_conf *mdev = w->mdev;
Philipp Reisner794abb72010-12-27 11:51:23 +0100424 switch (mdev->state.conn) {
425 case C_VERIFY_S:
Philipp Reisner00d56942011-02-09 18:09:48 +0100426 w_make_ov_request(w, cancel);
Philipp Reisner794abb72010-12-27 11:51:23 +0100427 break;
428 case C_SYNC_TARGET:
Philipp Reisner00d56942011-02-09 18:09:48 +0100429 w_make_resync_request(w, cancel);
Philipp Reisner794abb72010-12-27 11:51:23 +0100430 break;
431 }
432
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +0100433 return 0;
Philipp Reisner794abb72010-12-27 11:51:23 +0100434}
435
Philipp Reisnerb411b362009-09-25 16:07:19 -0700436void resync_timer_fn(unsigned long data)
437{
Philipp Reisnerb411b362009-09-25 16:07:19 -0700438 struct drbd_conf *mdev = (struct drbd_conf *) data;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700439
Philipp Reisner794abb72010-12-27 11:51:23 +0100440 if (list_empty(&mdev->resync_work.list))
Lars Ellenbergd5b27b02011-11-14 15:42:37 +0100441 drbd_queue_work(&mdev->tconn->sender_work, &mdev->resync_work);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700442}
443
Philipp Reisner778f2712010-07-06 11:14:00 +0200444static void fifo_set(struct fifo_buffer *fb, int value)
445{
446 int i;
447
448 for (i = 0; i < fb->size; i++)
Philipp Reisnerf10f2622010-10-05 16:50:17 +0200449 fb->values[i] = value;
Philipp Reisner778f2712010-07-06 11:14:00 +0200450}
451
452static int fifo_push(struct fifo_buffer *fb, int value)
453{
454 int ov;
455
456 ov = fb->values[fb->head_index];
457 fb->values[fb->head_index++] = value;
458
459 if (fb->head_index >= fb->size)
460 fb->head_index = 0;
461
462 return ov;
463}
464
465static void fifo_add_val(struct fifo_buffer *fb, int value)
466{
467 int i;
468
469 for (i = 0; i < fb->size; i++)
470 fb->values[i] += value;
471}
472
Philipp Reisner9958c852011-05-03 16:19:31 +0200473struct fifo_buffer *fifo_alloc(int fifo_size)
474{
475 struct fifo_buffer *fb;
476
477 fb = kzalloc(sizeof(struct fifo_buffer) + sizeof(int) * fifo_size, GFP_KERNEL);
478 if (!fb)
479 return NULL;
480
481 fb->head_index = 0;
482 fb->size = fifo_size;
483 fb->total = 0;
484
485 return fb;
486}
487
Philipp Reisner9d77a5f2010-11-07 18:02:56 +0100488static int drbd_rs_controller(struct drbd_conf *mdev)
Philipp Reisner778f2712010-07-06 11:14:00 +0200489{
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +0200490 struct disk_conf *dc;
Philipp Reisner778f2712010-07-06 11:14:00 +0200491 unsigned int sect_in; /* Number of sectors that came in since the last turn */
492 unsigned int want; /* The number of sectors we want in the proxy */
493 int req_sect; /* Number of sectors to request in this turn */
494 int correction; /* Number of sectors more we need in the proxy*/
495 int cps; /* correction per invocation of drbd_rs_controller() */
496 int steps; /* Number of time steps to plan ahead */
497 int curr_corr;
498 int max_sect;
Philipp Reisner813472c2011-05-03 16:47:02 +0200499 struct fifo_buffer *plan;
Philipp Reisner778f2712010-07-06 11:14:00 +0200500
501 sect_in = atomic_xchg(&mdev->rs_sect_in, 0); /* Number of sectors that came in */
502 mdev->rs_in_flight -= sect_in;
503
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +0200504 dc = rcu_dereference(mdev->ldev->disk_conf);
Philipp Reisner813472c2011-05-03 16:47:02 +0200505 plan = rcu_dereference(mdev->rs_plan_s);
Philipp Reisner778f2712010-07-06 11:14:00 +0200506
Philipp Reisner813472c2011-05-03 16:47:02 +0200507 steps = plan->size; /* (dc->c_plan_ahead * 10 * SLEEP_TIME) / HZ; */
Philipp Reisner778f2712010-07-06 11:14:00 +0200508
509 if (mdev->rs_in_flight + sect_in == 0) { /* At start of resync */
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +0200510 want = ((dc->resync_rate * 2 * SLEEP_TIME) / HZ) * steps;
Philipp Reisner778f2712010-07-06 11:14:00 +0200511 } else { /* normal path */
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +0200512 want = dc->c_fill_target ? dc->c_fill_target :
513 sect_in * dc->c_delay_target * HZ / (SLEEP_TIME * 10);
Philipp Reisner778f2712010-07-06 11:14:00 +0200514 }
515
Philipp Reisner813472c2011-05-03 16:47:02 +0200516 correction = want - mdev->rs_in_flight - plan->total;
Philipp Reisner778f2712010-07-06 11:14:00 +0200517
518 /* Plan ahead */
519 cps = correction / steps;
Philipp Reisner813472c2011-05-03 16:47:02 +0200520 fifo_add_val(plan, cps);
521 plan->total += cps * steps;
Philipp Reisner778f2712010-07-06 11:14:00 +0200522
523 /* What we do in this step */
Philipp Reisner813472c2011-05-03 16:47:02 +0200524 curr_corr = fifo_push(plan, 0);
525 plan->total -= curr_corr;
Philipp Reisner778f2712010-07-06 11:14:00 +0200526
527 req_sect = sect_in + curr_corr;
528 if (req_sect < 0)
529 req_sect = 0;
530
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +0200531 max_sect = (dc->c_max_rate * 2 * SLEEP_TIME) / HZ;
Philipp Reisner778f2712010-07-06 11:14:00 +0200532 if (req_sect > max_sect)
533 req_sect = max_sect;
534
535 /*
536 dev_warn(DEV, "si=%u if=%d wa=%u co=%d st=%d cps=%d pl=%d cc=%d rs=%d\n",
537 sect_in, mdev->rs_in_flight, want, correction,
538 steps, cps, mdev->rs_planed, curr_corr, req_sect);
539 */
540
541 return req_sect;
542}
543
Philipp Reisner9d77a5f2010-11-07 18:02:56 +0100544static int drbd_rs_number_requests(struct drbd_conf *mdev)
Lars Ellenberge65f4402010-11-05 10:04:07 +0100545{
546 int number;
Philipp Reisner813472c2011-05-03 16:47:02 +0200547
548 rcu_read_lock();
549 if (rcu_dereference(mdev->rs_plan_s)->size) {
Lars Ellenberge65f4402010-11-05 10:04:07 +0100550 number = drbd_rs_controller(mdev) >> (BM_BLOCK_SHIFT - 9);
551 mdev->c_sync_rate = number * HZ * (BM_BLOCK_SIZE / 1024) / SLEEP_TIME;
552 } else {
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +0200553 mdev->c_sync_rate = rcu_dereference(mdev->ldev->disk_conf)->resync_rate;
Lars Ellenberge65f4402010-11-05 10:04:07 +0100554 number = SLEEP_TIME * mdev->c_sync_rate / ((BM_BLOCK_SIZE / 1024) * HZ);
555 }
Philipp Reisner813472c2011-05-03 16:47:02 +0200556 rcu_read_unlock();
Lars Ellenberge65f4402010-11-05 10:04:07 +0100557
Lars Ellenberge65f4402010-11-05 10:04:07 +0100558 /* ignore the amount of pending requests, the resync controller should
559 * throttle down to incoming reply rate soon enough anyways. */
560 return number;
561}
562
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +0100563int w_make_resync_request(struct drbd_work *w, int cancel)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700564{
Philipp Reisner00d56942011-02-09 18:09:48 +0100565 struct drbd_conf *mdev = w->mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700566 unsigned long bit;
567 sector_t sector;
568 const sector_t capacity = drbd_get_capacity(mdev->this_bdev);
Lars Ellenberg1816a2b2010-11-11 15:19:07 +0100569 int max_bio_size;
Lars Ellenberge65f4402010-11-05 10:04:07 +0100570 int number, rollback_i, size;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700571 int align, queued, sndbuf;
Lars Ellenberg0f0601f2010-08-11 23:40:24 +0200572 int i = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700573
574 if (unlikely(cancel))
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +0100575 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700576
Lars Ellenbergaf85e8e2010-10-07 16:07:55 +0200577 if (mdev->rs_total == 0) {
578 /* empty resync? */
579 drbd_resync_finished(mdev);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +0100580 return 0;
Lars Ellenbergaf85e8e2010-10-07 16:07:55 +0200581 }
582
Philipp Reisnerb411b362009-09-25 16:07:19 -0700583 if (!get_ldev(mdev)) {
584 /* Since we only need to access mdev->rsync a
585 get_ldev_if_state(mdev,D_FAILED) would be sufficient, but
586 to continue resync with a broken disk makes no sense at
587 all */
588 dev_err(DEV, "Disk broke down during resync!\n");
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +0100589 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700590 }
591
Philipp Reisner0cfdd242011-05-25 11:14:35 +0200592 max_bio_size = queue_max_hw_sectors(mdev->rq_queue) << 9;
Lars Ellenberge65f4402010-11-05 10:04:07 +0100593 number = drbd_rs_number_requests(mdev);
594 if (number == 0)
Lars Ellenberg0f0601f2010-08-11 23:40:24 +0200595 goto requeue;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700596
Philipp Reisnerb411b362009-09-25 16:07:19 -0700597 for (i = 0; i < number; i++) {
598 /* Stop generating RS requests, when half of the send buffer is filled */
Philipp Reisnere42325a2011-01-19 13:55:45 +0100599 mutex_lock(&mdev->tconn->data.mutex);
600 if (mdev->tconn->data.socket) {
601 queued = mdev->tconn->data.socket->sk->sk_wmem_queued;
602 sndbuf = mdev->tconn->data.socket->sk->sk_sndbuf;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700603 } else {
604 queued = 1;
605 sndbuf = 0;
606 }
Philipp Reisnere42325a2011-01-19 13:55:45 +0100607 mutex_unlock(&mdev->tconn->data.mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700608 if (queued > sndbuf / 2)
609 goto requeue;
610
611next_sector:
612 size = BM_BLOCK_SIZE;
613 bit = drbd_bm_find_next(mdev, mdev->bm_resync_fo);
614
Lars Ellenberg4b0715f2010-12-14 15:13:04 +0100615 if (bit == DRBD_END_OF_BITMAP) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700616 mdev->bm_resync_fo = drbd_bm_bits(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700617 put_ldev(mdev);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +0100618 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700619 }
620
621 sector = BM_BIT_TO_SECT(bit);
622
Philipp Reisnere3555d82010-11-07 15:56:29 +0100623 if (drbd_rs_should_slow_down(mdev, sector) ||
624 drbd_try_rs_begin_io(mdev, sector)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700625 mdev->bm_resync_fo = bit;
626 goto requeue;
627 }
628 mdev->bm_resync_fo = bit + 1;
629
630 if (unlikely(drbd_bm_test_bit(mdev, bit) == 0)) {
631 drbd_rs_complete_io(mdev, sector);
632 goto next_sector;
633 }
634
Lars Ellenberg1816a2b2010-11-11 15:19:07 +0100635#if DRBD_MAX_BIO_SIZE > BM_BLOCK_SIZE
Philipp Reisnerb411b362009-09-25 16:07:19 -0700636 /* try to find some adjacent bits.
637 * we stop if we have already the maximum req size.
638 *
639 * Additionally always align bigger requests, in order to
640 * be prepared for all stripe sizes of software RAIDs.
Philipp Reisnerb411b362009-09-25 16:07:19 -0700641 */
642 align = 1;
Philipp Reisnerd2074502010-07-22 15:27:27 +0200643 rollback_i = i;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700644 for (;;) {
Lars Ellenberg1816a2b2010-11-11 15:19:07 +0100645 if (size + BM_BLOCK_SIZE > max_bio_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700646 break;
647
648 /* Be always aligned */
649 if (sector & ((1<<(align+3))-1))
650 break;
651
652 /* do not cross extent boundaries */
653 if (((bit+1) & BM_BLOCKS_PER_BM_EXT_MASK) == 0)
654 break;
655 /* now, is it actually dirty, after all?
656 * caution, drbd_bm_test_bit is tri-state for some
657 * obscure reason; ( b == 0 ) would get the out-of-band
658 * only accidentally right because of the "oddly sized"
659 * adjustment below */
660 if (drbd_bm_test_bit(mdev, bit+1) != 1)
661 break;
662 bit++;
663 size += BM_BLOCK_SIZE;
664 if ((BM_BLOCK_SIZE << align) <= size)
665 align++;
666 i++;
667 }
668 /* if we merged some,
669 * reset the offset to start the next drbd_bm_find_next from */
670 if (size > BM_BLOCK_SIZE)
671 mdev->bm_resync_fo = bit + 1;
672#endif
673
674 /* adjust very last sectors, in case we are oddly sized */
675 if (sector + (size>>9) > capacity)
676 size = (capacity-sector)<<9;
Lars Ellenbergf3990022011-03-23 14:31:09 +0100677 if (mdev->tconn->agreed_pro_version >= 89 && mdev->tconn->csums_tfm) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700678 switch (read_for_csum(mdev, sector, size)) {
Lars Ellenberg80a40e42010-08-11 23:28:00 +0200679 case -EIO: /* Disk failure */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700680 put_ldev(mdev);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +0100681 return -EIO;
Lars Ellenberg80a40e42010-08-11 23:28:00 +0200682 case -EAGAIN: /* allocation failed, or ldev busy */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700683 drbd_rs_complete_io(mdev, sector);
684 mdev->bm_resync_fo = BM_SECT_TO_BIT(sector);
Philipp Reisnerd2074502010-07-22 15:27:27 +0200685 i = rollback_i;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700686 goto requeue;
Lars Ellenberg80a40e42010-08-11 23:28:00 +0200687 case 0:
688 /* everything ok */
689 break;
690 default:
691 BUG();
Philipp Reisnerb411b362009-09-25 16:07:19 -0700692 }
693 } else {
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +0100694 int err;
695
Philipp Reisnerb411b362009-09-25 16:07:19 -0700696 inc_rs_pending(mdev);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +0100697 err = drbd_send_drequest(mdev, P_RS_DATA_REQUEST,
698 sector, size, ID_SYNCER);
699 if (err) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700700 dev_err(DEV, "drbd_send_drequest() failed, aborting...\n");
701 dec_rs_pending(mdev);
702 put_ldev(mdev);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +0100703 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700704 }
705 }
706 }
707
708 if (mdev->bm_resync_fo >= drbd_bm_bits(mdev)) {
709 /* last syncer _request_ was sent,
710 * but the P_RS_DATA_REPLY not yet received. sync will end (and
711 * next sync group will resume), as soon as we receive the last
712 * resync data block, and the last bit is cleared.
713 * until then resync "work" is "inactive" ...
714 */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700715 put_ldev(mdev);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +0100716 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700717 }
718
719 requeue:
Philipp Reisner778f2712010-07-06 11:14:00 +0200720 mdev->rs_in_flight += (i << (BM_BLOCK_SHIFT - 9));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700721 mod_timer(&mdev->resync_timer, jiffies + SLEEP_TIME);
722 put_ldev(mdev);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +0100723 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700724}
725
Philipp Reisner00d56942011-02-09 18:09:48 +0100726static int w_make_ov_request(struct drbd_work *w, int cancel)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700727{
Philipp Reisner00d56942011-02-09 18:09:48 +0100728 struct drbd_conf *mdev = w->mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700729 int number, i, size;
730 sector_t sector;
731 const sector_t capacity = drbd_get_capacity(mdev->this_bdev);
Lars Ellenberg58ffa582012-07-26 14:09:49 +0200732 bool stop_sector_reached = false;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700733
734 if (unlikely(cancel))
735 return 1;
736
Lars Ellenberg2649f082010-11-05 10:05:47 +0100737 number = drbd_rs_number_requests(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700738
739 sector = mdev->ov_position;
740 for (i = 0; i < number; i++) {
Lars Ellenberg58ffa582012-07-26 14:09:49 +0200741 if (sector >= capacity)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700742 return 1;
Lars Ellenberg58ffa582012-07-26 14:09:49 +0200743
744 /* We check for "finished" only in the reply path:
745 * w_e_end_ov_reply().
746 * We need to send at least one request out. */
747 stop_sector_reached = i > 0
748 && verify_can_do_stop_sector(mdev)
749 && sector >= mdev->ov_stop_sector;
750 if (stop_sector_reached)
751 break;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700752
753 size = BM_BLOCK_SIZE;
754
Philipp Reisnere3555d82010-11-07 15:56:29 +0100755 if (drbd_rs_should_slow_down(mdev, sector) ||
756 drbd_try_rs_begin_io(mdev, sector)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700757 mdev->ov_position = sector;
758 goto requeue;
759 }
760
761 if (sector + (size>>9) > capacity)
762 size = (capacity-sector)<<9;
763
764 inc_rs_pending(mdev);
Andreas Gruenbacher5b9f4992011-03-16 01:31:39 +0100765 if (drbd_send_ov_request(mdev, sector, size)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700766 dec_rs_pending(mdev);
767 return 0;
768 }
769 sector += BM_SECT_PER_BIT;
770 }
771 mdev->ov_position = sector;
772
773 requeue:
Lars Ellenberg2649f082010-11-05 10:05:47 +0100774 mdev->rs_in_flight += (i << (BM_BLOCK_SHIFT - 9));
Lars Ellenberg58ffa582012-07-26 14:09:49 +0200775 if (i == 0 || !stop_sector_reached)
776 mod_timer(&mdev->resync_timer, jiffies + SLEEP_TIME);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700777 return 1;
778}
779
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +0100780int w_ov_finished(struct drbd_work *w, int cancel)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700781{
Philipp Reisner00d56942011-02-09 18:09:48 +0100782 struct drbd_conf *mdev = w->mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700783 kfree(w);
Andreas Gruenbacher8f7bed72010-12-19 23:53:14 +0100784 ov_out_of_sync_print(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700785 drbd_resync_finished(mdev);
786
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +0100787 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700788}
789
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +0100790static int w_resync_finished(struct drbd_work *w, int cancel)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700791{
Philipp Reisner00d56942011-02-09 18:09:48 +0100792 struct drbd_conf *mdev = w->mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700793 kfree(w);
794
795 drbd_resync_finished(mdev);
796
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +0100797 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700798}
799
Lars Ellenbergaf85e8e2010-10-07 16:07:55 +0200800static void ping_peer(struct drbd_conf *mdev)
801{
Philipp Reisner2a67d8b2011-02-09 14:10:32 +0100802 struct drbd_tconn *tconn = mdev->tconn;
803
804 clear_bit(GOT_PING_ACK, &tconn->flags);
805 request_ping(tconn);
806 wait_event(tconn->ping_wait,
807 test_bit(GOT_PING_ACK, &tconn->flags) || mdev->state.conn < C_CONNECTED);
Lars Ellenbergaf85e8e2010-10-07 16:07:55 +0200808}
809
Philipp Reisnerb411b362009-09-25 16:07:19 -0700810int drbd_resync_finished(struct drbd_conf *mdev)
811{
812 unsigned long db, dt, dbdt;
813 unsigned long n_oos;
814 union drbd_state os, ns;
815 struct drbd_work *w;
816 char *khelper_cmd = NULL;
Lars Ellenberg26525612010-11-05 09:56:33 +0100817 int verify_done = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700818
819 /* Remove all elements from the resync LRU. Since future actions
820 * might set bits in the (main) bitmap, then the entries in the
821 * resync LRU would be wrong. */
822 if (drbd_rs_del_all(mdev)) {
823 /* In case this is not possible now, most probably because
824 * there are P_RS_DATA_REPLY Packets lingering on the worker's
825 * queue (or even the read operations for those packets
826 * is not finished by now). Retry in 100ms. */
827
Philipp Reisner20ee6392011-01-18 15:28:59 +0100828 schedule_timeout_interruptible(HZ / 10);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700829 w = kmalloc(sizeof(struct drbd_work), GFP_ATOMIC);
830 if (w) {
831 w->cb = w_resync_finished;
Philipp Reisner9b743da2011-07-15 18:15:45 +0200832 w->mdev = mdev;
Lars Ellenbergd5b27b02011-11-14 15:42:37 +0100833 drbd_queue_work(&mdev->tconn->sender_work, w);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700834 return 1;
835 }
836 dev_err(DEV, "Warn failed to drbd_rs_del_all() and to kmalloc(w).\n");
837 }
838
839 dt = (jiffies - mdev->rs_start - mdev->rs_paused) / HZ;
840 if (dt <= 0)
841 dt = 1;
Lars Ellenberg58ffa582012-07-26 14:09:49 +0200842
Philipp Reisnerb411b362009-09-25 16:07:19 -0700843 db = mdev->rs_total;
Lars Ellenberg58ffa582012-07-26 14:09:49 +0200844 /* adjust for verify start and stop sectors, respective reached position */
845 if (mdev->state.conn == C_VERIFY_S || mdev->state.conn == C_VERIFY_T)
846 db -= mdev->ov_left;
847
Philipp Reisnerb411b362009-09-25 16:07:19 -0700848 dbdt = Bit2KB(db/dt);
849 mdev->rs_paused /= HZ;
850
851 if (!get_ldev(mdev))
852 goto out;
853
Lars Ellenbergaf85e8e2010-10-07 16:07:55 +0200854 ping_peer(mdev);
855
Philipp Reisner87eeee42011-01-19 14:16:30 +0100856 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisner78bae592011-03-28 15:40:12 +0200857 os = drbd_read_state(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700858
Lars Ellenberg26525612010-11-05 09:56:33 +0100859 verify_done = (os.conn == C_VERIFY_S || os.conn == C_VERIFY_T);
860
Philipp Reisnerb411b362009-09-25 16:07:19 -0700861 /* This protects us against multiple calls (that can happen in the presence
862 of application IO), and against connectivity loss just before we arrive here. */
863 if (os.conn <= C_CONNECTED)
864 goto out_unlock;
865
866 ns = os;
867 ns.conn = C_CONNECTED;
868
869 dev_info(DEV, "%s done (total %lu sec; paused %lu sec; %lu K/sec)\n",
Lars Ellenberg58ffa582012-07-26 14:09:49 +0200870 verify_done ? "Online verify" : "Resync",
Philipp Reisnerb411b362009-09-25 16:07:19 -0700871 dt + mdev->rs_paused, mdev->rs_paused, dbdt);
872
873 n_oos = drbd_bm_total_weight(mdev);
874
875 if (os.conn == C_VERIFY_S || os.conn == C_VERIFY_T) {
876 if (n_oos) {
877 dev_alert(DEV, "Online verify found %lu %dk block out of sync!\n",
878 n_oos, Bit2KB(1));
879 khelper_cmd = "out-of-sync";
880 }
881 } else {
882 D_ASSERT((n_oos - mdev->rs_failed) == 0);
883
884 if (os.conn == C_SYNC_TARGET || os.conn == C_PAUSED_SYNC_T)
885 khelper_cmd = "after-resync-target";
886
Lars Ellenbergf3990022011-03-23 14:31:09 +0100887 if (mdev->tconn->csums_tfm && mdev->rs_total) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700888 const unsigned long s = mdev->rs_same_csum;
889 const unsigned long t = mdev->rs_total;
890 const int ratio =
891 (t == 0) ? 0 :
892 (t < 100000) ? ((s*100)/t) : (s/(t/100));
Bart Van Assche24c48302011-05-21 18:32:29 +0200893 dev_info(DEV, "%u %% had equal checksums, eliminated: %luK; "
Philipp Reisnerb411b362009-09-25 16:07:19 -0700894 "transferred %luK total %luK\n",
895 ratio,
896 Bit2KB(mdev->rs_same_csum),
897 Bit2KB(mdev->rs_total - mdev->rs_same_csum),
898 Bit2KB(mdev->rs_total));
899 }
900 }
901
902 if (mdev->rs_failed) {
903 dev_info(DEV, " %lu failed blocks\n", mdev->rs_failed);
904
905 if (os.conn == C_SYNC_TARGET || os.conn == C_PAUSED_SYNC_T) {
906 ns.disk = D_INCONSISTENT;
907 ns.pdsk = D_UP_TO_DATE;
908 } else {
909 ns.disk = D_UP_TO_DATE;
910 ns.pdsk = D_INCONSISTENT;
911 }
912 } else {
913 ns.disk = D_UP_TO_DATE;
914 ns.pdsk = D_UP_TO_DATE;
915
916 if (os.conn == C_SYNC_TARGET || os.conn == C_PAUSED_SYNC_T) {
917 if (mdev->p_uuid) {
918 int i;
919 for (i = UI_BITMAP ; i <= UI_HISTORY_END ; i++)
920 _drbd_uuid_set(mdev, i, mdev->p_uuid[i]);
921 drbd_uuid_set(mdev, UI_BITMAP, mdev->ldev->md.uuid[UI_CURRENT]);
922 _drbd_uuid_set(mdev, UI_CURRENT, mdev->p_uuid[UI_CURRENT]);
923 } else {
924 dev_err(DEV, "mdev->p_uuid is NULL! BUG\n");
925 }
926 }
927
Lars Ellenberg62b0da32011-01-20 13:25:21 +0100928 if (!(os.conn == C_VERIFY_S || os.conn == C_VERIFY_T)) {
929 /* for verify runs, we don't update uuids here,
930 * so there would be nothing to report. */
931 drbd_uuid_set_bm(mdev, 0UL);
932 drbd_print_uuids(mdev, "updated UUIDs");
933 if (mdev->p_uuid) {
934 /* Now the two UUID sets are equal, update what we
935 * know of the peer. */
936 int i;
937 for (i = UI_CURRENT ; i <= UI_HISTORY_END ; i++)
938 mdev->p_uuid[i] = mdev->ldev->md.uuid[i];
939 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700940 }
941 }
942
943 _drbd_set_state(mdev, ns, CS_VERBOSE, NULL);
944out_unlock:
Philipp Reisner87eeee42011-01-19 14:16:30 +0100945 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700946 put_ldev(mdev);
947out:
948 mdev->rs_total = 0;
949 mdev->rs_failed = 0;
950 mdev->rs_paused = 0;
Lars Ellenberg58ffa582012-07-26 14:09:49 +0200951
952 /* reset start sector, if we reached end of device */
953 if (verify_done && mdev->ov_left == 0)
Lars Ellenberg26525612010-11-05 09:56:33 +0100954 mdev->ov_start_sector = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700955
Lars Ellenberg13d42682010-10-13 17:37:54 +0200956 drbd_md_sync(mdev);
957
Philipp Reisnerb411b362009-09-25 16:07:19 -0700958 if (khelper_cmd)
959 drbd_khelper(mdev, khelper_cmd);
960
961 return 1;
962}
963
964/* helper */
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100965static void move_to_net_ee_or_free(struct drbd_conf *mdev, struct drbd_peer_request *peer_req)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700966{
Andreas Gruenbacher045417f2011-04-07 21:34:24 +0200967 if (drbd_peer_req_has_active_page(peer_req)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700968 /* This might happen if sendpage() has not finished */
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100969 int i = (peer_req->i.size + PAGE_SIZE -1) >> PAGE_SHIFT;
Lars Ellenberg435f0742010-09-06 12:30:25 +0200970 atomic_add(i, &mdev->pp_in_use_by_net);
971 atomic_sub(i, &mdev->pp_in_use);
Philipp Reisner87eeee42011-01-19 14:16:30 +0100972 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100973 list_add_tail(&peer_req->w.list, &mdev->net_ee);
Philipp Reisner87eeee42011-01-19 14:16:30 +0100974 spin_unlock_irq(&mdev->tconn->req_lock);
Lars Ellenberg435f0742010-09-06 12:30:25 +0200975 wake_up(&drbd_pp_wait);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700976 } else
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +0200977 drbd_free_peer_req(mdev, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700978}
979
980/**
981 * w_e_end_data_req() - Worker callback, to send a P_DATA_REPLY packet in response to a P_DATA_REQUEST
982 * @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_data_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
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100998 if (likely((peer_req->flags & EE_WAS_ERROR) == 0)) {
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +0100999 err = drbd_send_block(mdev, P_DATA_REPLY, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001000 } else {
1001 if (__ratelimit(&drbd_ratelimit_state))
1002 dev_err(DEV, "Sending NegDReply. sector=%llus.\n",
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001003 (unsigned long long)peer_req->i.sector);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001004
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001005 err = drbd_send_ack(mdev, P_NEG_DREPLY, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001006 }
1007
1008 dec_unacked(mdev);
1009
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001010 move_to_net_ee_or_free(mdev, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001011
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001012 if (unlikely(err))
Philipp Reisnerb411b362009-09-25 16:07:19 -07001013 dev_err(DEV, "drbd_send_block() failed\n");
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001014 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001015}
1016
1017/**
Andreas Gruenbachera209b4a2011-08-17 12:43:25 +02001018 * 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 -07001019 * @mdev: DRBD device.
1020 * @w: work object.
1021 * @cancel: The connection will be closed anyways
1022 */
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001023int w_e_end_rsdata_req(struct drbd_work *w, int cancel)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001024{
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001025 struct drbd_peer_request *peer_req = container_of(w, struct drbd_peer_request, w);
Philipp Reisner00d56942011-02-09 18:09:48 +01001026 struct drbd_conf *mdev = w->mdev;
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001027 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001028
1029 if (unlikely(cancel)) {
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +02001030 drbd_free_peer_req(mdev, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001031 dec_unacked(mdev);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001032 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001033 }
1034
1035 if (get_ldev_if_state(mdev, D_FAILED)) {
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001036 drbd_rs_complete_io(mdev, peer_req->i.sector);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001037 put_ldev(mdev);
1038 }
1039
Philipp Reisnerd612d302010-12-27 10:53:28 +01001040 if (mdev->state.conn == C_AHEAD) {
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001041 err = drbd_send_ack(mdev, P_RS_CANCEL, peer_req);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001042 } else if (likely((peer_req->flags & EE_WAS_ERROR) == 0)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001043 if (likely(mdev->state.pdsk >= D_INCONSISTENT)) {
1044 inc_rs_pending(mdev);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001045 err = drbd_send_block(mdev, P_RS_DATA_REPLY, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001046 } else {
1047 if (__ratelimit(&drbd_ratelimit_state))
1048 dev_err(DEV, "Not sending RSDataReply, "
1049 "partner DISKLESS!\n");
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001050 err = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001051 }
1052 } else {
1053 if (__ratelimit(&drbd_ratelimit_state))
1054 dev_err(DEV, "Sending NegRSDReply. sector %llus.\n",
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001055 (unsigned long long)peer_req->i.sector);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001056
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001057 err = drbd_send_ack(mdev, P_NEG_RS_DREPLY, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001058
1059 /* update resync data with failure */
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001060 drbd_rs_failed_io(mdev, peer_req->i.sector, peer_req->i.size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001061 }
1062
1063 dec_unacked(mdev);
1064
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001065 move_to_net_ee_or_free(mdev, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001066
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001067 if (unlikely(err))
Philipp Reisnerb411b362009-09-25 16:07:19 -07001068 dev_err(DEV, "drbd_send_block() failed\n");
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001069 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001070}
1071
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001072int w_e_end_csum_rs_req(struct drbd_work *w, int cancel)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001073{
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001074 struct drbd_peer_request *peer_req = container_of(w, struct drbd_peer_request, w);
Philipp Reisner00d56942011-02-09 18:09:48 +01001075 struct drbd_conf *mdev = w->mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001076 struct digest_info *di;
1077 int digest_size;
1078 void *digest = NULL;
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001079 int err, eq = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001080
1081 if (unlikely(cancel)) {
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +02001082 drbd_free_peer_req(mdev, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001083 dec_unacked(mdev);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001084 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001085 }
1086
Lars Ellenberg1d53f092010-09-05 01:13:24 +02001087 if (get_ldev(mdev)) {
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001088 drbd_rs_complete_io(mdev, peer_req->i.sector);
Lars Ellenberg1d53f092010-09-05 01:13:24 +02001089 put_ldev(mdev);
1090 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001091
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001092 di = peer_req->digest;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001093
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001094 if (likely((peer_req->flags & EE_WAS_ERROR) == 0)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001095 /* quick hack to try to avoid a race against reconfiguration.
1096 * a real fix would be much more involved,
1097 * introducing more locking mechanisms */
Lars Ellenbergf3990022011-03-23 14:31:09 +01001098 if (mdev->tconn->csums_tfm) {
1099 digest_size = crypto_hash_digestsize(mdev->tconn->csums_tfm);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001100 D_ASSERT(digest_size == di->digest_size);
1101 digest = kmalloc(digest_size, GFP_NOIO);
1102 }
1103 if (digest) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01001104 drbd_csum_ee(mdev, mdev->tconn->csums_tfm, peer_req, digest);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001105 eq = !memcmp(digest, di->digest, digest_size);
1106 kfree(digest);
1107 }
1108
1109 if (eq) {
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001110 drbd_set_in_sync(mdev, peer_req->i.sector, peer_req->i.size);
Lars Ellenberg676396d2010-03-03 02:08:22 +01001111 /* rs_same_csums unit is BM_BLOCK_SIZE */
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001112 mdev->rs_same_csum += peer_req->i.size >> BM_BLOCK_SHIFT;
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001113 err = drbd_send_ack(mdev, P_RS_IS_IN_SYNC, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001114 } else {
1115 inc_rs_pending(mdev);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001116 peer_req->block_id = ID_SYNCER; /* By setting block_id, digest pointer becomes invalid! */
1117 peer_req->flags &= ~EE_HAS_DIGEST; /* This peer request no longer has a digest pointer */
Philipp Reisner204bba92010-08-23 16:17:13 +02001118 kfree(di);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001119 err = drbd_send_block(mdev, P_RS_DATA_REPLY, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001120 }
1121 } else {
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001122 err = drbd_send_ack(mdev, P_NEG_RS_DREPLY, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001123 if (__ratelimit(&drbd_ratelimit_state))
1124 dev_err(DEV, "Sending NegDReply. I guess it gets messy.\n");
1125 }
1126
1127 dec_unacked(mdev);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001128 move_to_net_ee_or_free(mdev, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001129
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001130 if (unlikely(err))
Philipp Reisnerb411b362009-09-25 16:07:19 -07001131 dev_err(DEV, "drbd_send_block/ack() failed\n");
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001132 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001133}
1134
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001135int w_e_end_ov_req(struct drbd_work *w, int cancel)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001136{
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001137 struct drbd_peer_request *peer_req = container_of(w, struct drbd_peer_request, w);
Philipp Reisner00d56942011-02-09 18:09:48 +01001138 struct drbd_conf *mdev = w->mdev;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001139 sector_t sector = peer_req->i.sector;
1140 unsigned int size = peer_req->i.size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001141 int digest_size;
1142 void *digest;
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001143 int err = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001144
1145 if (unlikely(cancel))
1146 goto out;
1147
Lars Ellenbergf3990022011-03-23 14:31:09 +01001148 digest_size = crypto_hash_digestsize(mdev->tconn->verify_tfm);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001149 digest = kmalloc(digest_size, GFP_NOIO);
Philipp Reisner8f214202011-03-01 15:52:35 +01001150 if (!digest) {
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001151 err = 1; /* terminate the connection in case the allocation failed */
Philipp Reisner8f214202011-03-01 15:52:35 +01001152 goto out;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001153 }
1154
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001155 if (likely(!(peer_req->flags & EE_WAS_ERROR)))
Lars Ellenbergf3990022011-03-23 14:31:09 +01001156 drbd_csum_ee(mdev, mdev->tconn->verify_tfm, peer_req, digest);
Philipp Reisner8f214202011-03-01 15:52:35 +01001157 else
1158 memset(digest, 0, digest_size);
1159
Lars Ellenberg53ea4332011-03-08 17:11:40 +01001160 /* Free e and pages before send.
1161 * In case we block on congestion, we could otherwise run into
1162 * some distributed deadlock, if the other side blocks on
1163 * congestion as well, because our receiver blocks in
Andreas Gruenbacherc37c8ec2011-04-07 21:02:09 +02001164 * drbd_alloc_pages due to pp_in_use > max_buffers. */
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +02001165 drbd_free_peer_req(mdev, peer_req);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001166 peer_req = NULL;
Philipp Reisner8f214202011-03-01 15:52:35 +01001167 inc_rs_pending(mdev);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001168 err = drbd_send_drequest_csum(mdev, sector, size, digest, digest_size, P_OV_REPLY);
1169 if (err)
Philipp Reisner8f214202011-03-01 15:52:35 +01001170 dec_rs_pending(mdev);
1171 kfree(digest);
1172
Philipp Reisnerb411b362009-09-25 16:07:19 -07001173out:
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001174 if (peer_req)
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +02001175 drbd_free_peer_req(mdev, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001176 dec_unacked(mdev);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001177 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001178}
1179
Andreas Gruenbacher8f7bed72010-12-19 23:53:14 +01001180void drbd_ov_out_of_sync_found(struct drbd_conf *mdev, sector_t sector, int size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001181{
1182 if (mdev->ov_last_oos_start + mdev->ov_last_oos_size == sector) {
1183 mdev->ov_last_oos_size += size>>9;
1184 } else {
1185 mdev->ov_last_oos_start = sector;
1186 mdev->ov_last_oos_size = size>>9;
1187 }
1188 drbd_set_out_of_sync(mdev, sector, size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001189}
1190
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001191int w_e_end_ov_reply(struct drbd_work *w, int cancel)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001192{
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001193 struct drbd_peer_request *peer_req = container_of(w, struct drbd_peer_request, w);
Philipp Reisner00d56942011-02-09 18:09:48 +01001194 struct drbd_conf *mdev = w->mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001195 struct digest_info *di;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001196 void *digest;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001197 sector_t sector = peer_req->i.sector;
1198 unsigned int size = peer_req->i.size;
Lars Ellenberg53ea4332011-03-08 17:11:40 +01001199 int digest_size;
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001200 int err, eq = 0;
Lars Ellenberg58ffa582012-07-26 14:09:49 +02001201 bool stop_sector_reached = false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001202
1203 if (unlikely(cancel)) {
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +02001204 drbd_free_peer_req(mdev, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001205 dec_unacked(mdev);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001206 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001207 }
1208
1209 /* after "cancel", because after drbd_disconnect/drbd_rs_cancel_all
1210 * the resync lru has been cleaned up already */
Lars Ellenberg1d53f092010-09-05 01:13:24 +02001211 if (get_ldev(mdev)) {
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001212 drbd_rs_complete_io(mdev, peer_req->i.sector);
Lars Ellenberg1d53f092010-09-05 01:13:24 +02001213 put_ldev(mdev);
1214 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001215
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001216 di = peer_req->digest;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001217
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001218 if (likely((peer_req->flags & EE_WAS_ERROR) == 0)) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01001219 digest_size = crypto_hash_digestsize(mdev->tconn->verify_tfm);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001220 digest = kmalloc(digest_size, GFP_NOIO);
1221 if (digest) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01001222 drbd_csum_ee(mdev, mdev->tconn->verify_tfm, peer_req, digest);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001223
1224 D_ASSERT(digest_size == di->digest_size);
1225 eq = !memcmp(digest, di->digest, digest_size);
1226 kfree(digest);
1227 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001228 }
1229
Lars Ellenberg9676c762011-02-22 14:02:31 +01001230 /* Free peer_req and pages before send.
1231 * In case we block on congestion, we could otherwise run into
1232 * some distributed deadlock, if the other side blocks on
1233 * congestion as well, because our receiver blocks in
Andreas Gruenbacherc37c8ec2011-04-07 21:02:09 +02001234 * drbd_alloc_pages due to pp_in_use > max_buffers. */
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +02001235 drbd_free_peer_req(mdev, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001236 if (!eq)
Andreas Gruenbacher8f7bed72010-12-19 23:53:14 +01001237 drbd_ov_out_of_sync_found(mdev, sector, size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001238 else
Andreas Gruenbacher8f7bed72010-12-19 23:53:14 +01001239 ov_out_of_sync_print(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001240
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001241 err = drbd_send_ack_ex(mdev, P_OV_RESULT, sector, size,
Andreas Gruenbacherfa79abd2011-03-16 01:31:39 +01001242 eq ? ID_IN_SYNC : ID_OUT_OF_SYNC);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001243
Lars Ellenberg53ea4332011-03-08 17:11:40 +01001244 dec_unacked(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001245
Lars Ellenbergea5442a2010-11-05 09:48:01 +01001246 --mdev->ov_left;
1247
1248 /* let's advance progress step marks only for every other megabyte */
1249 if ((mdev->ov_left & 0x200) == 0x200)
1250 drbd_advance_rs_marks(mdev, mdev->ov_left);
1251
Lars Ellenberg58ffa582012-07-26 14:09:49 +02001252 stop_sector_reached = verify_can_do_stop_sector(mdev) &&
1253 (sector + (size>>9)) >= mdev->ov_stop_sector;
1254
1255 if (mdev->ov_left == 0 || stop_sector_reached) {
Andreas Gruenbacher8f7bed72010-12-19 23:53:14 +01001256 ov_out_of_sync_print(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001257 drbd_resync_finished(mdev);
1258 }
1259
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001260 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001261}
1262
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001263int w_prev_work_done(struct drbd_work *w, int cancel)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001264{
1265 struct drbd_wq_barrier *b = container_of(w, struct drbd_wq_barrier, w);
Philipp Reisner00d56942011-02-09 18:09:48 +01001266
Philipp Reisnerb411b362009-09-25 16:07:19 -07001267 complete(&b->done);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001268 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001269}
1270
Lars Ellenbergb6dd1a82011-11-28 15:04:49 +01001271/* FIXME
1272 * We need to track the number of pending barrier acks,
1273 * and to be able to wait for them.
1274 * See also comment in drbd_adm_attach before drbd_suspend_io.
1275 */
1276int drbd_send_barrier(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001277{
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02001278 struct p_barrier *p;
Lars Ellenbergb6dd1a82011-11-28 15:04:49 +01001279 struct drbd_socket *sock;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001280
Lars Ellenbergb6dd1a82011-11-28 15:04:49 +01001281 sock = &tconn->data;
1282 p = conn_prepare_command(tconn, sock);
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02001283 if (!p)
1284 return -EIO;
Lars Ellenbergb6dd1a82011-11-28 15:04:49 +01001285 p->barrier = tconn->send.current_epoch_nr;
1286 p->pad = 0;
1287 tconn->send.current_epoch_writes = 0;
1288
1289 return conn_send_command(tconn, sock, P_BARRIER, sizeof(*p), NULL, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001290}
1291
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001292int w_send_write_hint(struct drbd_work *w, int cancel)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001293{
Philipp Reisner00d56942011-02-09 18:09:48 +01001294 struct drbd_conf *mdev = w->mdev;
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02001295 struct drbd_socket *sock;
1296
Philipp Reisnerb411b362009-09-25 16:07:19 -07001297 if (cancel)
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001298 return 0;
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02001299 sock = &mdev->tconn->data;
1300 if (!drbd_prepare_command(mdev, sock))
1301 return -EIO;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001302 return drbd_send_command(mdev, sock, P_UNPLUG_REMOTE, 0, NULL, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001303}
1304
Lars Ellenberg4eb9b3c2012-08-20 11:05:23 +02001305static void re_init_if_first_write(struct drbd_tconn *tconn, unsigned int epoch)
1306{
1307 if (!tconn->send.seen_any_write_yet) {
1308 tconn->send.seen_any_write_yet = true;
1309 tconn->send.current_epoch_nr = epoch;
1310 tconn->send.current_epoch_writes = 0;
1311 }
1312}
1313
1314static void maybe_send_barrier(struct drbd_tconn *tconn, unsigned int epoch)
1315{
1316 /* re-init if first write on this connection */
1317 if (!tconn->send.seen_any_write_yet)
1318 return;
1319 if (tconn->send.current_epoch_nr != epoch) {
1320 if (tconn->send.current_epoch_writes)
1321 drbd_send_barrier(tconn);
1322 tconn->send.current_epoch_nr = epoch;
1323 }
1324}
1325
Andreas Gruenbacher8f7bed72010-12-19 23:53:14 +01001326int w_send_out_of_sync(struct drbd_work *w, int cancel)
Philipp Reisner73a01a12010-10-27 14:33:00 +02001327{
1328 struct drbd_request *req = container_of(w, struct drbd_request, w);
Philipp Reisner00d56942011-02-09 18:09:48 +01001329 struct drbd_conf *mdev = w->mdev;
Lars Ellenbergb6dd1a82011-11-28 15:04:49 +01001330 struct drbd_tconn *tconn = mdev->tconn;
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001331 int err;
Philipp Reisner73a01a12010-10-27 14:33:00 +02001332
1333 if (unlikely(cancel)) {
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01001334 req_mod(req, SEND_CANCELED);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001335 return 0;
Philipp Reisner73a01a12010-10-27 14:33:00 +02001336 }
1337
Lars Ellenbergb6dd1a82011-11-28 15:04:49 +01001338 /* this time, no tconn->send.current_epoch_writes++;
1339 * If it was sent, it was the closing barrier for the last
1340 * replicated epoch, before we went into AHEAD mode.
1341 * No more barriers will be sent, until we leave AHEAD mode again. */
Lars Ellenberg4eb9b3c2012-08-20 11:05:23 +02001342 maybe_send_barrier(tconn, req->epoch);
Lars Ellenbergb6dd1a82011-11-28 15:04:49 +01001343
Andreas Gruenbacher8f7bed72010-12-19 23:53:14 +01001344 err = drbd_send_out_of_sync(mdev, req);
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01001345 req_mod(req, OOS_HANDED_TO_NETWORK);
Philipp Reisner73a01a12010-10-27 14:33:00 +02001346
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001347 return err;
Philipp Reisner73a01a12010-10-27 14:33:00 +02001348}
1349
Philipp Reisnerb411b362009-09-25 16:07:19 -07001350/**
1351 * w_send_dblock() - Worker callback to send a P_DATA packet in order to mirror a write request
1352 * @mdev: DRBD device.
1353 * @w: work object.
1354 * @cancel: The connection will be closed anyways
1355 */
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001356int w_send_dblock(struct drbd_work *w, int cancel)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001357{
1358 struct drbd_request *req = container_of(w, struct drbd_request, w);
Philipp Reisner00d56942011-02-09 18:09:48 +01001359 struct drbd_conf *mdev = w->mdev;
Lars Ellenbergb6dd1a82011-11-28 15:04:49 +01001360 struct drbd_tconn *tconn = mdev->tconn;
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001361 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001362
1363 if (unlikely(cancel)) {
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01001364 req_mod(req, SEND_CANCELED);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001365 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001366 }
1367
Lars Ellenberg4eb9b3c2012-08-20 11:05:23 +02001368 re_init_if_first_write(tconn, req->epoch);
1369 maybe_send_barrier(tconn, req->epoch);
Lars Ellenbergb6dd1a82011-11-28 15:04:49 +01001370 tconn->send.current_epoch_writes++;
1371
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001372 err = drbd_send_dblock(mdev, req);
1373 req_mod(req, err ? SEND_FAILED : HANDED_OVER_TO_NETWORK);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001374
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001375 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001376}
1377
1378/**
1379 * w_send_read_req() - Worker callback to send a read request (P_DATA_REQUEST) packet
1380 * @mdev: DRBD device.
1381 * @w: work object.
1382 * @cancel: The connection will be closed anyways
1383 */
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001384int w_send_read_req(struct drbd_work *w, int cancel)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001385{
1386 struct drbd_request *req = container_of(w, struct drbd_request, w);
Philipp Reisner00d56942011-02-09 18:09:48 +01001387 struct drbd_conf *mdev = w->mdev;
Lars Ellenbergb6dd1a82011-11-28 15:04:49 +01001388 struct drbd_tconn *tconn = mdev->tconn;
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001389 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001390
1391 if (unlikely(cancel)) {
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01001392 req_mod(req, SEND_CANCELED);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001393 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001394 }
1395
Lars Ellenbergb6dd1a82011-11-28 15:04:49 +01001396 /* Even read requests may close a write epoch,
1397 * if there was any yet. */
Lars Ellenberg4eb9b3c2012-08-20 11:05:23 +02001398 maybe_send_barrier(tconn, req->epoch);
Lars Ellenbergb6dd1a82011-11-28 15:04:49 +01001399
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001400 err = drbd_send_drequest(mdev, P_DATA_REQUEST, req->i.sector, req->i.size,
Andreas Gruenbacher6c1005e2011-03-16 01:34:24 +01001401 (unsigned long)req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001402
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001403 req_mod(req, err ? SEND_FAILED : HANDED_OVER_TO_NETWORK);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001404
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001405 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001406}
1407
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001408int w_restart_disk_io(struct drbd_work *w, int cancel)
Philipp Reisner265be2d2010-05-31 10:14:17 +02001409{
1410 struct drbd_request *req = container_of(w, struct drbd_request, w);
Philipp Reisner00d56942011-02-09 18:09:48 +01001411 struct drbd_conf *mdev = w->mdev;
Philipp Reisner265be2d2010-05-31 10:14:17 +02001412
Philipp Reisner07782862010-08-31 12:00:50 +02001413 if (bio_data_dir(req->master_bio) == WRITE && req->rq_state & RQ_IN_ACT_LOG)
Lars Ellenberg181286a2011-03-31 15:18:56 +02001414 drbd_al_begin_io(mdev, &req->i);
Philipp Reisner265be2d2010-05-31 10:14:17 +02001415
1416 drbd_req_make_private_bio(req, req->master_bio);
1417 req->private_bio->bi_bdev = mdev->ldev->backing_bdev;
1418 generic_make_request(req->private_bio);
1419
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001420 return 0;
Philipp Reisner265be2d2010-05-31 10:14:17 +02001421}
1422
Philipp Reisnerb411b362009-09-25 16:07:19 -07001423static int _drbd_may_sync_now(struct drbd_conf *mdev)
1424{
1425 struct drbd_conf *odev = mdev;
Andreas Gruenbacher95f8efd2011-05-12 11:15:34 +02001426 int resync_after;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001427
1428 while (1) {
Philipp Reisner438c8372011-03-28 14:48:01 +02001429 if (!odev->ldev)
1430 return 1;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001431 rcu_read_lock();
Andreas Gruenbacher95f8efd2011-05-12 11:15:34 +02001432 resync_after = rcu_dereference(odev->ldev->disk_conf)->resync_after;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001433 rcu_read_unlock();
Andreas Gruenbacher95f8efd2011-05-12 11:15:34 +02001434 if (resync_after == -1)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001435 return 1;
Andreas Gruenbacher95f8efd2011-05-12 11:15:34 +02001436 odev = minor_to_mdev(resync_after);
Andreas Gruenbacher841ce242010-12-15 19:31:20 +01001437 if (!expect(odev))
1438 return 1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001439 if ((odev->state.conn >= C_SYNC_SOURCE &&
1440 odev->state.conn <= C_PAUSED_SYNC_T) ||
1441 odev->state.aftr_isp || odev->state.peer_isp ||
1442 odev->state.user_isp)
1443 return 0;
1444 }
1445}
1446
1447/**
1448 * _drbd_pause_after() - Pause resync on all devices that may not resync now
1449 * @mdev: DRBD device.
1450 *
1451 * Called from process context only (admin command and after_state_ch).
1452 */
1453static int _drbd_pause_after(struct drbd_conf *mdev)
1454{
1455 struct drbd_conf *odev;
1456 int i, rv = 0;
1457
Philipp Reisner695d08f2011-04-11 22:53:32 -07001458 rcu_read_lock();
Philipp Reisner81a5d602011-02-22 19:53:16 -05001459 idr_for_each_entry(&minors, odev, i) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001460 if (odev->state.conn == C_STANDALONE && odev->state.disk == D_DISKLESS)
1461 continue;
1462 if (!_drbd_may_sync_now(odev))
1463 rv |= (__drbd_set_state(_NS(odev, aftr_isp, 1), CS_HARD, NULL)
1464 != SS_NOTHING_TO_DO);
1465 }
Philipp Reisner695d08f2011-04-11 22:53:32 -07001466 rcu_read_unlock();
Philipp Reisnerb411b362009-09-25 16:07:19 -07001467
1468 return rv;
1469}
1470
1471/**
1472 * _drbd_resume_next() - Resume resync on all devices that may resync now
1473 * @mdev: DRBD device.
1474 *
1475 * Called from process context only (admin command and worker).
1476 */
1477static int _drbd_resume_next(struct drbd_conf *mdev)
1478{
1479 struct drbd_conf *odev;
1480 int i, rv = 0;
1481
Philipp Reisner695d08f2011-04-11 22:53:32 -07001482 rcu_read_lock();
Philipp Reisner81a5d602011-02-22 19:53:16 -05001483 idr_for_each_entry(&minors, odev, i) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001484 if (odev->state.conn == C_STANDALONE && odev->state.disk == D_DISKLESS)
1485 continue;
1486 if (odev->state.aftr_isp) {
1487 if (_drbd_may_sync_now(odev))
1488 rv |= (__drbd_set_state(_NS(odev, aftr_isp, 0),
1489 CS_HARD, NULL)
1490 != SS_NOTHING_TO_DO) ;
1491 }
1492 }
Philipp Reisner695d08f2011-04-11 22:53:32 -07001493 rcu_read_unlock();
Philipp Reisnerb411b362009-09-25 16:07:19 -07001494 return rv;
1495}
1496
1497void resume_next_sg(struct drbd_conf *mdev)
1498{
1499 write_lock_irq(&global_state_lock);
1500 _drbd_resume_next(mdev);
1501 write_unlock_irq(&global_state_lock);
1502}
1503
1504void suspend_other_sg(struct drbd_conf *mdev)
1505{
1506 write_lock_irq(&global_state_lock);
1507 _drbd_pause_after(mdev);
1508 write_unlock_irq(&global_state_lock);
1509}
1510
Philipp Reisnerdc97b702011-05-03 14:27:15 +02001511/* caller must hold global_state_lock */
Andreas Gruenbacher95f8efd2011-05-12 11:15:34 +02001512enum drbd_ret_code drbd_resync_after_valid(struct drbd_conf *mdev, int o_minor)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001513{
1514 struct drbd_conf *odev;
Andreas Gruenbacher95f8efd2011-05-12 11:15:34 +02001515 int resync_after;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001516
1517 if (o_minor == -1)
1518 return NO_ERROR;
1519 if (o_minor < -1 || minor_to_mdev(o_minor) == NULL)
Andreas Gruenbacher95f8efd2011-05-12 11:15:34 +02001520 return ERR_RESYNC_AFTER;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001521
1522 /* check for loops */
1523 odev = minor_to_mdev(o_minor);
1524 while (1) {
1525 if (odev == mdev)
Andreas Gruenbacher95f8efd2011-05-12 11:15:34 +02001526 return ERR_RESYNC_AFTER_CYCLE;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001527
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001528 rcu_read_lock();
Andreas Gruenbacher95f8efd2011-05-12 11:15:34 +02001529 resync_after = rcu_dereference(odev->ldev->disk_conf)->resync_after;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001530 rcu_read_unlock();
Philipp Reisnerb411b362009-09-25 16:07:19 -07001531 /* dependency chain ends here, no cycles. */
Andreas Gruenbacher95f8efd2011-05-12 11:15:34 +02001532 if (resync_after == -1)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001533 return NO_ERROR;
1534
1535 /* follow the dependency chain */
Andreas Gruenbacher95f8efd2011-05-12 11:15:34 +02001536 odev = minor_to_mdev(resync_after);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001537 }
1538}
1539
Philipp Reisnerdc97b702011-05-03 14:27:15 +02001540/* caller must hold global_state_lock */
Andreas Gruenbacher95f8efd2011-05-12 11:15:34 +02001541void drbd_resync_after_changed(struct drbd_conf *mdev)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001542{
1543 int changes;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001544
Philipp Reisnerdc97b702011-05-03 14:27:15 +02001545 do {
1546 changes = _drbd_pause_after(mdev);
1547 changes |= _drbd_resume_next(mdev);
1548 } while (changes);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001549}
1550
Lars Ellenberg9bd28d32010-11-05 09:55:18 +01001551void drbd_rs_controller_reset(struct drbd_conf *mdev)
1552{
Philipp Reisner813472c2011-05-03 16:47:02 +02001553 struct fifo_buffer *plan;
1554
Lars Ellenberg9bd28d32010-11-05 09:55:18 +01001555 atomic_set(&mdev->rs_sect_in, 0);
1556 atomic_set(&mdev->rs_sect_ev, 0);
1557 mdev->rs_in_flight = 0;
Philipp Reisner813472c2011-05-03 16:47:02 +02001558
1559 /* Updating the RCU protected object in place is necessary since
1560 this function gets called from atomic context.
1561 It is valid since all other updates also lead to an completely
1562 empty fifo */
1563 rcu_read_lock();
1564 plan = rcu_dereference(mdev->rs_plan_s);
1565 plan->total = 0;
1566 fifo_set(plan, 0);
1567 rcu_read_unlock();
Lars Ellenberg9bd28d32010-11-05 09:55:18 +01001568}
1569
Philipp Reisner1f04af32011-02-07 11:33:59 +01001570void start_resync_timer_fn(unsigned long data)
1571{
1572 struct drbd_conf *mdev = (struct drbd_conf *) data;
1573
Lars Ellenbergd5b27b02011-11-14 15:42:37 +01001574 drbd_queue_work(&mdev->tconn->sender_work, &mdev->start_resync_work);
Philipp Reisner1f04af32011-02-07 11:33:59 +01001575}
1576
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001577int w_start_resync(struct drbd_work *w, int cancel)
Philipp Reisner1f04af32011-02-07 11:33:59 +01001578{
Philipp Reisner00d56942011-02-09 18:09:48 +01001579 struct drbd_conf *mdev = w->mdev;
1580
Philipp Reisner1f04af32011-02-07 11:33:59 +01001581 if (atomic_read(&mdev->unacked_cnt) || atomic_read(&mdev->rs_pending_cnt)) {
1582 dev_warn(DEV, "w_start_resync later...\n");
1583 mdev->start_resync_timer.expires = jiffies + HZ/10;
1584 add_timer(&mdev->start_resync_timer);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001585 return 0;
Philipp Reisner1f04af32011-02-07 11:33:59 +01001586 }
1587
1588 drbd_start_resync(mdev, C_SYNC_SOURCE);
Philipp Reisner36baf612011-11-10 14:27:34 +01001589 clear_bit(AHEAD_TO_SYNC_SOURCE, &mdev->flags);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001590 return 0;
Philipp Reisner1f04af32011-02-07 11:33:59 +01001591}
1592
Philipp Reisnerb411b362009-09-25 16:07:19 -07001593/**
1594 * drbd_start_resync() - Start the resync process
1595 * @mdev: DRBD device.
1596 * @side: Either C_SYNC_SOURCE or C_SYNC_TARGET
1597 *
1598 * This function might bring you directly into one of the
1599 * C_PAUSED_SYNC_* states.
1600 */
1601void drbd_start_resync(struct drbd_conf *mdev, enum drbd_conns side)
1602{
1603 union drbd_state ns;
1604 int r;
1605
Philipp Reisnerc4752ef2010-10-27 17:32:36 +02001606 if (mdev->state.conn >= C_SYNC_SOURCE && mdev->state.conn < C_AHEAD) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001607 dev_err(DEV, "Resync already running!\n");
1608 return;
1609 }
1610
Philipp Reisnere64a3292011-02-05 17:34:11 +01001611 if (!test_bit(B_RS_H_DONE, &mdev->flags)) {
1612 if (side == C_SYNC_TARGET) {
1613 /* Since application IO was locked out during C_WF_BITMAP_T and
1614 C_WF_SYNC_UUID we are still unmodified. Before going to C_SYNC_TARGET
1615 we check that we might make the data inconsistent. */
1616 r = drbd_khelper(mdev, "before-resync-target");
1617 r = (r >> 8) & 0xff;
1618 if (r > 0) {
1619 dev_info(DEV, "before-resync-target handler returned %d, "
Philipp Reisner09b9e792010-12-03 16:04:24 +01001620 "dropping connection.\n", r);
Philipp Reisner38fa9982011-03-15 18:24:49 +01001621 conn_request_state(mdev->tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Philipp Reisner09b9e792010-12-03 16:04:24 +01001622 return;
1623 }
Philipp Reisnere64a3292011-02-05 17:34:11 +01001624 } else /* C_SYNC_SOURCE */ {
1625 r = drbd_khelper(mdev, "before-resync-source");
1626 r = (r >> 8) & 0xff;
1627 if (r > 0) {
1628 if (r == 3) {
1629 dev_info(DEV, "before-resync-source handler returned %d, "
1630 "ignoring. Old userland tools?", r);
1631 } else {
1632 dev_info(DEV, "before-resync-source handler returned %d, "
1633 "dropping connection.\n", r);
Philipp Reisner38fa9982011-03-15 18:24:49 +01001634 conn_request_state(mdev->tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Philipp Reisnere64a3292011-02-05 17:34:11 +01001635 return;
1636 }
1637 }
Philipp Reisner09b9e792010-12-03 16:04:24 +01001638 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001639 }
1640
Philipp Reisnere64a3292011-02-05 17:34:11 +01001641 if (current == mdev->tconn->worker.task) {
Philipp Reisnerdad20552011-02-11 19:43:55 +01001642 /* The worker should not sleep waiting for state_mutex,
Philipp Reisnere64a3292011-02-05 17:34:11 +01001643 that can take long */
Philipp Reisner8410da82011-02-11 20:11:10 +01001644 if (!mutex_trylock(mdev->state_mutex)) {
Philipp Reisnere64a3292011-02-05 17:34:11 +01001645 set_bit(B_RS_H_DONE, &mdev->flags);
1646 mdev->start_resync_timer.expires = jiffies + HZ/5;
1647 add_timer(&mdev->start_resync_timer);
1648 return;
1649 }
1650 } else {
Philipp Reisner8410da82011-02-11 20:11:10 +01001651 mutex_lock(mdev->state_mutex);
Philipp Reisnere64a3292011-02-05 17:34:11 +01001652 }
1653 clear_bit(B_RS_H_DONE, &mdev->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001654
Philipp Reisner0cfac5d2011-11-10 12:12:52 +01001655 write_lock_irq(&global_state_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001656 if (!get_ldev_if_state(mdev, D_NEGOTIATING)) {
Philipp Reisner0cfac5d2011-11-10 12:12:52 +01001657 write_unlock_irq(&global_state_lock);
Philipp Reisner8410da82011-02-11 20:11:10 +01001658 mutex_unlock(mdev->state_mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001659 return;
1660 }
1661
Philipp Reisner78bae592011-03-28 15:40:12 +02001662 ns = drbd_read_state(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001663
1664 ns.aftr_isp = !_drbd_may_sync_now(mdev);
1665
1666 ns.conn = side;
1667
1668 if (side == C_SYNC_TARGET)
1669 ns.disk = D_INCONSISTENT;
1670 else /* side == C_SYNC_SOURCE */
1671 ns.pdsk = D_INCONSISTENT;
1672
1673 r = __drbd_set_state(mdev, ns, CS_VERBOSE, NULL);
Philipp Reisner78bae592011-03-28 15:40:12 +02001674 ns = drbd_read_state(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001675
1676 if (ns.conn < C_CONNECTED)
1677 r = SS_UNKNOWN_ERROR;
1678
1679 if (r == SS_SUCCESS) {
Lars Ellenberg1d7734a2010-08-11 21:21:50 +02001680 unsigned long tw = drbd_bm_total_weight(mdev);
1681 unsigned long now = jiffies;
1682 int i;
1683
Philipp Reisnerb411b362009-09-25 16:07:19 -07001684 mdev->rs_failed = 0;
1685 mdev->rs_paused = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001686 mdev->rs_same_csum = 0;
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02001687 mdev->rs_last_events = 0;
1688 mdev->rs_last_sect_ev = 0;
Lars Ellenberg1d7734a2010-08-11 21:21:50 +02001689 mdev->rs_total = tw;
1690 mdev->rs_start = now;
1691 for (i = 0; i < DRBD_SYNC_MARKS; i++) {
1692 mdev->rs_mark_left[i] = tw;
1693 mdev->rs_mark_time[i] = now;
1694 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001695 _drbd_pause_after(mdev);
1696 }
1697 write_unlock_irq(&global_state_lock);
Lars Ellenberg5a22db82010-12-17 21:14:23 +01001698
Philipp Reisnerb411b362009-09-25 16:07:19 -07001699 if (r == SS_SUCCESS) {
1700 dev_info(DEV, "Began resync as %s (will sync %lu KB [%lu bits set]).\n",
1701 drbd_conn_str(ns.conn),
1702 (unsigned long) mdev->rs_total << (BM_BLOCK_SHIFT-10),
1703 (unsigned long) mdev->rs_total);
Lars Ellenberg6c922ed2011-01-12 11:51:13 +01001704 if (side == C_SYNC_TARGET)
1705 mdev->bm_resync_fo = 0;
1706
1707 /* Since protocol 96, we must serialize drbd_gen_and_send_sync_uuid
1708 * with w_send_oos, or the sync target will get confused as to
1709 * how much bits to resync. We cannot do that always, because for an
1710 * empty resync and protocol < 95, we need to do it here, as we call
1711 * drbd_resync_finished from here in that case.
1712 * We drbd_gen_and_send_sync_uuid here for protocol < 96,
1713 * and from after_state_ch otherwise. */
Philipp Reisner31890f42011-01-19 14:12:51 +01001714 if (side == C_SYNC_SOURCE && mdev->tconn->agreed_pro_version < 96)
Lars Ellenberg6c922ed2011-01-12 11:51:13 +01001715 drbd_gen_and_send_sync_uuid(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001716
Philipp Reisner31890f42011-01-19 14:12:51 +01001717 if (mdev->tconn->agreed_pro_version < 95 && mdev->rs_total == 0) {
Lars Ellenbergaf85e8e2010-10-07 16:07:55 +02001718 /* This still has a race (about when exactly the peers
1719 * detect connection loss) that can lead to a full sync
1720 * on next handshake. In 8.3.9 we fixed this with explicit
1721 * resync-finished notifications, but the fix
1722 * introduces a protocol change. Sleeping for some
1723 * time longer than the ping interval + timeout on the
1724 * SyncSource, to give the SyncTarget the chance to
1725 * detect connection loss, then waiting for a ping
1726 * response (implicit in drbd_resync_finished) reduces
1727 * the race considerably, but does not solve it. */
Philipp Reisner44ed1672011-04-19 17:10:19 +02001728 if (side == C_SYNC_SOURCE) {
1729 struct net_conf *nc;
1730 int timeo;
1731
1732 rcu_read_lock();
1733 nc = rcu_dereference(mdev->tconn->net_conf);
1734 timeo = nc->ping_int * HZ + nc->ping_timeo * HZ / 9;
1735 rcu_read_unlock();
1736 schedule_timeout_interruptible(timeo);
1737 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001738 drbd_resync_finished(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001739 }
1740
Lars Ellenberg9bd28d32010-11-05 09:55:18 +01001741 drbd_rs_controller_reset(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001742 /* ns.conn may already be != mdev->state.conn,
1743 * we may have been paused in between, or become paused until
1744 * the timer triggers.
1745 * No matter, that is handled in resync_timer_fn() */
1746 if (ns.conn == C_SYNC_TARGET)
1747 mod_timer(&mdev->resync_timer, jiffies);
1748
1749 drbd_md_sync(mdev);
1750 }
Lars Ellenberg5a22db82010-12-17 21:14:23 +01001751 put_ldev(mdev);
Philipp Reisner8410da82011-02-11 20:11:10 +01001752 mutex_unlock(mdev->state_mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001753}
1754
Lars Ellenbergb6dd1a82011-11-28 15:04:49 +01001755/* If the resource already closed the current epoch, but we did not
1756 * (because we have not yet seen new requests), we should send the
1757 * corresponding barrier now. Must be checked within the same spinlock
1758 * that is used to check for new requests. */
1759bool need_to_send_barrier(struct drbd_tconn *connection)
1760{
1761 if (!connection->send.seen_any_write_yet)
1762 return false;
1763
1764 /* Skip barriers that do not contain any writes.
1765 * This may happen during AHEAD mode. */
1766 if (!connection->send.current_epoch_writes)
1767 return false;
1768
1769 /* ->req_lock is held when requests are queued on
1770 * connection->sender_work, and put into ->transfer_log.
1771 * It is also held when ->current_tle_nr is increased.
1772 * So either there are already new requests queued,
1773 * and corresponding barriers will be send there.
1774 * Or nothing new is queued yet, so the difference will be 1.
1775 */
1776 if (atomic_read(&connection->current_tle_nr) !=
1777 connection->send.current_epoch_nr + 1)
1778 return false;
1779
1780 return true;
1781}
1782
Lars Ellenberg8c0785a2011-10-19 11:50:57 +02001783bool dequeue_work_batch(struct drbd_work_queue *queue, struct list_head *work_list)
1784{
1785 spin_lock_irq(&queue->q_lock);
1786 list_splice_init(&queue->q, work_list);
1787 spin_unlock_irq(&queue->q_lock);
1788 return !list_empty(work_list);
1789}
1790
1791bool dequeue_work_item(struct drbd_work_queue *queue, struct list_head *work_list)
1792{
1793 spin_lock_irq(&queue->q_lock);
1794 if (!list_empty(&queue->q))
1795 list_move(queue->q.next, work_list);
1796 spin_unlock_irq(&queue->q_lock);
1797 return !list_empty(work_list);
1798}
1799
Lars Ellenbergb6dd1a82011-11-28 15:04:49 +01001800void wait_for_work(struct drbd_tconn *connection, struct list_head *work_list)
1801{
1802 DEFINE_WAIT(wait);
1803 struct net_conf *nc;
1804 int uncork, cork;
1805
1806 dequeue_work_item(&connection->sender_work, work_list);
1807 if (!list_empty(work_list))
1808 return;
1809
1810 /* Still nothing to do?
1811 * Maybe we still need to close the current epoch,
1812 * even if no new requests are queued yet.
1813 *
1814 * Also, poke TCP, just in case.
1815 * Then wait for new work (or signal). */
1816 rcu_read_lock();
1817 nc = rcu_dereference(connection->net_conf);
1818 uncork = nc ? nc->tcp_cork : 0;
1819 rcu_read_unlock();
1820 if (uncork) {
1821 mutex_lock(&connection->data.mutex);
1822 if (connection->data.socket)
1823 drbd_tcp_uncork(connection->data.socket);
1824 mutex_unlock(&connection->data.mutex);
1825 }
1826
1827 for (;;) {
1828 int send_barrier;
1829 prepare_to_wait(&connection->sender_work.q_wait, &wait, TASK_INTERRUPTIBLE);
1830 spin_lock_irq(&connection->req_lock);
1831 spin_lock(&connection->sender_work.q_lock); /* FIXME get rid of this one? */
Lars Ellenbergbc317a92012-08-22 11:47:14 +02001832 /* dequeue single item only,
1833 * we still use drbd_queue_work_front() in some places */
1834 if (!list_empty(&connection->sender_work.q))
1835 list_move(connection->sender_work.q.next, work_list);
Lars Ellenbergb6dd1a82011-11-28 15:04:49 +01001836 spin_unlock(&connection->sender_work.q_lock); /* FIXME get rid of this one? */
1837 if (!list_empty(work_list) || signal_pending(current)) {
1838 spin_unlock_irq(&connection->req_lock);
1839 break;
1840 }
1841 send_barrier = need_to_send_barrier(connection);
1842 spin_unlock_irq(&connection->req_lock);
1843 if (send_barrier) {
1844 drbd_send_barrier(connection);
1845 connection->send.current_epoch_nr++;
1846 }
1847 schedule();
1848 /* may be woken up for other things but new work, too,
1849 * e.g. if the current epoch got closed.
1850 * In which case we send the barrier above. */
1851 }
1852 finish_wait(&connection->sender_work.q_wait, &wait);
1853
1854 /* someone may have changed the config while we have been waiting above. */
1855 rcu_read_lock();
1856 nc = rcu_dereference(connection->net_conf);
1857 cork = nc ? nc->tcp_cork : 0;
1858 rcu_read_unlock();
1859 mutex_lock(&connection->data.mutex);
1860 if (connection->data.socket) {
1861 if (cork)
1862 drbd_tcp_cork(connection->data.socket);
1863 else if (!uncork)
1864 drbd_tcp_uncork(connection->data.socket);
1865 }
1866 mutex_unlock(&connection->data.mutex);
1867}
1868
Philipp Reisnerb411b362009-09-25 16:07:19 -07001869int drbd_worker(struct drbd_thread *thi)
1870{
Philipp Reisner392c8802011-02-09 10:33:31 +01001871 struct drbd_tconn *tconn = thi->tconn;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001872 struct drbd_work *w = NULL;
Philipp Reisner0e29d162011-02-18 14:23:11 +01001873 struct drbd_conf *mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001874 LIST_HEAD(work_list);
Lars Ellenberg8c0785a2011-10-19 11:50:57 +02001875 int vnr;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001876
Andreas Gruenbachere77a0a52011-01-25 15:43:39 +01001877 while (get_t_state(thi) == RUNNING) {
Philipp Reisner80822282011-02-08 12:46:30 +01001878 drbd_thread_current_set_cpu(thi);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001879
Lars Ellenberg8c0785a2011-10-19 11:50:57 +02001880 /* as long as we use drbd_queue_work_front(),
1881 * we may only dequeue single work items here, not batches. */
1882 if (list_empty(&work_list))
Lars Ellenbergb6dd1a82011-11-28 15:04:49 +01001883 wait_for_work(tconn, &work_list);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001884
Lars Ellenberg8c0785a2011-10-19 11:50:57 +02001885 if (signal_pending(current)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001886 flush_signals(current);
Philipp Reisner19393e12011-02-09 10:09:07 +01001887 if (get_t_state(thi) == RUNNING) {
1888 conn_warn(tconn, "Worker got an unexpected signal\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07001889 continue;
Philipp Reisner19393e12011-02-09 10:09:07 +01001890 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001891 break;
1892 }
1893
Andreas Gruenbachere77a0a52011-01-25 15:43:39 +01001894 if (get_t_state(thi) != RUNNING)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001895 break;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001896
Lars Ellenberg8c0785a2011-10-19 11:50:57 +02001897 while (!list_empty(&work_list)) {
1898 w = list_first_entry(&work_list, struct drbd_work, list);
1899 list_del_init(&w->list);
1900 if (w->cb(w, tconn->cstate < C_WF_REPORT_PARAMS) == 0)
1901 continue;
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001902 if (tconn->cstate >= C_WF_REPORT_PARAMS)
1903 conn_request_state(tconn, NS(conn, C_NETWORK_FAILURE), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001904 }
1905 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001906
Lars Ellenberg8c0785a2011-10-19 11:50:57 +02001907 do {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001908 while (!list_empty(&work_list)) {
Lars Ellenberg8c0785a2011-10-19 11:50:57 +02001909 w = list_first_entry(&work_list, struct drbd_work, list);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001910 list_del_init(&w->list);
Philipp Reisner00d56942011-02-09 18:09:48 +01001911 w->cb(w, 1);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001912 }
Lars Ellenbergd5b27b02011-11-14 15:42:37 +01001913 dequeue_work_batch(&tconn->sender_work, &work_list);
Lars Ellenberg8c0785a2011-10-19 11:50:57 +02001914 } while (!list_empty(&work_list));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001915
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02001916 rcu_read_lock();
Lars Ellenbergf3990022011-03-23 14:31:09 +01001917 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
Philipp Reisner0e29d162011-02-18 14:23:11 +01001918 D_ASSERT(mdev->state.disk == D_DISKLESS && mdev->state.conn == C_STANDALONE);
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02001919 kref_get(&mdev->kref);
1920 rcu_read_unlock();
Philipp Reisner0e29d162011-02-18 14:23:11 +01001921 drbd_mdev_cleanup(mdev);
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02001922 kref_put(&mdev->kref, &drbd_minor_destroy);
1923 rcu_read_lock();
Philipp Reisner0e29d162011-02-18 14:23:11 +01001924 }
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02001925 rcu_read_unlock();
Philipp Reisnerb411b362009-09-25 16:07:19 -07001926
1927 return 0;
1928}