blob: 424dc7bdf9b7f97500c2f6afa2ec4aa69ef3e1d0 [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 Ellenbergedc9f5e2012-09-27 15:18:21 +0200109 __drbd_chk_io_error(mdev, DRBD_READ_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;
Lars Ellenberga506c132012-09-26 14:16:30 +0200137 list_move_tail(&peer_req->w.list, &mdev->done_ee);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700138
Andreas Gruenbacherbb3bfe92011-01-21 15:59:23 +0100139 /*
Andreas Gruenbacher5e472262011-01-27 14:42:51 +0100140 * Do not remove from the write_requests tree here: we did not send the
Andreas Gruenbacherbb3bfe92011-01-21 15:59:23 +0100141 * Ack yet and did not wake possibly waiting conflicting requests.
142 * Removed from the tree from "drbd_process_done_ee" within the
143 * appropriate w.cb (e_end_block/e_end_resync_block) or from
144 * _drbd_clear_done_ee.
145 */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700146
Andreas Gruenbacher579b57e2011-01-13 18:40:57 +0100147 do_wake = list_empty(block_id == ID_SYNCER ? &mdev->sync_ee : &mdev->active_ee);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700148
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100149 if (test_bit(__EE_WAS_ERROR, &peer_req->flags))
Lars Ellenbergedc9f5e2012-09-27 15:18:21 +0200150 __drbd_chk_io_error(mdev, DRBD_WRITE_ERROR);
Philipp Reisner87eeee42011-01-19 14:16:30 +0100151 spin_unlock_irqrestore(&mdev->tconn->req_lock, flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700152
Andreas Gruenbacher579b57e2011-01-13 18:40:57 +0100153 if (block_id == ID_SYNCER)
Lars Ellenberg181286a2011-03-31 15:18:56 +0200154 drbd_rs_complete_io(mdev, i.sector);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700155
156 if (do_wake)
157 wake_up(&mdev->ee_wait);
158
159 if (do_al_complete_io)
Lars Ellenberg181286a2011-03-31 15:18:56 +0200160 drbd_al_complete_io(mdev, &i);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700161
Philipp Reisner0625ac12011-02-07 14:49:19 +0100162 wake_asender(mdev->tconn);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700163 put_ldev(mdev);
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200164}
Philipp Reisnerb411b362009-09-25 16:07:19 -0700165
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200166/* writes on behalf of the partner, or resync writes,
167 * "submitted" by the receiver.
168 */
Andreas Gruenbacherfcefa622011-02-17 16:46:59 +0100169void drbd_peer_request_endio(struct bio *bio, int error)
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200170{
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100171 struct drbd_peer_request *peer_req = bio->bi_private;
Philipp Reisnera21e9292011-02-08 15:08:49 +0100172 struct drbd_conf *mdev = peer_req->w.mdev;
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200173 int uptodate = bio_flagged(bio, BIO_UPTODATE);
174 int is_write = bio_data_dir(bio) == WRITE;
175
Lars Ellenberg07194272010-12-20 15:38:07 +0100176 if (error && __ratelimit(&drbd_ratelimit_state))
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200177 dev_warn(DEV, "%s: error=%d s=%llus\n",
178 is_write ? "write" : "read", error,
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100179 (unsigned long long)peer_req->i.sector);
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200180 if (!error && !uptodate) {
Lars Ellenberg07194272010-12-20 15:38:07 +0100181 if (__ratelimit(&drbd_ratelimit_state))
182 dev_warn(DEV, "%s: setting error to -EIO s=%llus\n",
183 is_write ? "write" : "read",
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100184 (unsigned long long)peer_req->i.sector);
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200185 /* strange behavior of some lower level drivers...
186 * fail the request by clearing the uptodate flag,
187 * but do not return any error?! */
188 error = -EIO;
189 }
190
191 if (error)
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100192 set_bit(__EE_WAS_ERROR, &peer_req->flags);
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200193
194 bio_put(bio); /* no need for the bio anymore */
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100195 if (atomic_dec_and_test(&peer_req->pending_bios)) {
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200196 if (is_write)
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100197 drbd_endio_write_sec_final(peer_req);
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200198 else
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100199 drbd_endio_read_sec_final(peer_req);
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200200 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700201}
202
203/* read, readA or write requests on R_PRIMARY coming from drbd_make_request
204 */
Andreas Gruenbacherfcefa622011-02-17 16:46:59 +0100205void drbd_request_endio(struct bio *bio, int error)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700206{
Lars Ellenberga1154132010-11-13 20:42:29 +0100207 unsigned long flags;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700208 struct drbd_request *req = bio->bi_private;
Philipp Reisnera21e9292011-02-08 15:08:49 +0100209 struct drbd_conf *mdev = req->w.mdev;
Lars Ellenberga1154132010-11-13 20:42:29 +0100210 struct bio_and_error m;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700211 enum drbd_req_event what;
212 int uptodate = bio_flagged(bio, BIO_UPTODATE);
213
Philipp Reisnerb411b362009-09-25 16:07:19 -0700214 if (!error && !uptodate) {
215 dev_warn(DEV, "p %s: setting error to -EIO\n",
216 bio_data_dir(bio) == WRITE ? "write" : "read");
217 /* strange behavior of some lower level drivers...
218 * fail the request by clearing the uptodate flag,
219 * but do not return any error?! */
220 error = -EIO;
221 }
222
Philipp Reisner1b6dd252012-09-04 15:16:20 +0200223
224 /* If this request was aborted locally before,
225 * but now was completed "successfully",
226 * chances are that this caused arbitrary data corruption.
227 *
228 * "aborting" requests, or force-detaching the disk, is intended for
229 * completely blocked/hung local backing devices which do no longer
230 * complete requests at all, not even do error completions. In this
231 * situation, usually a hard-reset and failover is the only way out.
232 *
233 * By "aborting", basically faking a local error-completion,
234 * we allow for a more graceful swichover by cleanly migrating services.
235 * Still the affected node has to be rebooted "soon".
236 *
237 * By completing these requests, we allow the upper layers to re-use
238 * the associated data pages.
239 *
240 * If later the local backing device "recovers", and now DMAs some data
241 * from disk into the original request pages, in the best case it will
242 * just put random data into unused pages; but typically it will corrupt
243 * meanwhile completely unrelated data, causing all sorts of damage.
244 *
245 * Which means delayed successful completion,
246 * especially for READ requests,
247 * is a reason to panic().
248 *
249 * We assume that a delayed *error* completion is OK,
250 * though we still will complain noisily about it.
251 */
252 if (unlikely(req->rq_state & RQ_LOCAL_ABORTED)) {
253 if (__ratelimit(&drbd_ratelimit_state))
254 dev_emerg(DEV, "delayed completion of aborted local request; disk-timeout may be too aggressive\n");
255
256 if (!error)
257 panic("possible random memory corruption caused by delayed completion of aborted local request\n");
258 }
259
Philipp Reisnerb411b362009-09-25 16:07:19 -0700260 /* to avoid recursion in __req_mod */
261 if (unlikely(error)) {
262 what = (bio_data_dir(bio) == WRITE)
Andreas Gruenbacher8554df12011-01-25 15:37:43 +0100263 ? WRITE_COMPLETED_WITH_ERROR
Lars Ellenberg5c3c7e62010-04-10 02:10:09 +0200264 : (bio_rw(bio) == READ)
Andreas Gruenbacher8554df12011-01-25 15:37:43 +0100265 ? READ_COMPLETED_WITH_ERROR
266 : READ_AHEAD_COMPLETED_WITH_ERROR;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700267 } else
Andreas Gruenbacher8554df12011-01-25 15:37:43 +0100268 what = COMPLETED_OK;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700269
270 bio_put(req->private_bio);
271 req->private_bio = ERR_PTR(error);
272
Lars Ellenberga1154132010-11-13 20:42:29 +0100273 /* not req_mod(), we need irqsave here! */
Philipp Reisner87eeee42011-01-19 14:16:30 +0100274 spin_lock_irqsave(&mdev->tconn->req_lock, flags);
Lars Ellenberga1154132010-11-13 20:42:29 +0100275 __req_mod(req, what, &m);
Philipp Reisner87eeee42011-01-19 14:16:30 +0100276 spin_unlock_irqrestore(&mdev->tconn->req_lock, flags);
Lars Ellenberg24153082012-03-26 17:06:29 +0200277 put_ldev(mdev);
Lars Ellenberga1154132010-11-13 20:42:29 +0100278
279 if (m.bio)
280 complete_master_bio(mdev, &m);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700281}
282
Andreas Gruenbacherf6ffca92011-02-04 15:30:34 +0100283void drbd_csum_ee(struct drbd_conf *mdev, struct crypto_hash *tfm,
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100284 struct drbd_peer_request *peer_req, void *digest)
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200285{
286 struct hash_desc desc;
287 struct scatterlist sg;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100288 struct page *page = peer_req->pages;
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200289 struct page *tmp;
290 unsigned len;
291
292 desc.tfm = tfm;
293 desc.flags = 0;
294
295 sg_init_table(&sg, 1);
296 crypto_hash_init(&desc);
297
298 while ((tmp = page_chain_next(page))) {
299 /* all but the last page will be fully used */
300 sg_set_page(&sg, page, PAGE_SIZE, 0);
301 crypto_hash_update(&desc, &sg, sg.length);
302 page = tmp;
303 }
304 /* and now the last, possibly only partially used page */
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100305 len = peer_req->i.size & (PAGE_SIZE - 1);
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200306 sg_set_page(&sg, page, len ?: PAGE_SIZE, 0);
307 crypto_hash_update(&desc, &sg, sg.length);
308 crypto_hash_final(&desc, digest);
309}
310
311void drbd_csum_bio(struct drbd_conf *mdev, struct crypto_hash *tfm, struct bio *bio, void *digest)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700312{
313 struct hash_desc desc;
314 struct scatterlist sg;
315 struct bio_vec *bvec;
316 int i;
317
318 desc.tfm = tfm;
319 desc.flags = 0;
320
321 sg_init_table(&sg, 1);
322 crypto_hash_init(&desc);
323
Lars Ellenberg4b8514e2012-03-26 16:12:49 +0200324 bio_for_each_segment(bvec, bio, i) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700325 sg_set_page(&sg, bvec->bv_page, bvec->bv_len, bvec->bv_offset);
326 crypto_hash_update(&desc, &sg, sg.length);
327 }
328 crypto_hash_final(&desc, digest);
329}
330
Lars Ellenberg9676c762011-02-22 14:02:31 +0100331/* MAYBE merge common code with w_e_end_ov_req */
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +0100332static int w_e_send_csum(struct drbd_work *w, int cancel)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700333{
Philipp Reisner00d56942011-02-09 18:09:48 +0100334 struct drbd_peer_request *peer_req = container_of(w, struct drbd_peer_request, w);
335 struct drbd_conf *mdev = w->mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700336 int digest_size;
337 void *digest;
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +0100338 int err = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700339
Lars Ellenberg53ea4332011-03-08 17:11:40 +0100340 if (unlikely(cancel))
341 goto out;
342
Lars Ellenberg9676c762011-02-22 14:02:31 +0100343 if (unlikely((peer_req->flags & EE_WAS_ERROR) != 0))
Lars Ellenberg53ea4332011-03-08 17:11:40 +0100344 goto out;
345
Lars Ellenbergf3990022011-03-23 14:31:09 +0100346 digest_size = crypto_hash_digestsize(mdev->tconn->csums_tfm);
Lars Ellenberg53ea4332011-03-08 17:11:40 +0100347 digest = kmalloc(digest_size, GFP_NOIO);
348 if (digest) {
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100349 sector_t sector = peer_req->i.sector;
350 unsigned int size = peer_req->i.size;
Lars Ellenbergf3990022011-03-23 14:31:09 +0100351 drbd_csum_ee(mdev, mdev->tconn->csums_tfm, peer_req, digest);
Lars Ellenberg9676c762011-02-22 14:02:31 +0100352 /* Free peer_req and pages before send.
Lars Ellenberg53ea4332011-03-08 17:11:40 +0100353 * In case we block on congestion, we could otherwise run into
354 * some distributed deadlock, if the other side blocks on
355 * congestion as well, because our receiver blocks in
Andreas Gruenbacherc37c8ec2011-04-07 21:02:09 +0200356 * drbd_alloc_pages due to pp_in_use > max_buffers. */
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +0200357 drbd_free_peer_req(mdev, peer_req);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100358 peer_req = NULL;
Lars Ellenberg53ea4332011-03-08 17:11:40 +0100359 inc_rs_pending(mdev);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +0100360 err = drbd_send_drequest_csum(mdev, sector, size,
Andreas Gruenbacherdb1b0b72011-03-16 01:37:21 +0100361 digest, digest_size,
362 P_CSUM_RS_REQUEST);
Lars Ellenberg53ea4332011-03-08 17:11:40 +0100363 kfree(digest);
364 } else {
365 dev_err(DEV, "kmalloc() of digest failed.\n");
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +0100366 err = -ENOMEM;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700367 }
368
Lars Ellenberg53ea4332011-03-08 17:11:40 +0100369out:
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100370 if (peer_req)
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +0200371 drbd_free_peer_req(mdev, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700372
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +0100373 if (unlikely(err))
Philipp Reisnerb411b362009-09-25 16:07:19 -0700374 dev_err(DEV, "drbd_send_drequest(..., csum) failed\n");
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +0100375 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700376}
377
378#define GFP_TRY (__GFP_HIGHMEM | __GFP_NOWARN)
379
380static int read_for_csum(struct drbd_conf *mdev, sector_t sector, int size)
381{
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100382 struct drbd_peer_request *peer_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700383
384 if (!get_ldev(mdev))
Lars Ellenberg80a40e42010-08-11 23:28:00 +0200385 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700386
Philipp Reisnere3555d82010-11-07 15:56:29 +0100387 if (drbd_rs_should_slow_down(mdev, sector))
Lars Ellenberg0f0601f2010-08-11 23:40:24 +0200388 goto defer;
389
Philipp Reisnerb411b362009-09-25 16:07:19 -0700390 /* GFP_TRY, because if there is no memory available right now, this may
391 * be rescheduled for later. It is "only" background resync, after all. */
Andreas Gruenbacher0db55362011-04-06 16:09:15 +0200392 peer_req = drbd_alloc_peer_req(mdev, ID_SYNCER /* unused */, sector,
393 size, GFP_TRY);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100394 if (!peer_req)
Lars Ellenberg80a40e42010-08-11 23:28:00 +0200395 goto defer;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700396
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100397 peer_req->w.cb = w_e_send_csum;
Philipp Reisner87eeee42011-01-19 14:16:30 +0100398 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100399 list_add(&peer_req->w.list, &mdev->read_ee);
Philipp Reisner87eeee42011-01-19 14:16:30 +0100400 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700401
Lars Ellenberg0f0601f2010-08-11 23:40:24 +0200402 atomic_add(size >> 9, &mdev->rs_sect_ev);
Andreas Gruenbacherfbe29de2011-02-17 16:38:35 +0100403 if (drbd_submit_peer_request(mdev, peer_req, READ, DRBD_FAULT_RS_RD) == 0)
Lars Ellenberg80a40e42010-08-11 23:28:00 +0200404 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700405
Lars Ellenberg10f6d9922011-01-24 14:47:09 +0100406 /* If it failed because of ENOMEM, retry should help. If it failed
407 * because bio_add_page failed (probably broken lower level driver),
408 * retry may or may not help.
409 * If it does not, you may need to force disconnect. */
Philipp Reisner87eeee42011-01-19 14:16:30 +0100410 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100411 list_del(&peer_req->w.list);
Philipp Reisner87eeee42011-01-19 14:16:30 +0100412 spin_unlock_irq(&mdev->tconn->req_lock);
Lars Ellenberg22cc37a2010-09-14 20:40:41 +0200413
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +0200414 drbd_free_peer_req(mdev, peer_req);
Lars Ellenberg80a40e42010-08-11 23:28:00 +0200415defer:
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200416 put_ldev(mdev);
Lars Ellenberg80a40e42010-08-11 23:28:00 +0200417 return -EAGAIN;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700418}
419
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +0100420int w_resync_timer(struct drbd_work *w, int cancel)
Philipp Reisner794abb72010-12-27 11:51:23 +0100421{
Philipp Reisner00d56942011-02-09 18:09:48 +0100422 struct drbd_conf *mdev = w->mdev;
Philipp Reisner794abb72010-12-27 11:51:23 +0100423 switch (mdev->state.conn) {
424 case C_VERIFY_S:
Philipp Reisner00d56942011-02-09 18:09:48 +0100425 w_make_ov_request(w, cancel);
Philipp Reisner794abb72010-12-27 11:51:23 +0100426 break;
427 case C_SYNC_TARGET:
Philipp Reisner00d56942011-02-09 18:09:48 +0100428 w_make_resync_request(w, cancel);
Philipp Reisner794abb72010-12-27 11:51:23 +0100429 break;
430 }
431
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +0100432 return 0;
Philipp Reisner794abb72010-12-27 11:51:23 +0100433}
434
Philipp Reisnerb411b362009-09-25 16:07:19 -0700435void resync_timer_fn(unsigned long data)
436{
Philipp Reisnerb411b362009-09-25 16:07:19 -0700437 struct drbd_conf *mdev = (struct drbd_conf *) data;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700438
Philipp Reisner794abb72010-12-27 11:51:23 +0100439 if (list_empty(&mdev->resync_work.list))
Lars Ellenbergd5b27b02011-11-14 15:42:37 +0100440 drbd_queue_work(&mdev->tconn->sender_work, &mdev->resync_work);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700441}
442
Philipp Reisner778f2712010-07-06 11:14:00 +0200443static void fifo_set(struct fifo_buffer *fb, int value)
444{
445 int i;
446
447 for (i = 0; i < fb->size; i++)
Philipp Reisnerf10f2622010-10-05 16:50:17 +0200448 fb->values[i] = value;
Philipp Reisner778f2712010-07-06 11:14:00 +0200449}
450
451static int fifo_push(struct fifo_buffer *fb, int value)
452{
453 int ov;
454
455 ov = fb->values[fb->head_index];
456 fb->values[fb->head_index++] = value;
457
458 if (fb->head_index >= fb->size)
459 fb->head_index = 0;
460
461 return ov;
462}
463
464static void fifo_add_val(struct fifo_buffer *fb, int value)
465{
466 int i;
467
468 for (i = 0; i < fb->size; i++)
469 fb->values[i] += value;
470}
471
Philipp Reisner9958c852011-05-03 16:19:31 +0200472struct fifo_buffer *fifo_alloc(int fifo_size)
473{
474 struct fifo_buffer *fb;
475
Lars Ellenberg8747d302012-09-26 14:22:40 +0200476 fb = kzalloc(sizeof(struct fifo_buffer) + sizeof(int) * fifo_size, GFP_NOIO);
Philipp Reisner9958c852011-05-03 16:19:31 +0200477 if (!fb)
478 return NULL;
479
480 fb->head_index = 0;
481 fb->size = fifo_size;
482 fb->total = 0;
483
484 return fb;
485}
486
Philipp Reisner9d77a5f2010-11-07 18:02:56 +0100487static int drbd_rs_controller(struct drbd_conf *mdev)
Philipp Reisner778f2712010-07-06 11:14:00 +0200488{
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +0200489 struct disk_conf *dc;
Philipp Reisner778f2712010-07-06 11:14:00 +0200490 unsigned int sect_in; /* Number of sectors that came in since the last turn */
491 unsigned int want; /* The number of sectors we want in the proxy */
492 int req_sect; /* Number of sectors to request in this turn */
493 int correction; /* Number of sectors more we need in the proxy*/
494 int cps; /* correction per invocation of drbd_rs_controller() */
495 int steps; /* Number of time steps to plan ahead */
496 int curr_corr;
497 int max_sect;
Philipp Reisner813472c2011-05-03 16:47:02 +0200498 struct fifo_buffer *plan;
Philipp Reisner778f2712010-07-06 11:14:00 +0200499
500 sect_in = atomic_xchg(&mdev->rs_sect_in, 0); /* Number of sectors that came in */
501 mdev->rs_in_flight -= sect_in;
502
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +0200503 dc = rcu_dereference(mdev->ldev->disk_conf);
Philipp Reisner813472c2011-05-03 16:47:02 +0200504 plan = rcu_dereference(mdev->rs_plan_s);
Philipp Reisner778f2712010-07-06 11:14:00 +0200505
Philipp Reisner813472c2011-05-03 16:47:02 +0200506 steps = plan->size; /* (dc->c_plan_ahead * 10 * SLEEP_TIME) / HZ; */
Philipp Reisner778f2712010-07-06 11:14:00 +0200507
508 if (mdev->rs_in_flight + sect_in == 0) { /* At start of resync */
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +0200509 want = ((dc->resync_rate * 2 * SLEEP_TIME) / HZ) * steps;
Philipp Reisner778f2712010-07-06 11:14:00 +0200510 } else { /* normal path */
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +0200511 want = dc->c_fill_target ? dc->c_fill_target :
512 sect_in * dc->c_delay_target * HZ / (SLEEP_TIME * 10);
Philipp Reisner778f2712010-07-06 11:14:00 +0200513 }
514
Philipp Reisner813472c2011-05-03 16:47:02 +0200515 correction = want - mdev->rs_in_flight - plan->total;
Philipp Reisner778f2712010-07-06 11:14:00 +0200516
517 /* Plan ahead */
518 cps = correction / steps;
Philipp Reisner813472c2011-05-03 16:47:02 +0200519 fifo_add_val(plan, cps);
520 plan->total += cps * steps;
Philipp Reisner778f2712010-07-06 11:14:00 +0200521
522 /* What we do in this step */
Philipp Reisner813472c2011-05-03 16:47:02 +0200523 curr_corr = fifo_push(plan, 0);
524 plan->total -= curr_corr;
Philipp Reisner778f2712010-07-06 11:14:00 +0200525
526 req_sect = sect_in + curr_corr;
527 if (req_sect < 0)
528 req_sect = 0;
529
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +0200530 max_sect = (dc->c_max_rate * 2 * SLEEP_TIME) / HZ;
Philipp Reisner778f2712010-07-06 11:14:00 +0200531 if (req_sect > max_sect)
532 req_sect = max_sect;
533
534 /*
535 dev_warn(DEV, "si=%u if=%d wa=%u co=%d st=%d cps=%d pl=%d cc=%d rs=%d\n",
536 sect_in, mdev->rs_in_flight, want, correction,
537 steps, cps, mdev->rs_planed, curr_corr, req_sect);
538 */
539
540 return req_sect;
541}
542
Philipp Reisner9d77a5f2010-11-07 18:02:56 +0100543static int drbd_rs_number_requests(struct drbd_conf *mdev)
Lars Ellenberge65f4402010-11-05 10:04:07 +0100544{
545 int number;
Philipp Reisner813472c2011-05-03 16:47:02 +0200546
547 rcu_read_lock();
548 if (rcu_dereference(mdev->rs_plan_s)->size) {
Lars Ellenberge65f4402010-11-05 10:04:07 +0100549 number = drbd_rs_controller(mdev) >> (BM_BLOCK_SHIFT - 9);
550 mdev->c_sync_rate = number * HZ * (BM_BLOCK_SIZE / 1024) / SLEEP_TIME;
551 } else {
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +0200552 mdev->c_sync_rate = rcu_dereference(mdev->ldev->disk_conf)->resync_rate;
Lars Ellenberge65f4402010-11-05 10:04:07 +0100553 number = SLEEP_TIME * mdev->c_sync_rate / ((BM_BLOCK_SIZE / 1024) * HZ);
554 }
Philipp Reisner813472c2011-05-03 16:47:02 +0200555 rcu_read_unlock();
Lars Ellenberge65f4402010-11-05 10:04:07 +0100556
Lars Ellenberge65f4402010-11-05 10:04:07 +0100557 /* ignore the amount of pending requests, the resync controller should
558 * throttle down to incoming reply rate soon enough anyways. */
559 return number;
560}
561
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +0100562int w_make_resync_request(struct drbd_work *w, int cancel)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700563{
Philipp Reisner00d56942011-02-09 18:09:48 +0100564 struct drbd_conf *mdev = w->mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700565 unsigned long bit;
566 sector_t sector;
567 const sector_t capacity = drbd_get_capacity(mdev->this_bdev);
Lars Ellenberg1816a2b2010-11-11 15:19:07 +0100568 int max_bio_size;
Lars Ellenberge65f4402010-11-05 10:04:07 +0100569 int number, rollback_i, size;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700570 int align, queued, sndbuf;
Lars Ellenberg0f0601f2010-08-11 23:40:24 +0200571 int i = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700572
573 if (unlikely(cancel))
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +0100574 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700575
Lars Ellenbergaf85e8e2010-10-07 16:07:55 +0200576 if (mdev->rs_total == 0) {
577 /* empty resync? */
578 drbd_resync_finished(mdev);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +0100579 return 0;
Lars Ellenbergaf85e8e2010-10-07 16:07:55 +0200580 }
581
Philipp Reisnerb411b362009-09-25 16:07:19 -0700582 if (!get_ldev(mdev)) {
583 /* Since we only need to access mdev->rsync a
584 get_ldev_if_state(mdev,D_FAILED) would be sufficient, but
585 to continue resync with a broken disk makes no sense at
586 all */
587 dev_err(DEV, "Disk broke down during resync!\n");
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +0100588 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700589 }
590
Philipp Reisner0cfdd242011-05-25 11:14:35 +0200591 max_bio_size = queue_max_hw_sectors(mdev->rq_queue) << 9;
Lars Ellenberge65f4402010-11-05 10:04:07 +0100592 number = drbd_rs_number_requests(mdev);
593 if (number == 0)
Lars Ellenberg0f0601f2010-08-11 23:40:24 +0200594 goto requeue;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700595
Philipp Reisnerb411b362009-09-25 16:07:19 -0700596 for (i = 0; i < number; i++) {
597 /* Stop generating RS requests, when half of the send buffer is filled */
Philipp Reisnere42325a2011-01-19 13:55:45 +0100598 mutex_lock(&mdev->tconn->data.mutex);
599 if (mdev->tconn->data.socket) {
600 queued = mdev->tconn->data.socket->sk->sk_wmem_queued;
601 sndbuf = mdev->tconn->data.socket->sk->sk_sndbuf;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700602 } else {
603 queued = 1;
604 sndbuf = 0;
605 }
Philipp Reisnere42325a2011-01-19 13:55:45 +0100606 mutex_unlock(&mdev->tconn->data.mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700607 if (queued > sndbuf / 2)
608 goto requeue;
609
610next_sector:
611 size = BM_BLOCK_SIZE;
612 bit = drbd_bm_find_next(mdev, mdev->bm_resync_fo);
613
Lars Ellenberg4b0715f2010-12-14 15:13:04 +0100614 if (bit == DRBD_END_OF_BITMAP) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700615 mdev->bm_resync_fo = drbd_bm_bits(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700616 put_ldev(mdev);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +0100617 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700618 }
619
620 sector = BM_BIT_TO_SECT(bit);
621
Philipp Reisnere3555d82010-11-07 15:56:29 +0100622 if (drbd_rs_should_slow_down(mdev, sector) ||
623 drbd_try_rs_begin_io(mdev, sector)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700624 mdev->bm_resync_fo = bit;
625 goto requeue;
626 }
627 mdev->bm_resync_fo = bit + 1;
628
629 if (unlikely(drbd_bm_test_bit(mdev, bit) == 0)) {
630 drbd_rs_complete_io(mdev, sector);
631 goto next_sector;
632 }
633
Lars Ellenberg1816a2b2010-11-11 15:19:07 +0100634#if DRBD_MAX_BIO_SIZE > BM_BLOCK_SIZE
Philipp Reisnerb411b362009-09-25 16:07:19 -0700635 /* try to find some adjacent bits.
636 * we stop if we have already the maximum req size.
637 *
638 * Additionally always align bigger requests, in order to
639 * be prepared for all stripe sizes of software RAIDs.
Philipp Reisnerb411b362009-09-25 16:07:19 -0700640 */
641 align = 1;
Philipp Reisnerd2074502010-07-22 15:27:27 +0200642 rollback_i = i;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700643 for (;;) {
Lars Ellenberg1816a2b2010-11-11 15:19:07 +0100644 if (size + BM_BLOCK_SIZE > max_bio_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700645 break;
646
647 /* Be always aligned */
648 if (sector & ((1<<(align+3))-1))
649 break;
650
651 /* do not cross extent boundaries */
652 if (((bit+1) & BM_BLOCKS_PER_BM_EXT_MASK) == 0)
653 break;
654 /* now, is it actually dirty, after all?
655 * caution, drbd_bm_test_bit is tri-state for some
656 * obscure reason; ( b == 0 ) would get the out-of-band
657 * only accidentally right because of the "oddly sized"
658 * adjustment below */
659 if (drbd_bm_test_bit(mdev, bit+1) != 1)
660 break;
661 bit++;
662 size += BM_BLOCK_SIZE;
663 if ((BM_BLOCK_SIZE << align) <= size)
664 align++;
665 i++;
666 }
667 /* if we merged some,
668 * reset the offset to start the next drbd_bm_find_next from */
669 if (size > BM_BLOCK_SIZE)
670 mdev->bm_resync_fo = bit + 1;
671#endif
672
673 /* adjust very last sectors, in case we are oddly sized */
674 if (sector + (size>>9) > capacity)
675 size = (capacity-sector)<<9;
Lars Ellenbergf3990022011-03-23 14:31:09 +0100676 if (mdev->tconn->agreed_pro_version >= 89 && mdev->tconn->csums_tfm) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700677 switch (read_for_csum(mdev, sector, size)) {
Lars Ellenberg80a40e42010-08-11 23:28:00 +0200678 case -EIO: /* Disk failure */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700679 put_ldev(mdev);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +0100680 return -EIO;
Lars Ellenberg80a40e42010-08-11 23:28:00 +0200681 case -EAGAIN: /* allocation failed, or ldev busy */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700682 drbd_rs_complete_io(mdev, sector);
683 mdev->bm_resync_fo = BM_SECT_TO_BIT(sector);
Philipp Reisnerd2074502010-07-22 15:27:27 +0200684 i = rollback_i;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700685 goto requeue;
Lars Ellenberg80a40e42010-08-11 23:28:00 +0200686 case 0:
687 /* everything ok */
688 break;
689 default:
690 BUG();
Philipp Reisnerb411b362009-09-25 16:07:19 -0700691 }
692 } else {
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +0100693 int err;
694
Philipp Reisnerb411b362009-09-25 16:07:19 -0700695 inc_rs_pending(mdev);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +0100696 err = drbd_send_drequest(mdev, P_RS_DATA_REQUEST,
697 sector, size, ID_SYNCER);
698 if (err) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700699 dev_err(DEV, "drbd_send_drequest() failed, aborting...\n");
700 dec_rs_pending(mdev);
701 put_ldev(mdev);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +0100702 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700703 }
704 }
705 }
706
707 if (mdev->bm_resync_fo >= drbd_bm_bits(mdev)) {
708 /* last syncer _request_ was sent,
709 * but the P_RS_DATA_REPLY not yet received. sync will end (and
710 * next sync group will resume), as soon as we receive the last
711 * resync data block, and the last bit is cleared.
712 * until then resync "work" is "inactive" ...
713 */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700714 put_ldev(mdev);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +0100715 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700716 }
717
718 requeue:
Philipp Reisner778f2712010-07-06 11:14:00 +0200719 mdev->rs_in_flight += (i << (BM_BLOCK_SHIFT - 9));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700720 mod_timer(&mdev->resync_timer, jiffies + SLEEP_TIME);
721 put_ldev(mdev);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +0100722 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700723}
724
Philipp Reisner00d56942011-02-09 18:09:48 +0100725static int w_make_ov_request(struct drbd_work *w, int cancel)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700726{
Philipp Reisner00d56942011-02-09 18:09:48 +0100727 struct drbd_conf *mdev = w->mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700728 int number, i, size;
729 sector_t sector;
730 const sector_t capacity = drbd_get_capacity(mdev->this_bdev);
Lars Ellenberg58ffa582012-07-26 14:09:49 +0200731 bool stop_sector_reached = false;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700732
733 if (unlikely(cancel))
734 return 1;
735
Lars Ellenberg2649f082010-11-05 10:05:47 +0100736 number = drbd_rs_number_requests(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700737
738 sector = mdev->ov_position;
739 for (i = 0; i < number; i++) {
Lars Ellenberg58ffa582012-07-26 14:09:49 +0200740 if (sector >= capacity)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700741 return 1;
Lars Ellenberg58ffa582012-07-26 14:09:49 +0200742
743 /* We check for "finished" only in the reply path:
744 * w_e_end_ov_reply().
745 * We need to send at least one request out. */
746 stop_sector_reached = i > 0
747 && verify_can_do_stop_sector(mdev)
748 && sector >= mdev->ov_stop_sector;
749 if (stop_sector_reached)
750 break;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700751
752 size = BM_BLOCK_SIZE;
753
Philipp Reisnere3555d82010-11-07 15:56:29 +0100754 if (drbd_rs_should_slow_down(mdev, sector) ||
755 drbd_try_rs_begin_io(mdev, sector)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700756 mdev->ov_position = sector;
757 goto requeue;
758 }
759
760 if (sector + (size>>9) > capacity)
761 size = (capacity-sector)<<9;
762
763 inc_rs_pending(mdev);
Andreas Gruenbacher5b9f4992011-03-16 01:31:39 +0100764 if (drbd_send_ov_request(mdev, sector, size)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700765 dec_rs_pending(mdev);
766 return 0;
767 }
768 sector += BM_SECT_PER_BIT;
769 }
770 mdev->ov_position = sector;
771
772 requeue:
Lars Ellenberg2649f082010-11-05 10:05:47 +0100773 mdev->rs_in_flight += (i << (BM_BLOCK_SHIFT - 9));
Lars Ellenberg58ffa582012-07-26 14:09:49 +0200774 if (i == 0 || !stop_sector_reached)
775 mod_timer(&mdev->resync_timer, jiffies + SLEEP_TIME);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700776 return 1;
777}
778
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +0100779int w_ov_finished(struct drbd_work *w, int cancel)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700780{
Philipp Reisner00d56942011-02-09 18:09:48 +0100781 struct drbd_conf *mdev = w->mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700782 kfree(w);
Andreas Gruenbacher8f7bed72010-12-19 23:53:14 +0100783 ov_out_of_sync_print(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700784 drbd_resync_finished(mdev);
785
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +0100786 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700787}
788
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +0100789static int w_resync_finished(struct drbd_work *w, int cancel)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700790{
Philipp Reisner00d56942011-02-09 18:09:48 +0100791 struct drbd_conf *mdev = w->mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700792 kfree(w);
793
794 drbd_resync_finished(mdev);
795
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +0100796 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700797}
798
Lars Ellenbergaf85e8e2010-10-07 16:07:55 +0200799static void ping_peer(struct drbd_conf *mdev)
800{
Philipp Reisner2a67d8b2011-02-09 14:10:32 +0100801 struct drbd_tconn *tconn = mdev->tconn;
802
803 clear_bit(GOT_PING_ACK, &tconn->flags);
804 request_ping(tconn);
805 wait_event(tconn->ping_wait,
806 test_bit(GOT_PING_ACK, &tconn->flags) || mdev->state.conn < C_CONNECTED);
Lars Ellenbergaf85e8e2010-10-07 16:07:55 +0200807}
808
Philipp Reisnerb411b362009-09-25 16:07:19 -0700809int drbd_resync_finished(struct drbd_conf *mdev)
810{
811 unsigned long db, dt, dbdt;
812 unsigned long n_oos;
813 union drbd_state os, ns;
814 struct drbd_work *w;
815 char *khelper_cmd = NULL;
Lars Ellenberg26525612010-11-05 09:56:33 +0100816 int verify_done = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700817
818 /* Remove all elements from the resync LRU. Since future actions
819 * might set bits in the (main) bitmap, then the entries in the
820 * resync LRU would be wrong. */
821 if (drbd_rs_del_all(mdev)) {
822 /* In case this is not possible now, most probably because
823 * there are P_RS_DATA_REPLY Packets lingering on the worker's
824 * queue (or even the read operations for those packets
825 * is not finished by now). Retry in 100ms. */
826
Philipp Reisner20ee6392011-01-18 15:28:59 +0100827 schedule_timeout_interruptible(HZ / 10);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700828 w = kmalloc(sizeof(struct drbd_work), GFP_ATOMIC);
829 if (w) {
830 w->cb = w_resync_finished;
Philipp Reisner9b743da2011-07-15 18:15:45 +0200831 w->mdev = mdev;
Lars Ellenbergd5b27b02011-11-14 15:42:37 +0100832 drbd_queue_work(&mdev->tconn->sender_work, w);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700833 return 1;
834 }
835 dev_err(DEV, "Warn failed to drbd_rs_del_all() and to kmalloc(w).\n");
836 }
837
838 dt = (jiffies - mdev->rs_start - mdev->rs_paused) / HZ;
839 if (dt <= 0)
840 dt = 1;
Lars Ellenberg58ffa582012-07-26 14:09:49 +0200841
Philipp Reisnerb411b362009-09-25 16:07:19 -0700842 db = mdev->rs_total;
Lars Ellenberg58ffa582012-07-26 14:09:49 +0200843 /* adjust for verify start and stop sectors, respective reached position */
844 if (mdev->state.conn == C_VERIFY_S || mdev->state.conn == C_VERIFY_T)
845 db -= mdev->ov_left;
846
Philipp Reisnerb411b362009-09-25 16:07:19 -0700847 dbdt = Bit2KB(db/dt);
848 mdev->rs_paused /= HZ;
849
850 if (!get_ldev(mdev))
851 goto out;
852
Lars Ellenbergaf85e8e2010-10-07 16:07:55 +0200853 ping_peer(mdev);
854
Philipp Reisner87eeee42011-01-19 14:16:30 +0100855 spin_lock_irq(&mdev->tconn->req_lock);
Philipp Reisner78bae592011-03-28 15:40:12 +0200856 os = drbd_read_state(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700857
Lars Ellenberg26525612010-11-05 09:56:33 +0100858 verify_done = (os.conn == C_VERIFY_S || os.conn == C_VERIFY_T);
859
Philipp Reisnerb411b362009-09-25 16:07:19 -0700860 /* This protects us against multiple calls (that can happen in the presence
861 of application IO), and against connectivity loss just before we arrive here. */
862 if (os.conn <= C_CONNECTED)
863 goto out_unlock;
864
865 ns = os;
866 ns.conn = C_CONNECTED;
867
868 dev_info(DEV, "%s done (total %lu sec; paused %lu sec; %lu K/sec)\n",
Lars Ellenberg58ffa582012-07-26 14:09:49 +0200869 verify_done ? "Online verify" : "Resync",
Philipp Reisnerb411b362009-09-25 16:07:19 -0700870 dt + mdev->rs_paused, mdev->rs_paused, dbdt);
871
872 n_oos = drbd_bm_total_weight(mdev);
873
874 if (os.conn == C_VERIFY_S || os.conn == C_VERIFY_T) {
875 if (n_oos) {
876 dev_alert(DEV, "Online verify found %lu %dk block out of sync!\n",
877 n_oos, Bit2KB(1));
878 khelper_cmd = "out-of-sync";
879 }
880 } else {
881 D_ASSERT((n_oos - mdev->rs_failed) == 0);
882
883 if (os.conn == C_SYNC_TARGET || os.conn == C_PAUSED_SYNC_T)
884 khelper_cmd = "after-resync-target";
885
Lars Ellenbergf3990022011-03-23 14:31:09 +0100886 if (mdev->tconn->csums_tfm && mdev->rs_total) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700887 const unsigned long s = mdev->rs_same_csum;
888 const unsigned long t = mdev->rs_total;
889 const int ratio =
890 (t == 0) ? 0 :
891 (t < 100000) ? ((s*100)/t) : (s/(t/100));
Bart Van Assche24c48302011-05-21 18:32:29 +0200892 dev_info(DEV, "%u %% had equal checksums, eliminated: %luK; "
Philipp Reisnerb411b362009-09-25 16:07:19 -0700893 "transferred %luK total %luK\n",
894 ratio,
895 Bit2KB(mdev->rs_same_csum),
896 Bit2KB(mdev->rs_total - mdev->rs_same_csum),
897 Bit2KB(mdev->rs_total));
898 }
899 }
900
901 if (mdev->rs_failed) {
902 dev_info(DEV, " %lu failed blocks\n", mdev->rs_failed);
903
904 if (os.conn == C_SYNC_TARGET || os.conn == C_PAUSED_SYNC_T) {
905 ns.disk = D_INCONSISTENT;
906 ns.pdsk = D_UP_TO_DATE;
907 } else {
908 ns.disk = D_UP_TO_DATE;
909 ns.pdsk = D_INCONSISTENT;
910 }
911 } else {
912 ns.disk = D_UP_TO_DATE;
913 ns.pdsk = D_UP_TO_DATE;
914
915 if (os.conn == C_SYNC_TARGET || os.conn == C_PAUSED_SYNC_T) {
916 if (mdev->p_uuid) {
917 int i;
918 for (i = UI_BITMAP ; i <= UI_HISTORY_END ; i++)
919 _drbd_uuid_set(mdev, i, mdev->p_uuid[i]);
920 drbd_uuid_set(mdev, UI_BITMAP, mdev->ldev->md.uuid[UI_CURRENT]);
921 _drbd_uuid_set(mdev, UI_CURRENT, mdev->p_uuid[UI_CURRENT]);
922 } else {
923 dev_err(DEV, "mdev->p_uuid is NULL! BUG\n");
924 }
925 }
926
Lars Ellenberg62b0da32011-01-20 13:25:21 +0100927 if (!(os.conn == C_VERIFY_S || os.conn == C_VERIFY_T)) {
928 /* for verify runs, we don't update uuids here,
929 * so there would be nothing to report. */
930 drbd_uuid_set_bm(mdev, 0UL);
931 drbd_print_uuids(mdev, "updated UUIDs");
932 if (mdev->p_uuid) {
933 /* Now the two UUID sets are equal, update what we
934 * know of the peer. */
935 int i;
936 for (i = UI_CURRENT ; i <= UI_HISTORY_END ; i++)
937 mdev->p_uuid[i] = mdev->ldev->md.uuid[i];
938 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700939 }
940 }
941
942 _drbd_set_state(mdev, ns, CS_VERBOSE, NULL);
943out_unlock:
Philipp Reisner87eeee42011-01-19 14:16:30 +0100944 spin_unlock_irq(&mdev->tconn->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700945 put_ldev(mdev);
946out:
947 mdev->rs_total = 0;
948 mdev->rs_failed = 0;
949 mdev->rs_paused = 0;
Lars Ellenberg58ffa582012-07-26 14:09:49 +0200950
951 /* reset start sector, if we reached end of device */
952 if (verify_done && mdev->ov_left == 0)
Lars Ellenberg26525612010-11-05 09:56:33 +0100953 mdev->ov_start_sector = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700954
Lars Ellenberg13d42682010-10-13 17:37:54 +0200955 drbd_md_sync(mdev);
956
Philipp Reisnerb411b362009-09-25 16:07:19 -0700957 if (khelper_cmd)
958 drbd_khelper(mdev, khelper_cmd);
959
960 return 1;
961}
962
963/* helper */
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100964static void move_to_net_ee_or_free(struct drbd_conf *mdev, struct drbd_peer_request *peer_req)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700965{
Andreas Gruenbacher045417f2011-04-07 21:34:24 +0200966 if (drbd_peer_req_has_active_page(peer_req)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700967 /* This might happen if sendpage() has not finished */
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100968 int i = (peer_req->i.size + PAGE_SIZE -1) >> PAGE_SHIFT;
Lars Ellenberg435f0742010-09-06 12:30:25 +0200969 atomic_add(i, &mdev->pp_in_use_by_net);
970 atomic_sub(i, &mdev->pp_in_use);
Philipp Reisner87eeee42011-01-19 14:16:30 +0100971 spin_lock_irq(&mdev->tconn->req_lock);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100972 list_add_tail(&peer_req->w.list, &mdev->net_ee);
Philipp Reisner87eeee42011-01-19 14:16:30 +0100973 spin_unlock_irq(&mdev->tconn->req_lock);
Lars Ellenberg435f0742010-09-06 12:30:25 +0200974 wake_up(&drbd_pp_wait);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700975 } else
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +0200976 drbd_free_peer_req(mdev, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700977}
978
979/**
980 * w_e_end_data_req() - Worker callback, to send a P_DATA_REPLY packet in response to a P_DATA_REQUEST
981 * @mdev: DRBD device.
982 * @w: work object.
983 * @cancel: The connection will be closed anyways
984 */
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +0100985int w_e_end_data_req(struct drbd_work *w, int cancel)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700986{
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100987 struct drbd_peer_request *peer_req = container_of(w, struct drbd_peer_request, w);
Philipp Reisner00d56942011-02-09 18:09:48 +0100988 struct drbd_conf *mdev = w->mdev;
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +0100989 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700990
991 if (unlikely(cancel)) {
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +0200992 drbd_free_peer_req(mdev, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700993 dec_unacked(mdev);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +0100994 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700995 }
996
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100997 if (likely((peer_req->flags & EE_WAS_ERROR) == 0)) {
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +0100998 err = drbd_send_block(mdev, P_DATA_REPLY, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700999 } else {
1000 if (__ratelimit(&drbd_ratelimit_state))
1001 dev_err(DEV, "Sending NegDReply. sector=%llus.\n",
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001002 (unsigned long long)peer_req->i.sector);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001003
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001004 err = drbd_send_ack(mdev, P_NEG_DREPLY, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001005 }
1006
1007 dec_unacked(mdev);
1008
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001009 move_to_net_ee_or_free(mdev, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001010
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001011 if (unlikely(err))
Philipp Reisnerb411b362009-09-25 16:07:19 -07001012 dev_err(DEV, "drbd_send_block() failed\n");
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001013 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001014}
1015
1016/**
Andreas Gruenbachera209b4a2011-08-17 12:43:25 +02001017 * 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 -07001018 * @mdev: DRBD device.
1019 * @w: work object.
1020 * @cancel: The connection will be closed anyways
1021 */
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001022int w_e_end_rsdata_req(struct drbd_work *w, int cancel)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001023{
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001024 struct drbd_peer_request *peer_req = container_of(w, struct drbd_peer_request, w);
Philipp Reisner00d56942011-02-09 18:09:48 +01001025 struct drbd_conf *mdev = w->mdev;
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001026 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001027
1028 if (unlikely(cancel)) {
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +02001029 drbd_free_peer_req(mdev, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001030 dec_unacked(mdev);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001031 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001032 }
1033
1034 if (get_ldev_if_state(mdev, D_FAILED)) {
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001035 drbd_rs_complete_io(mdev, peer_req->i.sector);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001036 put_ldev(mdev);
1037 }
1038
Philipp Reisnerd612d302010-12-27 10:53:28 +01001039 if (mdev->state.conn == C_AHEAD) {
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001040 err = drbd_send_ack(mdev, P_RS_CANCEL, peer_req);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001041 } else if (likely((peer_req->flags & EE_WAS_ERROR) == 0)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001042 if (likely(mdev->state.pdsk >= D_INCONSISTENT)) {
1043 inc_rs_pending(mdev);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001044 err = drbd_send_block(mdev, P_RS_DATA_REPLY, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001045 } else {
1046 if (__ratelimit(&drbd_ratelimit_state))
1047 dev_err(DEV, "Not sending RSDataReply, "
1048 "partner DISKLESS!\n");
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001049 err = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001050 }
1051 } else {
1052 if (__ratelimit(&drbd_ratelimit_state))
1053 dev_err(DEV, "Sending NegRSDReply. sector %llus.\n",
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001054 (unsigned long long)peer_req->i.sector);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001055
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001056 err = drbd_send_ack(mdev, P_NEG_RS_DREPLY, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001057
1058 /* update resync data with failure */
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001059 drbd_rs_failed_io(mdev, peer_req->i.sector, peer_req->i.size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001060 }
1061
1062 dec_unacked(mdev);
1063
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001064 move_to_net_ee_or_free(mdev, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001065
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001066 if (unlikely(err))
Philipp Reisnerb411b362009-09-25 16:07:19 -07001067 dev_err(DEV, "drbd_send_block() failed\n");
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001068 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001069}
1070
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001071int w_e_end_csum_rs_req(struct drbd_work *w, int cancel)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001072{
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001073 struct drbd_peer_request *peer_req = container_of(w, struct drbd_peer_request, w);
Philipp Reisner00d56942011-02-09 18:09:48 +01001074 struct drbd_conf *mdev = w->mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001075 struct digest_info *di;
1076 int digest_size;
1077 void *digest = NULL;
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001078 int err, eq = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001079
1080 if (unlikely(cancel)) {
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +02001081 drbd_free_peer_req(mdev, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001082 dec_unacked(mdev);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001083 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001084 }
1085
Lars Ellenberg1d53f092010-09-05 01:13:24 +02001086 if (get_ldev(mdev)) {
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001087 drbd_rs_complete_io(mdev, peer_req->i.sector);
Lars Ellenberg1d53f092010-09-05 01:13:24 +02001088 put_ldev(mdev);
1089 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001090
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001091 di = peer_req->digest;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001092
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001093 if (likely((peer_req->flags & EE_WAS_ERROR) == 0)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001094 /* quick hack to try to avoid a race against reconfiguration.
1095 * a real fix would be much more involved,
1096 * introducing more locking mechanisms */
Lars Ellenbergf3990022011-03-23 14:31:09 +01001097 if (mdev->tconn->csums_tfm) {
1098 digest_size = crypto_hash_digestsize(mdev->tconn->csums_tfm);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001099 D_ASSERT(digest_size == di->digest_size);
1100 digest = kmalloc(digest_size, GFP_NOIO);
1101 }
1102 if (digest) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01001103 drbd_csum_ee(mdev, mdev->tconn->csums_tfm, peer_req, digest);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001104 eq = !memcmp(digest, di->digest, digest_size);
1105 kfree(digest);
1106 }
1107
1108 if (eq) {
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001109 drbd_set_in_sync(mdev, peer_req->i.sector, peer_req->i.size);
Lars Ellenberg676396d2010-03-03 02:08:22 +01001110 /* rs_same_csums unit is BM_BLOCK_SIZE */
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001111 mdev->rs_same_csum += peer_req->i.size >> BM_BLOCK_SHIFT;
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001112 err = drbd_send_ack(mdev, P_RS_IS_IN_SYNC, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001113 } else {
1114 inc_rs_pending(mdev);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001115 peer_req->block_id = ID_SYNCER; /* By setting block_id, digest pointer becomes invalid! */
1116 peer_req->flags &= ~EE_HAS_DIGEST; /* This peer request no longer has a digest pointer */
Philipp Reisner204bba92010-08-23 16:17:13 +02001117 kfree(di);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001118 err = drbd_send_block(mdev, P_RS_DATA_REPLY, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001119 }
1120 } else {
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001121 err = drbd_send_ack(mdev, P_NEG_RS_DREPLY, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001122 if (__ratelimit(&drbd_ratelimit_state))
1123 dev_err(DEV, "Sending NegDReply. I guess it gets messy.\n");
1124 }
1125
1126 dec_unacked(mdev);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001127 move_to_net_ee_or_free(mdev, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001128
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001129 if (unlikely(err))
Philipp Reisnerb411b362009-09-25 16:07:19 -07001130 dev_err(DEV, "drbd_send_block/ack() failed\n");
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001131 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001132}
1133
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001134int w_e_end_ov_req(struct drbd_work *w, int cancel)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001135{
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001136 struct drbd_peer_request *peer_req = container_of(w, struct drbd_peer_request, w);
Philipp Reisner00d56942011-02-09 18:09:48 +01001137 struct drbd_conf *mdev = w->mdev;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001138 sector_t sector = peer_req->i.sector;
1139 unsigned int size = peer_req->i.size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001140 int digest_size;
1141 void *digest;
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001142 int err = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001143
1144 if (unlikely(cancel))
1145 goto out;
1146
Lars Ellenbergf3990022011-03-23 14:31:09 +01001147 digest_size = crypto_hash_digestsize(mdev->tconn->verify_tfm);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001148 digest = kmalloc(digest_size, GFP_NOIO);
Philipp Reisner8f214202011-03-01 15:52:35 +01001149 if (!digest) {
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001150 err = 1; /* terminate the connection in case the allocation failed */
Philipp Reisner8f214202011-03-01 15:52:35 +01001151 goto out;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001152 }
1153
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001154 if (likely(!(peer_req->flags & EE_WAS_ERROR)))
Lars Ellenbergf3990022011-03-23 14:31:09 +01001155 drbd_csum_ee(mdev, mdev->tconn->verify_tfm, peer_req, digest);
Philipp Reisner8f214202011-03-01 15:52:35 +01001156 else
1157 memset(digest, 0, digest_size);
1158
Lars Ellenberg53ea4332011-03-08 17:11:40 +01001159 /* Free e and pages before send.
1160 * In case we block on congestion, we could otherwise run into
1161 * some distributed deadlock, if the other side blocks on
1162 * congestion as well, because our receiver blocks in
Andreas Gruenbacherc37c8ec2011-04-07 21:02:09 +02001163 * drbd_alloc_pages due to pp_in_use > max_buffers. */
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +02001164 drbd_free_peer_req(mdev, peer_req);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001165 peer_req = NULL;
Philipp Reisner8f214202011-03-01 15:52:35 +01001166 inc_rs_pending(mdev);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001167 err = drbd_send_drequest_csum(mdev, sector, size, digest, digest_size, P_OV_REPLY);
1168 if (err)
Philipp Reisner8f214202011-03-01 15:52:35 +01001169 dec_rs_pending(mdev);
1170 kfree(digest);
1171
Philipp Reisnerb411b362009-09-25 16:07:19 -07001172out:
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001173 if (peer_req)
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +02001174 drbd_free_peer_req(mdev, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001175 dec_unacked(mdev);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001176 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001177}
1178
Andreas Gruenbacher8f7bed72010-12-19 23:53:14 +01001179void drbd_ov_out_of_sync_found(struct drbd_conf *mdev, sector_t sector, int size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001180{
1181 if (mdev->ov_last_oos_start + mdev->ov_last_oos_size == sector) {
1182 mdev->ov_last_oos_size += size>>9;
1183 } else {
1184 mdev->ov_last_oos_start = sector;
1185 mdev->ov_last_oos_size = size>>9;
1186 }
1187 drbd_set_out_of_sync(mdev, sector, size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001188}
1189
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001190int w_e_end_ov_reply(struct drbd_work *w, int cancel)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001191{
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001192 struct drbd_peer_request *peer_req = container_of(w, struct drbd_peer_request, w);
Philipp Reisner00d56942011-02-09 18:09:48 +01001193 struct drbd_conf *mdev = w->mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001194 struct digest_info *di;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001195 void *digest;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001196 sector_t sector = peer_req->i.sector;
1197 unsigned int size = peer_req->i.size;
Lars Ellenberg53ea4332011-03-08 17:11:40 +01001198 int digest_size;
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001199 int err, eq = 0;
Lars Ellenberg58ffa582012-07-26 14:09:49 +02001200 bool stop_sector_reached = false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001201
1202 if (unlikely(cancel)) {
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +02001203 drbd_free_peer_req(mdev, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001204 dec_unacked(mdev);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001205 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001206 }
1207
1208 /* after "cancel", because after drbd_disconnect/drbd_rs_cancel_all
1209 * the resync lru has been cleaned up already */
Lars Ellenberg1d53f092010-09-05 01:13:24 +02001210 if (get_ldev(mdev)) {
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001211 drbd_rs_complete_io(mdev, peer_req->i.sector);
Lars Ellenberg1d53f092010-09-05 01:13:24 +02001212 put_ldev(mdev);
1213 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001214
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001215 di = peer_req->digest;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001216
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001217 if (likely((peer_req->flags & EE_WAS_ERROR) == 0)) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01001218 digest_size = crypto_hash_digestsize(mdev->tconn->verify_tfm);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001219 digest = kmalloc(digest_size, GFP_NOIO);
1220 if (digest) {
Lars Ellenbergf3990022011-03-23 14:31:09 +01001221 drbd_csum_ee(mdev, mdev->tconn->verify_tfm, peer_req, digest);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001222
1223 D_ASSERT(digest_size == di->digest_size);
1224 eq = !memcmp(digest, di->digest, digest_size);
1225 kfree(digest);
1226 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001227 }
1228
Lars Ellenberg9676c762011-02-22 14:02:31 +01001229 /* Free peer_req and pages before send.
1230 * In case we block on congestion, we could otherwise run into
1231 * some distributed deadlock, if the other side blocks on
1232 * congestion as well, because our receiver blocks in
Andreas Gruenbacherc37c8ec2011-04-07 21:02:09 +02001233 * drbd_alloc_pages due to pp_in_use > max_buffers. */
Andreas Gruenbacher3967deb2011-04-06 16:16:56 +02001234 drbd_free_peer_req(mdev, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001235 if (!eq)
Andreas Gruenbacher8f7bed72010-12-19 23:53:14 +01001236 drbd_ov_out_of_sync_found(mdev, sector, size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001237 else
Andreas Gruenbacher8f7bed72010-12-19 23:53:14 +01001238 ov_out_of_sync_print(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001239
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001240 err = drbd_send_ack_ex(mdev, P_OV_RESULT, sector, size,
Andreas Gruenbacherfa79abd2011-03-16 01:31:39 +01001241 eq ? ID_IN_SYNC : ID_OUT_OF_SYNC);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001242
Lars Ellenberg53ea4332011-03-08 17:11:40 +01001243 dec_unacked(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001244
Lars Ellenbergea5442a2010-11-05 09:48:01 +01001245 --mdev->ov_left;
1246
1247 /* let's advance progress step marks only for every other megabyte */
1248 if ((mdev->ov_left & 0x200) == 0x200)
1249 drbd_advance_rs_marks(mdev, mdev->ov_left);
1250
Lars Ellenberg58ffa582012-07-26 14:09:49 +02001251 stop_sector_reached = verify_can_do_stop_sector(mdev) &&
1252 (sector + (size>>9)) >= mdev->ov_stop_sector;
1253
1254 if (mdev->ov_left == 0 || stop_sector_reached) {
Andreas Gruenbacher8f7bed72010-12-19 23:53:14 +01001255 ov_out_of_sync_print(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001256 drbd_resync_finished(mdev);
1257 }
1258
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001259 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001260}
1261
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001262int w_prev_work_done(struct drbd_work *w, int cancel)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001263{
1264 struct drbd_wq_barrier *b = container_of(w, struct drbd_wq_barrier, w);
Philipp Reisner00d56942011-02-09 18:09:48 +01001265
Philipp Reisnerb411b362009-09-25 16:07:19 -07001266 complete(&b->done);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001267 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001268}
1269
Lars Ellenbergb6dd1a82011-11-28 15:04:49 +01001270/* FIXME
1271 * We need to track the number of pending barrier acks,
1272 * and to be able to wait for them.
1273 * See also comment in drbd_adm_attach before drbd_suspend_io.
1274 */
1275int drbd_send_barrier(struct drbd_tconn *tconn)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001276{
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02001277 struct p_barrier *p;
Lars Ellenbergb6dd1a82011-11-28 15:04:49 +01001278 struct drbd_socket *sock;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001279
Lars Ellenbergb6dd1a82011-11-28 15:04:49 +01001280 sock = &tconn->data;
1281 p = conn_prepare_command(tconn, sock);
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02001282 if (!p)
1283 return -EIO;
Lars Ellenbergb6dd1a82011-11-28 15:04:49 +01001284 p->barrier = tconn->send.current_epoch_nr;
1285 p->pad = 0;
1286 tconn->send.current_epoch_writes = 0;
1287
1288 return conn_send_command(tconn, sock, P_BARRIER, sizeof(*p), NULL, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001289}
1290
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001291int w_send_write_hint(struct drbd_work *w, int cancel)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001292{
Philipp Reisner00d56942011-02-09 18:09:48 +01001293 struct drbd_conf *mdev = w->mdev;
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02001294 struct drbd_socket *sock;
1295
Philipp Reisnerb411b362009-09-25 16:07:19 -07001296 if (cancel)
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001297 return 0;
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02001298 sock = &mdev->tconn->data;
1299 if (!drbd_prepare_command(mdev, sock))
1300 return -EIO;
Andreas Gruenbachere6589832011-03-30 12:54:42 +02001301 return drbd_send_command(mdev, sock, P_UNPLUG_REMOTE, 0, NULL, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001302}
1303
Lars Ellenberg4eb9b3c2012-08-20 11:05:23 +02001304static void re_init_if_first_write(struct drbd_tconn *tconn, unsigned int epoch)
1305{
1306 if (!tconn->send.seen_any_write_yet) {
1307 tconn->send.seen_any_write_yet = true;
1308 tconn->send.current_epoch_nr = epoch;
1309 tconn->send.current_epoch_writes = 0;
1310 }
1311}
1312
1313static void maybe_send_barrier(struct drbd_tconn *tconn, unsigned int epoch)
1314{
1315 /* re-init if first write on this connection */
1316 if (!tconn->send.seen_any_write_yet)
1317 return;
1318 if (tconn->send.current_epoch_nr != epoch) {
1319 if (tconn->send.current_epoch_writes)
1320 drbd_send_barrier(tconn);
1321 tconn->send.current_epoch_nr = epoch;
1322 }
1323}
1324
Andreas Gruenbacher8f7bed72010-12-19 23:53:14 +01001325int w_send_out_of_sync(struct drbd_work *w, int cancel)
Philipp Reisner73a01a12010-10-27 14:33:00 +02001326{
1327 struct drbd_request *req = container_of(w, struct drbd_request, w);
Philipp Reisner00d56942011-02-09 18:09:48 +01001328 struct drbd_conf *mdev = w->mdev;
Lars Ellenbergb6dd1a82011-11-28 15:04:49 +01001329 struct drbd_tconn *tconn = mdev->tconn;
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001330 int err;
Philipp Reisner73a01a12010-10-27 14:33:00 +02001331
1332 if (unlikely(cancel)) {
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01001333 req_mod(req, SEND_CANCELED);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001334 return 0;
Philipp Reisner73a01a12010-10-27 14:33:00 +02001335 }
1336
Lars Ellenbergb6dd1a82011-11-28 15:04:49 +01001337 /* this time, no tconn->send.current_epoch_writes++;
1338 * If it was sent, it was the closing barrier for the last
1339 * replicated epoch, before we went into AHEAD mode.
1340 * No more barriers will be sent, until we leave AHEAD mode again. */
Lars Ellenberg4eb9b3c2012-08-20 11:05:23 +02001341 maybe_send_barrier(tconn, req->epoch);
Lars Ellenbergb6dd1a82011-11-28 15:04:49 +01001342
Andreas Gruenbacher8f7bed72010-12-19 23:53:14 +01001343 err = drbd_send_out_of_sync(mdev, req);
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01001344 req_mod(req, OOS_HANDED_TO_NETWORK);
Philipp Reisner73a01a12010-10-27 14:33:00 +02001345
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001346 return err;
Philipp Reisner73a01a12010-10-27 14:33:00 +02001347}
1348
Philipp Reisnerb411b362009-09-25 16:07:19 -07001349/**
1350 * w_send_dblock() - Worker callback to send a P_DATA packet in order to mirror a write request
1351 * @mdev: DRBD device.
1352 * @w: work object.
1353 * @cancel: The connection will be closed anyways
1354 */
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001355int w_send_dblock(struct drbd_work *w, int cancel)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001356{
1357 struct drbd_request *req = container_of(w, struct drbd_request, w);
Philipp Reisner00d56942011-02-09 18:09:48 +01001358 struct drbd_conf *mdev = w->mdev;
Lars Ellenbergb6dd1a82011-11-28 15:04:49 +01001359 struct drbd_tconn *tconn = mdev->tconn;
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001360 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001361
1362 if (unlikely(cancel)) {
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01001363 req_mod(req, SEND_CANCELED);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001364 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001365 }
1366
Lars Ellenberg4eb9b3c2012-08-20 11:05:23 +02001367 re_init_if_first_write(tconn, req->epoch);
1368 maybe_send_barrier(tconn, req->epoch);
Lars Ellenbergb6dd1a82011-11-28 15:04:49 +01001369 tconn->send.current_epoch_writes++;
1370
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001371 err = drbd_send_dblock(mdev, req);
1372 req_mod(req, err ? SEND_FAILED : HANDED_OVER_TO_NETWORK);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001373
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001374 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001375}
1376
1377/**
1378 * w_send_read_req() - Worker callback to send a read request (P_DATA_REQUEST) packet
1379 * @mdev: DRBD device.
1380 * @w: work object.
1381 * @cancel: The connection will be closed anyways
1382 */
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001383int w_send_read_req(struct drbd_work *w, int cancel)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001384{
1385 struct drbd_request *req = container_of(w, struct drbd_request, w);
Philipp Reisner00d56942011-02-09 18:09:48 +01001386 struct drbd_conf *mdev = w->mdev;
Lars Ellenbergb6dd1a82011-11-28 15:04:49 +01001387 struct drbd_tconn *tconn = mdev->tconn;
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001388 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001389
1390 if (unlikely(cancel)) {
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01001391 req_mod(req, SEND_CANCELED);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001392 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001393 }
1394
Lars Ellenbergb6dd1a82011-11-28 15:04:49 +01001395 /* Even read requests may close a write epoch,
1396 * if there was any yet. */
Lars Ellenberg4eb9b3c2012-08-20 11:05:23 +02001397 maybe_send_barrier(tconn, req->epoch);
Lars Ellenbergb6dd1a82011-11-28 15:04:49 +01001398
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001399 err = drbd_send_drequest(mdev, P_DATA_REQUEST, req->i.sector, req->i.size,
Andreas Gruenbacher6c1005e2011-03-16 01:34:24 +01001400 (unsigned long)req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001401
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001402 req_mod(req, err ? SEND_FAILED : HANDED_OVER_TO_NETWORK);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001403
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001404 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001405}
1406
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001407int w_restart_disk_io(struct drbd_work *w, int cancel)
Philipp Reisner265be2d2010-05-31 10:14:17 +02001408{
1409 struct drbd_request *req = container_of(w, struct drbd_request, w);
Philipp Reisner00d56942011-02-09 18:09:48 +01001410 struct drbd_conf *mdev = w->mdev;
Philipp Reisner265be2d2010-05-31 10:14:17 +02001411
Philipp Reisner07782862010-08-31 12:00:50 +02001412 if (bio_data_dir(req->master_bio) == WRITE && req->rq_state & RQ_IN_ACT_LOG)
Lars Ellenberg181286a2011-03-31 15:18:56 +02001413 drbd_al_begin_io(mdev, &req->i);
Philipp Reisner265be2d2010-05-31 10:14:17 +02001414
1415 drbd_req_make_private_bio(req, req->master_bio);
1416 req->private_bio->bi_bdev = mdev->ldev->backing_bdev;
1417 generic_make_request(req->private_bio);
1418
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001419 return 0;
Philipp Reisner265be2d2010-05-31 10:14:17 +02001420}
1421
Philipp Reisnerb411b362009-09-25 16:07:19 -07001422static int _drbd_may_sync_now(struct drbd_conf *mdev)
1423{
1424 struct drbd_conf *odev = mdev;
Andreas Gruenbacher95f8efd2011-05-12 11:15:34 +02001425 int resync_after;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001426
1427 while (1) {
Philipp Reisner438c8372011-03-28 14:48:01 +02001428 if (!odev->ldev)
1429 return 1;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001430 rcu_read_lock();
Andreas Gruenbacher95f8efd2011-05-12 11:15:34 +02001431 resync_after = rcu_dereference(odev->ldev->disk_conf)->resync_after;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001432 rcu_read_unlock();
Andreas Gruenbacher95f8efd2011-05-12 11:15:34 +02001433 if (resync_after == -1)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001434 return 1;
Andreas Gruenbacher95f8efd2011-05-12 11:15:34 +02001435 odev = minor_to_mdev(resync_after);
Andreas Gruenbacher841ce242010-12-15 19:31:20 +01001436 if (!expect(odev))
1437 return 1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001438 if ((odev->state.conn >= C_SYNC_SOURCE &&
1439 odev->state.conn <= C_PAUSED_SYNC_T) ||
1440 odev->state.aftr_isp || odev->state.peer_isp ||
1441 odev->state.user_isp)
1442 return 0;
1443 }
1444}
1445
1446/**
1447 * _drbd_pause_after() - Pause resync on all devices that may not resync now
1448 * @mdev: DRBD device.
1449 *
1450 * Called from process context only (admin command and after_state_ch).
1451 */
1452static int _drbd_pause_after(struct drbd_conf *mdev)
1453{
1454 struct drbd_conf *odev;
1455 int i, rv = 0;
1456
Philipp Reisner695d08f2011-04-11 22:53:32 -07001457 rcu_read_lock();
Philipp Reisner81a5d602011-02-22 19:53:16 -05001458 idr_for_each_entry(&minors, odev, i) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001459 if (odev->state.conn == C_STANDALONE && odev->state.disk == D_DISKLESS)
1460 continue;
1461 if (!_drbd_may_sync_now(odev))
1462 rv |= (__drbd_set_state(_NS(odev, aftr_isp, 1), CS_HARD, NULL)
1463 != SS_NOTHING_TO_DO);
1464 }
Philipp Reisner695d08f2011-04-11 22:53:32 -07001465 rcu_read_unlock();
Philipp Reisnerb411b362009-09-25 16:07:19 -07001466
1467 return rv;
1468}
1469
1470/**
1471 * _drbd_resume_next() - Resume resync on all devices that may resync now
1472 * @mdev: DRBD device.
1473 *
1474 * Called from process context only (admin command and worker).
1475 */
1476static int _drbd_resume_next(struct drbd_conf *mdev)
1477{
1478 struct drbd_conf *odev;
1479 int i, rv = 0;
1480
Philipp Reisner695d08f2011-04-11 22:53:32 -07001481 rcu_read_lock();
Philipp Reisner81a5d602011-02-22 19:53:16 -05001482 idr_for_each_entry(&minors, odev, i) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001483 if (odev->state.conn == C_STANDALONE && odev->state.disk == D_DISKLESS)
1484 continue;
1485 if (odev->state.aftr_isp) {
1486 if (_drbd_may_sync_now(odev))
1487 rv |= (__drbd_set_state(_NS(odev, aftr_isp, 0),
1488 CS_HARD, NULL)
1489 != SS_NOTHING_TO_DO) ;
1490 }
1491 }
Philipp Reisner695d08f2011-04-11 22:53:32 -07001492 rcu_read_unlock();
Philipp Reisnerb411b362009-09-25 16:07:19 -07001493 return rv;
1494}
1495
1496void resume_next_sg(struct drbd_conf *mdev)
1497{
1498 write_lock_irq(&global_state_lock);
1499 _drbd_resume_next(mdev);
1500 write_unlock_irq(&global_state_lock);
1501}
1502
1503void suspend_other_sg(struct drbd_conf *mdev)
1504{
1505 write_lock_irq(&global_state_lock);
1506 _drbd_pause_after(mdev);
1507 write_unlock_irq(&global_state_lock);
1508}
1509
Philipp Reisnerdc97b702011-05-03 14:27:15 +02001510/* caller must hold global_state_lock */
Andreas Gruenbacher95f8efd2011-05-12 11:15:34 +02001511enum drbd_ret_code drbd_resync_after_valid(struct drbd_conf *mdev, int o_minor)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001512{
1513 struct drbd_conf *odev;
Andreas Gruenbacher95f8efd2011-05-12 11:15:34 +02001514 int resync_after;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001515
1516 if (o_minor == -1)
1517 return NO_ERROR;
1518 if (o_minor < -1 || minor_to_mdev(o_minor) == NULL)
Andreas Gruenbacher95f8efd2011-05-12 11:15:34 +02001519 return ERR_RESYNC_AFTER;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001520
1521 /* check for loops */
1522 odev = minor_to_mdev(o_minor);
1523 while (1) {
1524 if (odev == mdev)
Andreas Gruenbacher95f8efd2011-05-12 11:15:34 +02001525 return ERR_RESYNC_AFTER_CYCLE;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001526
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001527 rcu_read_lock();
Andreas Gruenbacher95f8efd2011-05-12 11:15:34 +02001528 resync_after = rcu_dereference(odev->ldev->disk_conf)->resync_after;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001529 rcu_read_unlock();
Philipp Reisnerb411b362009-09-25 16:07:19 -07001530 /* dependency chain ends here, no cycles. */
Andreas Gruenbacher95f8efd2011-05-12 11:15:34 +02001531 if (resync_after == -1)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001532 return NO_ERROR;
1533
1534 /* follow the dependency chain */
Andreas Gruenbacher95f8efd2011-05-12 11:15:34 +02001535 odev = minor_to_mdev(resync_after);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001536 }
1537}
1538
Philipp Reisnerdc97b702011-05-03 14:27:15 +02001539/* caller must hold global_state_lock */
Andreas Gruenbacher95f8efd2011-05-12 11:15:34 +02001540void drbd_resync_after_changed(struct drbd_conf *mdev)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001541{
1542 int changes;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001543
Philipp Reisnerdc97b702011-05-03 14:27:15 +02001544 do {
1545 changes = _drbd_pause_after(mdev);
1546 changes |= _drbd_resume_next(mdev);
1547 } while (changes);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001548}
1549
Lars Ellenberg9bd28d32010-11-05 09:55:18 +01001550void drbd_rs_controller_reset(struct drbd_conf *mdev)
1551{
Philipp Reisner813472c2011-05-03 16:47:02 +02001552 struct fifo_buffer *plan;
1553
Lars Ellenberg9bd28d32010-11-05 09:55:18 +01001554 atomic_set(&mdev->rs_sect_in, 0);
1555 atomic_set(&mdev->rs_sect_ev, 0);
1556 mdev->rs_in_flight = 0;
Philipp Reisner813472c2011-05-03 16:47:02 +02001557
1558 /* Updating the RCU protected object in place is necessary since
1559 this function gets called from atomic context.
1560 It is valid since all other updates also lead to an completely
1561 empty fifo */
1562 rcu_read_lock();
1563 plan = rcu_dereference(mdev->rs_plan_s);
1564 plan->total = 0;
1565 fifo_set(plan, 0);
1566 rcu_read_unlock();
Lars Ellenberg9bd28d32010-11-05 09:55:18 +01001567}
1568
Philipp Reisner1f04af32011-02-07 11:33:59 +01001569void start_resync_timer_fn(unsigned long data)
1570{
1571 struct drbd_conf *mdev = (struct drbd_conf *) data;
1572
Lars Ellenbergd5b27b02011-11-14 15:42:37 +01001573 drbd_queue_work(&mdev->tconn->sender_work, &mdev->start_resync_work);
Philipp Reisner1f04af32011-02-07 11:33:59 +01001574}
1575
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001576int w_start_resync(struct drbd_work *w, int cancel)
Philipp Reisner1f04af32011-02-07 11:33:59 +01001577{
Philipp Reisner00d56942011-02-09 18:09:48 +01001578 struct drbd_conf *mdev = w->mdev;
1579
Philipp Reisner1f04af32011-02-07 11:33:59 +01001580 if (atomic_read(&mdev->unacked_cnt) || atomic_read(&mdev->rs_pending_cnt)) {
1581 dev_warn(DEV, "w_start_resync later...\n");
1582 mdev->start_resync_timer.expires = jiffies + HZ/10;
1583 add_timer(&mdev->start_resync_timer);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001584 return 0;
Philipp Reisner1f04af32011-02-07 11:33:59 +01001585 }
1586
1587 drbd_start_resync(mdev, C_SYNC_SOURCE);
Philipp Reisner36baf612011-11-10 14:27:34 +01001588 clear_bit(AHEAD_TO_SYNC_SOURCE, &mdev->flags);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001589 return 0;
Philipp Reisner1f04af32011-02-07 11:33:59 +01001590}
1591
Philipp Reisnerb411b362009-09-25 16:07:19 -07001592/**
1593 * drbd_start_resync() - Start the resync process
1594 * @mdev: DRBD device.
1595 * @side: Either C_SYNC_SOURCE or C_SYNC_TARGET
1596 *
1597 * This function might bring you directly into one of the
1598 * C_PAUSED_SYNC_* states.
1599 */
1600void drbd_start_resync(struct drbd_conf *mdev, enum drbd_conns side)
1601{
1602 union drbd_state ns;
1603 int r;
1604
Philipp Reisnerc4752ef2010-10-27 17:32:36 +02001605 if (mdev->state.conn >= C_SYNC_SOURCE && mdev->state.conn < C_AHEAD) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001606 dev_err(DEV, "Resync already running!\n");
1607 return;
1608 }
1609
Philipp Reisnere64a3292011-02-05 17:34:11 +01001610 if (!test_bit(B_RS_H_DONE, &mdev->flags)) {
1611 if (side == C_SYNC_TARGET) {
1612 /* Since application IO was locked out during C_WF_BITMAP_T and
1613 C_WF_SYNC_UUID we are still unmodified. Before going to C_SYNC_TARGET
1614 we check that we might make the data inconsistent. */
1615 r = drbd_khelper(mdev, "before-resync-target");
1616 r = (r >> 8) & 0xff;
1617 if (r > 0) {
1618 dev_info(DEV, "before-resync-target handler returned %d, "
Philipp Reisner09b9e792010-12-03 16:04:24 +01001619 "dropping connection.\n", r);
Philipp Reisner38fa9982011-03-15 18:24:49 +01001620 conn_request_state(mdev->tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Philipp Reisner09b9e792010-12-03 16:04:24 +01001621 return;
1622 }
Philipp Reisnere64a3292011-02-05 17:34:11 +01001623 } else /* C_SYNC_SOURCE */ {
1624 r = drbd_khelper(mdev, "before-resync-source");
1625 r = (r >> 8) & 0xff;
1626 if (r > 0) {
1627 if (r == 3) {
1628 dev_info(DEV, "before-resync-source handler returned %d, "
1629 "ignoring. Old userland tools?", r);
1630 } else {
1631 dev_info(DEV, "before-resync-source handler returned %d, "
1632 "dropping connection.\n", r);
Philipp Reisner38fa9982011-03-15 18:24:49 +01001633 conn_request_state(mdev->tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Philipp Reisnere64a3292011-02-05 17:34:11 +01001634 return;
1635 }
1636 }
Philipp Reisner09b9e792010-12-03 16:04:24 +01001637 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001638 }
1639
Philipp Reisnere64a3292011-02-05 17:34:11 +01001640 if (current == mdev->tconn->worker.task) {
Philipp Reisnerdad20552011-02-11 19:43:55 +01001641 /* The worker should not sleep waiting for state_mutex,
Philipp Reisnere64a3292011-02-05 17:34:11 +01001642 that can take long */
Philipp Reisner8410da8f02011-02-11 20:11:10 +01001643 if (!mutex_trylock(mdev->state_mutex)) {
Philipp Reisnere64a3292011-02-05 17:34:11 +01001644 set_bit(B_RS_H_DONE, &mdev->flags);
1645 mdev->start_resync_timer.expires = jiffies + HZ/5;
1646 add_timer(&mdev->start_resync_timer);
1647 return;
1648 }
1649 } else {
Philipp Reisner8410da8f02011-02-11 20:11:10 +01001650 mutex_lock(mdev->state_mutex);
Philipp Reisnere64a3292011-02-05 17:34:11 +01001651 }
1652 clear_bit(B_RS_H_DONE, &mdev->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001653
Philipp Reisner0cfac5d2011-11-10 12:12:52 +01001654 write_lock_irq(&global_state_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001655 if (!get_ldev_if_state(mdev, D_NEGOTIATING)) {
Philipp Reisner0cfac5d2011-11-10 12:12:52 +01001656 write_unlock_irq(&global_state_lock);
Philipp Reisner8410da8f02011-02-11 20:11:10 +01001657 mutex_unlock(mdev->state_mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001658 return;
1659 }
1660
Philipp Reisner78bae592011-03-28 15:40:12 +02001661 ns = drbd_read_state(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001662
1663 ns.aftr_isp = !_drbd_may_sync_now(mdev);
1664
1665 ns.conn = side;
1666
1667 if (side == C_SYNC_TARGET)
1668 ns.disk = D_INCONSISTENT;
1669 else /* side == C_SYNC_SOURCE */
1670 ns.pdsk = D_INCONSISTENT;
1671
1672 r = __drbd_set_state(mdev, ns, CS_VERBOSE, NULL);
Philipp Reisner78bae592011-03-28 15:40:12 +02001673 ns = drbd_read_state(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001674
1675 if (ns.conn < C_CONNECTED)
1676 r = SS_UNKNOWN_ERROR;
1677
1678 if (r == SS_SUCCESS) {
Lars Ellenberg1d7734a2010-08-11 21:21:50 +02001679 unsigned long tw = drbd_bm_total_weight(mdev);
1680 unsigned long now = jiffies;
1681 int i;
1682
Philipp Reisnerb411b362009-09-25 16:07:19 -07001683 mdev->rs_failed = 0;
1684 mdev->rs_paused = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001685 mdev->rs_same_csum = 0;
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02001686 mdev->rs_last_events = 0;
1687 mdev->rs_last_sect_ev = 0;
Lars Ellenberg1d7734a2010-08-11 21:21:50 +02001688 mdev->rs_total = tw;
1689 mdev->rs_start = now;
1690 for (i = 0; i < DRBD_SYNC_MARKS; i++) {
1691 mdev->rs_mark_left[i] = tw;
1692 mdev->rs_mark_time[i] = now;
1693 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001694 _drbd_pause_after(mdev);
1695 }
1696 write_unlock_irq(&global_state_lock);
Lars Ellenberg5a22db82010-12-17 21:14:23 +01001697
Philipp Reisnerb411b362009-09-25 16:07:19 -07001698 if (r == SS_SUCCESS) {
Philipp Reisner328e0f12012-10-19 14:37:47 +02001699 /* reset rs_last_bcast when a resync or verify is started,
1700 * to deal with potential jiffies wrap. */
1701 mdev->rs_last_bcast = jiffies - HZ;
1702
Philipp Reisnerb411b362009-09-25 16:07:19 -07001703 dev_info(DEV, "Began resync as %s (will sync %lu KB [%lu bits set]).\n",
1704 drbd_conn_str(ns.conn),
1705 (unsigned long) mdev->rs_total << (BM_BLOCK_SHIFT-10),
1706 (unsigned long) mdev->rs_total);
Lars Ellenberg6c922ed2011-01-12 11:51:13 +01001707 if (side == C_SYNC_TARGET)
1708 mdev->bm_resync_fo = 0;
1709
1710 /* Since protocol 96, we must serialize drbd_gen_and_send_sync_uuid
1711 * with w_send_oos, or the sync target will get confused as to
1712 * how much bits to resync. We cannot do that always, because for an
1713 * empty resync and protocol < 95, we need to do it here, as we call
1714 * drbd_resync_finished from here in that case.
1715 * We drbd_gen_and_send_sync_uuid here for protocol < 96,
1716 * and from after_state_ch otherwise. */
Philipp Reisner31890f42011-01-19 14:12:51 +01001717 if (side == C_SYNC_SOURCE && mdev->tconn->agreed_pro_version < 96)
Lars Ellenberg6c922ed2011-01-12 11:51:13 +01001718 drbd_gen_and_send_sync_uuid(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001719
Philipp Reisner31890f42011-01-19 14:12:51 +01001720 if (mdev->tconn->agreed_pro_version < 95 && mdev->rs_total == 0) {
Lars Ellenbergaf85e8e2010-10-07 16:07:55 +02001721 /* This still has a race (about when exactly the peers
1722 * detect connection loss) that can lead to a full sync
1723 * on next handshake. In 8.3.9 we fixed this with explicit
1724 * resync-finished notifications, but the fix
1725 * introduces a protocol change. Sleeping for some
1726 * time longer than the ping interval + timeout on the
1727 * SyncSource, to give the SyncTarget the chance to
1728 * detect connection loss, then waiting for a ping
1729 * response (implicit in drbd_resync_finished) reduces
1730 * the race considerably, but does not solve it. */
Philipp Reisner44ed1672011-04-19 17:10:19 +02001731 if (side == C_SYNC_SOURCE) {
1732 struct net_conf *nc;
1733 int timeo;
1734
1735 rcu_read_lock();
1736 nc = rcu_dereference(mdev->tconn->net_conf);
1737 timeo = nc->ping_int * HZ + nc->ping_timeo * HZ / 9;
1738 rcu_read_unlock();
1739 schedule_timeout_interruptible(timeo);
1740 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001741 drbd_resync_finished(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001742 }
1743
Lars Ellenberg9bd28d32010-11-05 09:55:18 +01001744 drbd_rs_controller_reset(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001745 /* ns.conn may already be != mdev->state.conn,
1746 * we may have been paused in between, or become paused until
1747 * the timer triggers.
1748 * No matter, that is handled in resync_timer_fn() */
1749 if (ns.conn == C_SYNC_TARGET)
1750 mod_timer(&mdev->resync_timer, jiffies);
1751
1752 drbd_md_sync(mdev);
1753 }
Lars Ellenberg5a22db82010-12-17 21:14:23 +01001754 put_ldev(mdev);
Philipp Reisner8410da8f02011-02-11 20:11:10 +01001755 mutex_unlock(mdev->state_mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001756}
1757
Lars Ellenbergb6dd1a82011-11-28 15:04:49 +01001758/* If the resource already closed the current epoch, but we did not
1759 * (because we have not yet seen new requests), we should send the
1760 * corresponding barrier now. Must be checked within the same spinlock
1761 * that is used to check for new requests. */
1762bool need_to_send_barrier(struct drbd_tconn *connection)
1763{
1764 if (!connection->send.seen_any_write_yet)
1765 return false;
1766
1767 /* Skip barriers that do not contain any writes.
1768 * This may happen during AHEAD mode. */
1769 if (!connection->send.current_epoch_writes)
1770 return false;
1771
1772 /* ->req_lock is held when requests are queued on
1773 * connection->sender_work, and put into ->transfer_log.
1774 * It is also held when ->current_tle_nr is increased.
1775 * So either there are already new requests queued,
1776 * and corresponding barriers will be send there.
1777 * Or nothing new is queued yet, so the difference will be 1.
1778 */
1779 if (atomic_read(&connection->current_tle_nr) !=
1780 connection->send.current_epoch_nr + 1)
1781 return false;
1782
1783 return true;
1784}
1785
Lars Ellenberg8c0785a2011-10-19 11:50:57 +02001786bool dequeue_work_batch(struct drbd_work_queue *queue, struct list_head *work_list)
1787{
1788 spin_lock_irq(&queue->q_lock);
1789 list_splice_init(&queue->q, work_list);
1790 spin_unlock_irq(&queue->q_lock);
1791 return !list_empty(work_list);
1792}
1793
1794bool dequeue_work_item(struct drbd_work_queue *queue, struct list_head *work_list)
1795{
1796 spin_lock_irq(&queue->q_lock);
1797 if (!list_empty(&queue->q))
1798 list_move(queue->q.next, work_list);
1799 spin_unlock_irq(&queue->q_lock);
1800 return !list_empty(work_list);
1801}
1802
Lars Ellenbergb6dd1a82011-11-28 15:04:49 +01001803void wait_for_work(struct drbd_tconn *connection, struct list_head *work_list)
1804{
1805 DEFINE_WAIT(wait);
1806 struct net_conf *nc;
1807 int uncork, cork;
1808
1809 dequeue_work_item(&connection->sender_work, work_list);
1810 if (!list_empty(work_list))
1811 return;
1812
1813 /* Still nothing to do?
1814 * Maybe we still need to close the current epoch,
1815 * even if no new requests are queued yet.
1816 *
1817 * Also, poke TCP, just in case.
1818 * Then wait for new work (or signal). */
1819 rcu_read_lock();
1820 nc = rcu_dereference(connection->net_conf);
1821 uncork = nc ? nc->tcp_cork : 0;
1822 rcu_read_unlock();
1823 if (uncork) {
1824 mutex_lock(&connection->data.mutex);
1825 if (connection->data.socket)
1826 drbd_tcp_uncork(connection->data.socket);
1827 mutex_unlock(&connection->data.mutex);
1828 }
1829
1830 for (;;) {
1831 int send_barrier;
1832 prepare_to_wait(&connection->sender_work.q_wait, &wait, TASK_INTERRUPTIBLE);
1833 spin_lock_irq(&connection->req_lock);
1834 spin_lock(&connection->sender_work.q_lock); /* FIXME get rid of this one? */
Lars Ellenbergbc317a92012-08-22 11:47:14 +02001835 /* dequeue single item only,
1836 * we still use drbd_queue_work_front() in some places */
1837 if (!list_empty(&connection->sender_work.q))
1838 list_move(connection->sender_work.q.next, work_list);
Lars Ellenbergb6dd1a82011-11-28 15:04:49 +01001839 spin_unlock(&connection->sender_work.q_lock); /* FIXME get rid of this one? */
1840 if (!list_empty(work_list) || signal_pending(current)) {
1841 spin_unlock_irq(&connection->req_lock);
1842 break;
1843 }
1844 send_barrier = need_to_send_barrier(connection);
1845 spin_unlock_irq(&connection->req_lock);
1846 if (send_barrier) {
1847 drbd_send_barrier(connection);
1848 connection->send.current_epoch_nr++;
1849 }
1850 schedule();
1851 /* may be woken up for other things but new work, too,
1852 * e.g. if the current epoch got closed.
1853 * In which case we send the barrier above. */
1854 }
1855 finish_wait(&connection->sender_work.q_wait, &wait);
1856
1857 /* someone may have changed the config while we have been waiting above. */
1858 rcu_read_lock();
1859 nc = rcu_dereference(connection->net_conf);
1860 cork = nc ? nc->tcp_cork : 0;
1861 rcu_read_unlock();
1862 mutex_lock(&connection->data.mutex);
1863 if (connection->data.socket) {
1864 if (cork)
1865 drbd_tcp_cork(connection->data.socket);
1866 else if (!uncork)
1867 drbd_tcp_uncork(connection->data.socket);
1868 }
1869 mutex_unlock(&connection->data.mutex);
1870}
1871
Philipp Reisnerb411b362009-09-25 16:07:19 -07001872int drbd_worker(struct drbd_thread *thi)
1873{
Philipp Reisner392c8802011-02-09 10:33:31 +01001874 struct drbd_tconn *tconn = thi->tconn;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001875 struct drbd_work *w = NULL;
Philipp Reisner0e29d162011-02-18 14:23:11 +01001876 struct drbd_conf *mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001877 LIST_HEAD(work_list);
Lars Ellenberg8c0785a2011-10-19 11:50:57 +02001878 int vnr;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001879
Andreas Gruenbachere77a0a52011-01-25 15:43:39 +01001880 while (get_t_state(thi) == RUNNING) {
Philipp Reisner80822282011-02-08 12:46:30 +01001881 drbd_thread_current_set_cpu(thi);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001882
Lars Ellenberg8c0785a2011-10-19 11:50:57 +02001883 /* as long as we use drbd_queue_work_front(),
1884 * we may only dequeue single work items here, not batches. */
1885 if (list_empty(&work_list))
Lars Ellenbergb6dd1a82011-11-28 15:04:49 +01001886 wait_for_work(tconn, &work_list);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001887
Lars Ellenberg8c0785a2011-10-19 11:50:57 +02001888 if (signal_pending(current)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001889 flush_signals(current);
Philipp Reisner19393e12011-02-09 10:09:07 +01001890 if (get_t_state(thi) == RUNNING) {
1891 conn_warn(tconn, "Worker got an unexpected signal\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07001892 continue;
Philipp Reisner19393e12011-02-09 10:09:07 +01001893 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001894 break;
1895 }
1896
Andreas Gruenbachere77a0a52011-01-25 15:43:39 +01001897 if (get_t_state(thi) != RUNNING)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001898 break;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001899
Lars Ellenberg8c0785a2011-10-19 11:50:57 +02001900 while (!list_empty(&work_list)) {
1901 w = list_first_entry(&work_list, struct drbd_work, list);
1902 list_del_init(&w->list);
1903 if (w->cb(w, tconn->cstate < C_WF_REPORT_PARAMS) == 0)
1904 continue;
Philipp Reisnerbbeb6412011-02-10 13:45:46 +01001905 if (tconn->cstate >= C_WF_REPORT_PARAMS)
1906 conn_request_state(tconn, NS(conn, C_NETWORK_FAILURE), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001907 }
1908 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001909
Lars Ellenberg8c0785a2011-10-19 11:50:57 +02001910 do {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001911 while (!list_empty(&work_list)) {
Lars Ellenberg8c0785a2011-10-19 11:50:57 +02001912 w = list_first_entry(&work_list, struct drbd_work, list);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001913 list_del_init(&w->list);
Philipp Reisner00d56942011-02-09 18:09:48 +01001914 w->cb(w, 1);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001915 }
Lars Ellenbergd5b27b02011-11-14 15:42:37 +01001916 dequeue_work_batch(&tconn->sender_work, &work_list);
Lars Ellenberg8c0785a2011-10-19 11:50:57 +02001917 } while (!list_empty(&work_list));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001918
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02001919 rcu_read_lock();
Lars Ellenbergf3990022011-03-23 14:31:09 +01001920 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
Philipp Reisner0e29d162011-02-18 14:23:11 +01001921 D_ASSERT(mdev->state.disk == D_DISKLESS && mdev->state.conn == C_STANDALONE);
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02001922 kref_get(&mdev->kref);
1923 rcu_read_unlock();
Philipp Reisner0e29d162011-02-18 14:23:11 +01001924 drbd_mdev_cleanup(mdev);
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02001925 kref_put(&mdev->kref, &drbd_minor_destroy);
1926 rcu_read_lock();
Philipp Reisner0e29d162011-02-18 14:23:11 +01001927 }
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02001928 rcu_read_unlock();
Philipp Reisnerb411b362009-09-25 16:07:19 -07001929
1930 return 0;
1931}