blob: 10438c41f559ba30a8468010f730c9bd250f48dc [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 Reisnerb411b362009-09-25 16:07:19 -070041static int w_make_ov_request(struct drbd_conf *mdev, struct drbd_work *w, int cancel);
Philipp Reisner9d77a5f2010-11-07 18:02:56 +010042static int w_make_resync_request(struct drbd_conf *mdev,
43 struct drbd_work *w, int cancel);
Philipp Reisnerb411b362009-09-25 16:07:19 -070044
45
46
Andreas Gruenbacherc5a91612011-01-25 17:33:38 +010047/* endio handlers:
48 * drbd_md_io_complete (defined here)
49 * drbd_endio_pri (defined here)
50 * drbd_endio_sec (defined here)
51 * bm_async_io_complete (defined in drbd_bitmap.c)
52 *
Philipp Reisnerb411b362009-09-25 16:07:19 -070053 * For all these callbacks, note the following:
54 * The callbacks will be called in irq context by the IDE drivers,
55 * and in Softirqs/Tasklets/BH context by the SCSI drivers.
56 * Try to get the locking right :)
57 *
58 */
59
60
61/* About the global_state_lock
62 Each state transition on an device holds a read lock. In case we have
63 to evaluate the sync after dependencies, we grab a write lock, because
64 we need stable states on all devices for that. */
65rwlock_t global_state_lock;
66
67/* used for synchronous meta data and bitmap IO
68 * submitted by drbd_md_sync_page_io()
69 */
70void drbd_md_io_complete(struct bio *bio, int error)
71{
72 struct drbd_md_io *md_io;
73
74 md_io = (struct drbd_md_io *)bio->bi_private;
75 md_io->error = error;
76
Philipp Reisnerb411b362009-09-25 16:07:19 -070077 complete(&md_io->event);
78}
79
80/* reads on behalf of the partner,
81 * "submitted" by the receiver
82 */
Lars Ellenberg45bb9122010-05-14 17:10:48 +020083void drbd_endio_read_sec_final(struct drbd_epoch_entry *e) __releases(local)
Philipp Reisnerb411b362009-09-25 16:07:19 -070084{
85 unsigned long flags = 0;
Lars Ellenberg45bb9122010-05-14 17:10:48 +020086 struct drbd_conf *mdev = e->mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -070087
88 D_ASSERT(e->block_id != ID_VACANT);
89
Philipp Reisnerb411b362009-09-25 16:07:19 -070090 spin_lock_irqsave(&mdev->req_lock, flags);
91 mdev->read_cnt += e->size >> 9;
92 list_del(&e->w.list);
93 if (list_empty(&mdev->read_ee))
94 wake_up(&mdev->ee_wait);
Lars Ellenberg45bb9122010-05-14 17:10:48 +020095 if (test_bit(__EE_WAS_ERROR, &e->flags))
Andreas Gruenbacher81e84652010-12-09 15:03:57 +010096 __drbd_chk_io_error(mdev, false);
Philipp Reisnerb411b362009-09-25 16:07:19 -070097 spin_unlock_irqrestore(&mdev->req_lock, flags);
98
Philipp Reisnerb411b362009-09-25 16:07:19 -070099 drbd_queue_work(&mdev->data.work, &e->w);
100 put_ldev(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700101}
102
103/* writes on behalf of the partner, or resync writes,
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200104 * "submitted" by the receiver, final stage. */
105static void drbd_endio_write_sec_final(struct drbd_epoch_entry *e) __releases(local)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700106{
107 unsigned long flags = 0;
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200108 struct drbd_conf *mdev = e->mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700109 sector_t e_sector;
110 int do_wake;
111 int is_syncer_req;
112 int do_al_complete_io;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700113
Philipp Reisnerb411b362009-09-25 16:07:19 -0700114 D_ASSERT(e->block_id != ID_VACANT);
115
Philipp Reisnerb411b362009-09-25 16:07:19 -0700116 /* after we moved e to done_ee,
117 * we may no longer access it,
118 * it may be freed/reused already!
119 * (as soon as we release the req_lock) */
120 e_sector = e->sector;
121 do_al_complete_io = e->flags & EE_CALL_AL_COMPLETE_IO;
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200122 is_syncer_req = is_syncer_block_id(e->block_id);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700123
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200124 spin_lock_irqsave(&mdev->req_lock, flags);
125 mdev->writ_cnt += e->size >> 9;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700126 list_del(&e->w.list); /* has been on active_ee or sync_ee */
127 list_add_tail(&e->w.list, &mdev->done_ee);
128
Bart Van Assche24c48302011-05-21 18:32:29 +0200129 /* No hlist_del_init(&e->collision) here, we did not send the Ack yet,
Philipp Reisnerb411b362009-09-25 16:07:19 -0700130 * neither did we wake possibly waiting conflicting requests.
131 * done from "drbd_process_done_ee" within the appropriate w.cb
132 * (e_end_block/e_end_resync_block) or from _drbd_clear_done_ee */
133
134 do_wake = is_syncer_req
135 ? list_empty(&mdev->sync_ee)
136 : list_empty(&mdev->active_ee);
137
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200138 if (test_bit(__EE_WAS_ERROR, &e->flags))
Andreas Gruenbacher81e84652010-12-09 15:03:57 +0100139 __drbd_chk_io_error(mdev, false);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700140 spin_unlock_irqrestore(&mdev->req_lock, flags);
141
142 if (is_syncer_req)
143 drbd_rs_complete_io(mdev, e_sector);
144
145 if (do_wake)
146 wake_up(&mdev->ee_wait);
147
148 if (do_al_complete_io)
149 drbd_al_complete_io(mdev, e_sector);
150
151 wake_asender(mdev);
152 put_ldev(mdev);
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200153}
Philipp Reisnerb411b362009-09-25 16:07:19 -0700154
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200155/* writes on behalf of the partner, or resync writes,
156 * "submitted" by the receiver.
157 */
158void drbd_endio_sec(struct bio *bio, int error)
159{
160 struct drbd_epoch_entry *e = bio->bi_private;
161 struct drbd_conf *mdev = e->mdev;
162 int uptodate = bio_flagged(bio, BIO_UPTODATE);
163 int is_write = bio_data_dir(bio) == WRITE;
164
Lars Ellenberg07194272010-12-20 15:38:07 +0100165 if (error && __ratelimit(&drbd_ratelimit_state))
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200166 dev_warn(DEV, "%s: error=%d s=%llus\n",
167 is_write ? "write" : "read", error,
168 (unsigned long long)e->sector);
169 if (!error && !uptodate) {
Lars Ellenberg07194272010-12-20 15:38:07 +0100170 if (__ratelimit(&drbd_ratelimit_state))
171 dev_warn(DEV, "%s: setting error to -EIO s=%llus\n",
172 is_write ? "write" : "read",
173 (unsigned long long)e->sector);
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200174 /* strange behavior of some lower level drivers...
175 * fail the request by clearing the uptodate flag,
176 * but do not return any error?! */
177 error = -EIO;
178 }
179
180 if (error)
181 set_bit(__EE_WAS_ERROR, &e->flags);
182
183 bio_put(bio); /* no need for the bio anymore */
184 if (atomic_dec_and_test(&e->pending_bios)) {
185 if (is_write)
186 drbd_endio_write_sec_final(e);
187 else
188 drbd_endio_read_sec_final(e);
189 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700190}
191
192/* read, readA or write requests on R_PRIMARY coming from drbd_make_request
193 */
194void drbd_endio_pri(struct bio *bio, int error)
195{
Lars Ellenberga1154132010-11-13 20:42:29 +0100196 unsigned long flags;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700197 struct drbd_request *req = bio->bi_private;
198 struct drbd_conf *mdev = req->mdev;
Lars Ellenberga1154132010-11-13 20:42:29 +0100199 struct bio_and_error m;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700200 enum drbd_req_event what;
201 int uptodate = bio_flagged(bio, BIO_UPTODATE);
202
Philipp Reisnerb411b362009-09-25 16:07:19 -0700203 if (!error && !uptodate) {
204 dev_warn(DEV, "p %s: setting error to -EIO\n",
205 bio_data_dir(bio) == WRITE ? "write" : "read");
206 /* strange behavior of some lower level drivers...
207 * fail the request by clearing the uptodate flag,
208 * but do not return any error?! */
209 error = -EIO;
210 }
211
Philipp Reisnerb411b362009-09-25 16:07:19 -0700212 /* to avoid recursion in __req_mod */
213 if (unlikely(error)) {
214 what = (bio_data_dir(bio) == WRITE)
215 ? write_completed_with_error
Lars Ellenberg5c3c7e62010-04-10 02:10:09 +0200216 : (bio_rw(bio) == READ)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700217 ? read_completed_with_error
218 : read_ahead_completed_with_error;
219 } else
220 what = completed_ok;
221
222 bio_put(req->private_bio);
223 req->private_bio = ERR_PTR(error);
224
Lars Ellenberga1154132010-11-13 20:42:29 +0100225 /* not req_mod(), we need irqsave here! */
226 spin_lock_irqsave(&mdev->req_lock, flags);
227 __req_mod(req, what, &m);
228 spin_unlock_irqrestore(&mdev->req_lock, flags);
229
230 if (m.bio)
231 complete_master_bio(mdev, &m);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700232}
233
Philipp Reisnerb411b362009-09-25 16:07:19 -0700234int w_read_retry_remote(struct drbd_conf *mdev, struct drbd_work *w, int cancel)
235{
236 struct drbd_request *req = container_of(w, struct drbd_request, w);
237
238 /* We should not detach for read io-error,
239 * but try to WRITE the P_DATA_REPLY to the failed location,
240 * to give the disk the chance to relocate that block */
241
242 spin_lock_irq(&mdev->req_lock);
Lars Ellenbergd255e5f2010-05-27 09:45:45 +0200243 if (cancel || mdev->state.pdsk != D_UP_TO_DATE) {
244 _req_mod(req, read_retry_remote_canceled);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700245 spin_unlock_irq(&mdev->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700246 return 1;
247 }
248 spin_unlock_irq(&mdev->req_lock);
249
250 return w_send_read_req(mdev, w, 0);
251}
252
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200253void drbd_csum_ee(struct drbd_conf *mdev, struct crypto_hash *tfm, struct drbd_epoch_entry *e, void *digest)
254{
255 struct hash_desc desc;
256 struct scatterlist sg;
257 struct page *page = e->pages;
258 struct page *tmp;
259 unsigned len;
260
261 desc.tfm = tfm;
262 desc.flags = 0;
263
264 sg_init_table(&sg, 1);
265 crypto_hash_init(&desc);
266
267 while ((tmp = page_chain_next(page))) {
268 /* all but the last page will be fully used */
269 sg_set_page(&sg, page, PAGE_SIZE, 0);
270 crypto_hash_update(&desc, &sg, sg.length);
271 page = tmp;
272 }
273 /* and now the last, possibly only partially used page */
274 len = e->size & (PAGE_SIZE - 1);
275 sg_set_page(&sg, page, len ?: PAGE_SIZE, 0);
276 crypto_hash_update(&desc, &sg, sg.length);
277 crypto_hash_final(&desc, digest);
278}
279
280void drbd_csum_bio(struct drbd_conf *mdev, struct crypto_hash *tfm, struct bio *bio, void *digest)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700281{
282 struct hash_desc desc;
283 struct scatterlist sg;
284 struct bio_vec *bvec;
285 int i;
286
287 desc.tfm = tfm;
288 desc.flags = 0;
289
290 sg_init_table(&sg, 1);
291 crypto_hash_init(&desc);
292
293 __bio_for_each_segment(bvec, bio, i, 0) {
294 sg_set_page(&sg, bvec->bv_page, bvec->bv_len, bvec->bv_offset);
295 crypto_hash_update(&desc, &sg, sg.length);
296 }
297 crypto_hash_final(&desc, digest);
298}
299
Lars Ellenberg53ea4332011-03-08 17:11:40 +0100300/* TODO merge common code with w_e_end_ov_req */
301int w_e_send_csum(struct drbd_conf *mdev, struct drbd_work *w, int cancel)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700302{
303 struct drbd_epoch_entry *e = container_of(w, struct drbd_epoch_entry, w);
304 int digest_size;
305 void *digest;
Lars Ellenberg53ea4332011-03-08 17:11:40 +0100306 int ok = 1;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700307
Lars Ellenberg53ea4332011-03-08 17:11:40 +0100308 if (unlikely(cancel))
309 goto out;
310
311 if (likely((e->flags & EE_WAS_ERROR) != 0))
312 goto out;
313
314 digest_size = crypto_hash_digestsize(mdev->csums_tfm);
315 digest = kmalloc(digest_size, GFP_NOIO);
316 if (digest) {
317 sector_t sector = e->sector;
318 unsigned int size = e->size;
319 drbd_csum_ee(mdev, mdev->csums_tfm, e, digest);
320 /* Free e and pages before send.
321 * In case we block on congestion, we could otherwise run into
322 * some distributed deadlock, if the other side blocks on
323 * congestion as well, because our receiver blocks in
324 * drbd_pp_alloc due to pp_in_use > max_buffers. */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700325 drbd_free_ee(mdev, e);
Lars Ellenberg53ea4332011-03-08 17:11:40 +0100326 e = NULL;
327 inc_rs_pending(mdev);
328 ok = drbd_send_drequest_csum(mdev, sector, size,
329 digest, digest_size,
330 P_CSUM_RS_REQUEST);
331 kfree(digest);
332 } else {
333 dev_err(DEV, "kmalloc() of digest failed.\n");
334 ok = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700335 }
336
Lars Ellenberg53ea4332011-03-08 17:11:40 +0100337out:
338 if (e)
339 drbd_free_ee(mdev, e);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700340
341 if (unlikely(!ok))
342 dev_err(DEV, "drbd_send_drequest(..., csum) failed\n");
343 return ok;
344}
345
346#define GFP_TRY (__GFP_HIGHMEM | __GFP_NOWARN)
347
348static int read_for_csum(struct drbd_conf *mdev, sector_t sector, int size)
349{
350 struct drbd_epoch_entry *e;
351
352 if (!get_ldev(mdev))
Lars Ellenberg80a40e42010-08-11 23:28:00 +0200353 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700354
Philipp Reisnere3555d82010-11-07 15:56:29 +0100355 if (drbd_rs_should_slow_down(mdev, sector))
Lars Ellenberg0f0601f2010-08-11 23:40:24 +0200356 goto defer;
357
Philipp Reisnerb411b362009-09-25 16:07:19 -0700358 /* GFP_TRY, because if there is no memory available right now, this may
359 * be rescheduled for later. It is "only" background resync, after all. */
Andreas Gruenbacher9a8e7752011-01-11 14:04:09 +0100360 e = drbd_alloc_ee(mdev, ID_SYNCER /* unused */, sector, size, GFP_TRY);
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200361 if (!e)
Lars Ellenberg80a40e42010-08-11 23:28:00 +0200362 goto defer;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700363
Lars Ellenberg80a40e42010-08-11 23:28:00 +0200364 e->w.cb = w_e_send_csum;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700365 spin_lock_irq(&mdev->req_lock);
366 list_add(&e->w.list, &mdev->read_ee);
367 spin_unlock_irq(&mdev->req_lock);
368
Lars Ellenberg0f0601f2010-08-11 23:40:24 +0200369 atomic_add(size >> 9, &mdev->rs_sect_ev);
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200370 if (drbd_submit_ee(mdev, e, READ, DRBD_FAULT_RS_RD) == 0)
Lars Ellenberg80a40e42010-08-11 23:28:00 +0200371 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700372
Lars Ellenberg10f6d9922011-01-24 14:47:09 +0100373 /* If it failed because of ENOMEM, retry should help. If it failed
374 * because bio_add_page failed (probably broken lower level driver),
375 * retry may or may not help.
376 * If it does not, you may need to force disconnect. */
Lars Ellenberg22cc37a2010-09-14 20:40:41 +0200377 spin_lock_irq(&mdev->req_lock);
378 list_del(&e->w.list);
379 spin_unlock_irq(&mdev->req_lock);
380
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200381 drbd_free_ee(mdev, e);
Lars Ellenberg80a40e42010-08-11 23:28:00 +0200382defer:
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200383 put_ldev(mdev);
Lars Ellenberg80a40e42010-08-11 23:28:00 +0200384 return -EAGAIN;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700385}
386
Philipp Reisner794abb72010-12-27 11:51:23 +0100387int w_resync_timer(struct drbd_conf *mdev, struct drbd_work *w, int cancel)
388{
389 switch (mdev->state.conn) {
390 case C_VERIFY_S:
391 w_make_ov_request(mdev, w, cancel);
392 break;
393 case C_SYNC_TARGET:
394 w_make_resync_request(mdev, w, cancel);
395 break;
396 }
397
398 return 1;
399}
400
Philipp Reisnerb411b362009-09-25 16:07:19 -0700401void resync_timer_fn(unsigned long data)
402{
Philipp Reisnerb411b362009-09-25 16:07:19 -0700403 struct drbd_conf *mdev = (struct drbd_conf *) data;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700404
Philipp Reisner794abb72010-12-27 11:51:23 +0100405 if (list_empty(&mdev->resync_work.list))
Philipp Reisnerb411b362009-09-25 16:07:19 -0700406 drbd_queue_work(&mdev->data.work, &mdev->resync_work);
407}
408
Philipp Reisner778f2712010-07-06 11:14:00 +0200409static void fifo_set(struct fifo_buffer *fb, int value)
410{
411 int i;
412
413 for (i = 0; i < fb->size; i++)
Philipp Reisnerf10f2622010-10-05 16:50:17 +0200414 fb->values[i] = value;
Philipp Reisner778f2712010-07-06 11:14:00 +0200415}
416
417static int fifo_push(struct fifo_buffer *fb, int value)
418{
419 int ov;
420
421 ov = fb->values[fb->head_index];
422 fb->values[fb->head_index++] = value;
423
424 if (fb->head_index >= fb->size)
425 fb->head_index = 0;
426
427 return ov;
428}
429
430static void fifo_add_val(struct fifo_buffer *fb, int value)
431{
432 int i;
433
434 for (i = 0; i < fb->size; i++)
435 fb->values[i] += value;
436}
437
Philipp Reisner9d77a5f2010-11-07 18:02:56 +0100438static int drbd_rs_controller(struct drbd_conf *mdev)
Philipp Reisner778f2712010-07-06 11:14:00 +0200439{
440 unsigned int sect_in; /* Number of sectors that came in since the last turn */
441 unsigned int want; /* The number of sectors we want in the proxy */
442 int req_sect; /* Number of sectors to request in this turn */
443 int correction; /* Number of sectors more we need in the proxy*/
444 int cps; /* correction per invocation of drbd_rs_controller() */
445 int steps; /* Number of time steps to plan ahead */
446 int curr_corr;
447 int max_sect;
448
449 sect_in = atomic_xchg(&mdev->rs_sect_in, 0); /* Number of sectors that came in */
450 mdev->rs_in_flight -= sect_in;
451
452 spin_lock(&mdev->peer_seq_lock); /* get an atomic view on mdev->rs_plan_s */
453
454 steps = mdev->rs_plan_s.size; /* (mdev->sync_conf.c_plan_ahead * 10 * SLEEP_TIME) / HZ; */
455
456 if (mdev->rs_in_flight + sect_in == 0) { /* At start of resync */
457 want = ((mdev->sync_conf.rate * 2 * SLEEP_TIME) / HZ) * steps;
458 } else { /* normal path */
459 want = mdev->sync_conf.c_fill_target ? mdev->sync_conf.c_fill_target :
460 sect_in * mdev->sync_conf.c_delay_target * HZ / (SLEEP_TIME * 10);
461 }
462
463 correction = want - mdev->rs_in_flight - mdev->rs_planed;
464
465 /* Plan ahead */
466 cps = correction / steps;
467 fifo_add_val(&mdev->rs_plan_s, cps);
468 mdev->rs_planed += cps * steps;
469
470 /* What we do in this step */
471 curr_corr = fifo_push(&mdev->rs_plan_s, 0);
472 spin_unlock(&mdev->peer_seq_lock);
473 mdev->rs_planed -= curr_corr;
474
475 req_sect = sect_in + curr_corr;
476 if (req_sect < 0)
477 req_sect = 0;
478
479 max_sect = (mdev->sync_conf.c_max_rate * 2 * SLEEP_TIME) / HZ;
480 if (req_sect > max_sect)
481 req_sect = max_sect;
482
483 /*
484 dev_warn(DEV, "si=%u if=%d wa=%u co=%d st=%d cps=%d pl=%d cc=%d rs=%d\n",
485 sect_in, mdev->rs_in_flight, want, correction,
486 steps, cps, mdev->rs_planed, curr_corr, req_sect);
487 */
488
489 return req_sect;
490}
491
Philipp Reisner9d77a5f2010-11-07 18:02:56 +0100492static int drbd_rs_number_requests(struct drbd_conf *mdev)
Lars Ellenberge65f4402010-11-05 10:04:07 +0100493{
494 int number;
495 if (mdev->rs_plan_s.size) { /* mdev->sync_conf.c_plan_ahead */
496 number = drbd_rs_controller(mdev) >> (BM_BLOCK_SHIFT - 9);
497 mdev->c_sync_rate = number * HZ * (BM_BLOCK_SIZE / 1024) / SLEEP_TIME;
498 } else {
499 mdev->c_sync_rate = mdev->sync_conf.rate;
500 number = SLEEP_TIME * mdev->c_sync_rate / ((BM_BLOCK_SIZE / 1024) * HZ);
501 }
502
Lars Ellenberge65f4402010-11-05 10:04:07 +0100503 /* ignore the amount of pending requests, the resync controller should
504 * throttle down to incoming reply rate soon enough anyways. */
505 return number;
506}
507
Philipp Reisner9d77a5f2010-11-07 18:02:56 +0100508static int w_make_resync_request(struct drbd_conf *mdev,
509 struct drbd_work *w, int cancel)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700510{
511 unsigned long bit;
512 sector_t sector;
513 const sector_t capacity = drbd_get_capacity(mdev->this_bdev);
Lars Ellenberg1816a2b2010-11-11 15:19:07 +0100514 int max_bio_size;
Lars Ellenberge65f4402010-11-05 10:04:07 +0100515 int number, rollback_i, size;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700516 int align, queued, sndbuf;
Lars Ellenberg0f0601f2010-08-11 23:40:24 +0200517 int i = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700518
519 if (unlikely(cancel))
520 return 1;
521
Lars Ellenbergaf85e8e2010-10-07 16:07:55 +0200522 if (mdev->rs_total == 0) {
523 /* empty resync? */
524 drbd_resync_finished(mdev);
525 return 1;
526 }
527
Philipp Reisnerb411b362009-09-25 16:07:19 -0700528 if (!get_ldev(mdev)) {
529 /* Since we only need to access mdev->rsync a
530 get_ldev_if_state(mdev,D_FAILED) would be sufficient, but
531 to continue resync with a broken disk makes no sense at
532 all */
533 dev_err(DEV, "Disk broke down during resync!\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700534 return 1;
535 }
536
Philipp Reisner0cfdd242011-05-25 11:14:35 +0200537 max_bio_size = queue_max_hw_sectors(mdev->rq_queue) << 9;
Lars Ellenberge65f4402010-11-05 10:04:07 +0100538 number = drbd_rs_number_requests(mdev);
539 if (number == 0)
Lars Ellenberg0f0601f2010-08-11 23:40:24 +0200540 goto requeue;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700541
Philipp Reisnerb411b362009-09-25 16:07:19 -0700542 for (i = 0; i < number; i++) {
543 /* Stop generating RS requests, when half of the send buffer is filled */
544 mutex_lock(&mdev->data.mutex);
545 if (mdev->data.socket) {
546 queued = mdev->data.socket->sk->sk_wmem_queued;
547 sndbuf = mdev->data.socket->sk->sk_sndbuf;
548 } else {
549 queued = 1;
550 sndbuf = 0;
551 }
552 mutex_unlock(&mdev->data.mutex);
553 if (queued > sndbuf / 2)
554 goto requeue;
555
556next_sector:
557 size = BM_BLOCK_SIZE;
558 bit = drbd_bm_find_next(mdev, mdev->bm_resync_fo);
559
Lars Ellenberg4b0715f2010-12-14 15:13:04 +0100560 if (bit == DRBD_END_OF_BITMAP) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700561 mdev->bm_resync_fo = drbd_bm_bits(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700562 put_ldev(mdev);
563 return 1;
564 }
565
566 sector = BM_BIT_TO_SECT(bit);
567
Philipp Reisnere3555d82010-11-07 15:56:29 +0100568 if (drbd_rs_should_slow_down(mdev, sector) ||
569 drbd_try_rs_begin_io(mdev, sector)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700570 mdev->bm_resync_fo = bit;
571 goto requeue;
572 }
573 mdev->bm_resync_fo = bit + 1;
574
575 if (unlikely(drbd_bm_test_bit(mdev, bit) == 0)) {
576 drbd_rs_complete_io(mdev, sector);
577 goto next_sector;
578 }
579
Lars Ellenberg1816a2b2010-11-11 15:19:07 +0100580#if DRBD_MAX_BIO_SIZE > BM_BLOCK_SIZE
Philipp Reisnerb411b362009-09-25 16:07:19 -0700581 /* try to find some adjacent bits.
582 * we stop if we have already the maximum req size.
583 *
584 * Additionally always align bigger requests, in order to
585 * be prepared for all stripe sizes of software RAIDs.
Philipp Reisnerb411b362009-09-25 16:07:19 -0700586 */
587 align = 1;
Philipp Reisnerd2074502010-07-22 15:27:27 +0200588 rollback_i = i;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700589 for (;;) {
Lars Ellenberg1816a2b2010-11-11 15:19:07 +0100590 if (size + BM_BLOCK_SIZE > max_bio_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700591 break;
592
593 /* Be always aligned */
594 if (sector & ((1<<(align+3))-1))
595 break;
596
597 /* do not cross extent boundaries */
598 if (((bit+1) & BM_BLOCKS_PER_BM_EXT_MASK) == 0)
599 break;
600 /* now, is it actually dirty, after all?
601 * caution, drbd_bm_test_bit is tri-state for some
602 * obscure reason; ( b == 0 ) would get the out-of-band
603 * only accidentally right because of the "oddly sized"
604 * adjustment below */
605 if (drbd_bm_test_bit(mdev, bit+1) != 1)
606 break;
607 bit++;
608 size += BM_BLOCK_SIZE;
609 if ((BM_BLOCK_SIZE << align) <= size)
610 align++;
611 i++;
612 }
613 /* if we merged some,
614 * reset the offset to start the next drbd_bm_find_next from */
615 if (size > BM_BLOCK_SIZE)
616 mdev->bm_resync_fo = bit + 1;
617#endif
618
619 /* adjust very last sectors, in case we are oddly sized */
620 if (sector + (size>>9) > capacity)
621 size = (capacity-sector)<<9;
622 if (mdev->agreed_pro_version >= 89 && mdev->csums_tfm) {
623 switch (read_for_csum(mdev, sector, size)) {
Lars Ellenberg80a40e42010-08-11 23:28:00 +0200624 case -EIO: /* Disk failure */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700625 put_ldev(mdev);
626 return 0;
Lars Ellenberg80a40e42010-08-11 23:28:00 +0200627 case -EAGAIN: /* allocation failed, or ldev busy */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700628 drbd_rs_complete_io(mdev, sector);
629 mdev->bm_resync_fo = BM_SECT_TO_BIT(sector);
Philipp Reisnerd2074502010-07-22 15:27:27 +0200630 i = rollback_i;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700631 goto requeue;
Lars Ellenberg80a40e42010-08-11 23:28:00 +0200632 case 0:
633 /* everything ok */
634 break;
635 default:
636 BUG();
Philipp Reisnerb411b362009-09-25 16:07:19 -0700637 }
638 } else {
639 inc_rs_pending(mdev);
640 if (!drbd_send_drequest(mdev, P_RS_DATA_REQUEST,
641 sector, size, ID_SYNCER)) {
642 dev_err(DEV, "drbd_send_drequest() failed, aborting...\n");
643 dec_rs_pending(mdev);
644 put_ldev(mdev);
645 return 0;
646 }
647 }
648 }
649
650 if (mdev->bm_resync_fo >= drbd_bm_bits(mdev)) {
651 /* last syncer _request_ was sent,
652 * but the P_RS_DATA_REPLY not yet received. sync will end (and
653 * next sync group will resume), as soon as we receive the last
654 * resync data block, and the last bit is cleared.
655 * until then resync "work" is "inactive" ...
656 */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700657 put_ldev(mdev);
658 return 1;
659 }
660
661 requeue:
Philipp Reisner778f2712010-07-06 11:14:00 +0200662 mdev->rs_in_flight += (i << (BM_BLOCK_SHIFT - 9));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700663 mod_timer(&mdev->resync_timer, jiffies + SLEEP_TIME);
664 put_ldev(mdev);
665 return 1;
666}
667
668static int w_make_ov_request(struct drbd_conf *mdev, struct drbd_work *w, int cancel)
669{
670 int number, i, size;
671 sector_t sector;
672 const sector_t capacity = drbd_get_capacity(mdev->this_bdev);
673
674 if (unlikely(cancel))
675 return 1;
676
Lars Ellenberg2649f082010-11-05 10:05:47 +0100677 number = drbd_rs_number_requests(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700678
679 sector = mdev->ov_position;
680 for (i = 0; i < number; i++) {
681 if (sector >= capacity) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700682 return 1;
683 }
684
685 size = BM_BLOCK_SIZE;
686
Philipp Reisnere3555d82010-11-07 15:56:29 +0100687 if (drbd_rs_should_slow_down(mdev, sector) ||
688 drbd_try_rs_begin_io(mdev, sector)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700689 mdev->ov_position = sector;
690 goto requeue;
691 }
692
693 if (sector + (size>>9) > capacity)
694 size = (capacity-sector)<<9;
695
696 inc_rs_pending(mdev);
697 if (!drbd_send_ov_request(mdev, sector, size)) {
698 dec_rs_pending(mdev);
699 return 0;
700 }
701 sector += BM_SECT_PER_BIT;
702 }
703 mdev->ov_position = sector;
704
705 requeue:
Lars Ellenberg2649f082010-11-05 10:05:47 +0100706 mdev->rs_in_flight += (i << (BM_BLOCK_SHIFT - 9));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700707 mod_timer(&mdev->resync_timer, jiffies + SLEEP_TIME);
708 return 1;
709}
710
Philipp Reisnerc4752ef2010-10-27 17:32:36 +0200711
Philipp Reisner370a43e2011-01-14 16:03:11 +0100712void start_resync_timer_fn(unsigned long data)
713{
714 struct drbd_conf *mdev = (struct drbd_conf *) data;
715
716 drbd_queue_work(&mdev->data.work, &mdev->start_resync_work);
717}
718
Philipp Reisnerc4752ef2010-10-27 17:32:36 +0200719int w_start_resync(struct drbd_conf *mdev, struct drbd_work *w, int cancel)
720{
Philipp Reisner370a43e2011-01-14 16:03:11 +0100721 if (atomic_read(&mdev->unacked_cnt) || atomic_read(&mdev->rs_pending_cnt)) {
722 dev_warn(DEV, "w_start_resync later...\n");
723 mdev->start_resync_timer.expires = jiffies + HZ/10;
724 add_timer(&mdev->start_resync_timer);
725 return 1;
726 }
Philipp Reisnerc4752ef2010-10-27 17:32:36 +0200727
Philipp Reisner370a43e2011-01-14 16:03:11 +0100728 drbd_start_resync(mdev, C_SYNC_SOURCE);
729 clear_bit(AHEAD_TO_SYNC_SOURCE, &mdev->current_epoch->flags);
Philipp Reisnerc4752ef2010-10-27 17:32:36 +0200730 return 1;
731}
732
Philipp Reisnerb411b362009-09-25 16:07:19 -0700733int w_ov_finished(struct drbd_conf *mdev, struct drbd_work *w, int cancel)
734{
735 kfree(w);
736 ov_oos_print(mdev);
737 drbd_resync_finished(mdev);
738
739 return 1;
740}
741
742static int w_resync_finished(struct drbd_conf *mdev, struct drbd_work *w, int cancel)
743{
744 kfree(w);
745
746 drbd_resync_finished(mdev);
747
748 return 1;
749}
750
Lars Ellenbergaf85e8e2010-10-07 16:07:55 +0200751static void ping_peer(struct drbd_conf *mdev)
752{
753 clear_bit(GOT_PING_ACK, &mdev->flags);
754 request_ping(mdev);
755 wait_event(mdev->misc_wait,
756 test_bit(GOT_PING_ACK, &mdev->flags) || mdev->state.conn < C_CONNECTED);
757}
758
Philipp Reisnerb411b362009-09-25 16:07:19 -0700759int drbd_resync_finished(struct drbd_conf *mdev)
760{
761 unsigned long db, dt, dbdt;
762 unsigned long n_oos;
763 union drbd_state os, ns;
764 struct drbd_work *w;
765 char *khelper_cmd = NULL;
Lars Ellenberg26525612010-11-05 09:56:33 +0100766 int verify_done = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700767
768 /* Remove all elements from the resync LRU. Since future actions
769 * might set bits in the (main) bitmap, then the entries in the
770 * resync LRU would be wrong. */
771 if (drbd_rs_del_all(mdev)) {
772 /* In case this is not possible now, most probably because
773 * there are P_RS_DATA_REPLY Packets lingering on the worker's
774 * queue (or even the read operations for those packets
775 * is not finished by now). Retry in 100ms. */
776
Philipp Reisner20ee6392011-01-18 15:28:59 +0100777 schedule_timeout_interruptible(HZ / 10);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700778 w = kmalloc(sizeof(struct drbd_work), GFP_ATOMIC);
779 if (w) {
780 w->cb = w_resync_finished;
781 drbd_queue_work(&mdev->data.work, w);
782 return 1;
783 }
784 dev_err(DEV, "Warn failed to drbd_rs_del_all() and to kmalloc(w).\n");
785 }
786
787 dt = (jiffies - mdev->rs_start - mdev->rs_paused) / HZ;
788 if (dt <= 0)
789 dt = 1;
790 db = mdev->rs_total;
791 dbdt = Bit2KB(db/dt);
792 mdev->rs_paused /= HZ;
793
794 if (!get_ldev(mdev))
795 goto out;
796
Lars Ellenbergaf85e8e2010-10-07 16:07:55 +0200797 ping_peer(mdev);
798
Philipp Reisnerb411b362009-09-25 16:07:19 -0700799 spin_lock_irq(&mdev->req_lock);
800 os = mdev->state;
801
Lars Ellenberg26525612010-11-05 09:56:33 +0100802 verify_done = (os.conn == C_VERIFY_S || os.conn == C_VERIFY_T);
803
Philipp Reisnerb411b362009-09-25 16:07:19 -0700804 /* This protects us against multiple calls (that can happen in the presence
805 of application IO), and against connectivity loss just before we arrive here. */
806 if (os.conn <= C_CONNECTED)
807 goto out_unlock;
808
809 ns = os;
810 ns.conn = C_CONNECTED;
811
812 dev_info(DEV, "%s done (total %lu sec; paused %lu sec; %lu K/sec)\n",
Lars Ellenberg26525612010-11-05 09:56:33 +0100813 verify_done ? "Online verify " : "Resync",
Philipp Reisnerb411b362009-09-25 16:07:19 -0700814 dt + mdev->rs_paused, mdev->rs_paused, dbdt);
815
816 n_oos = drbd_bm_total_weight(mdev);
817
818 if (os.conn == C_VERIFY_S || os.conn == C_VERIFY_T) {
819 if (n_oos) {
820 dev_alert(DEV, "Online verify found %lu %dk block out of sync!\n",
821 n_oos, Bit2KB(1));
822 khelper_cmd = "out-of-sync";
823 }
824 } else {
825 D_ASSERT((n_oos - mdev->rs_failed) == 0);
826
827 if (os.conn == C_SYNC_TARGET || os.conn == C_PAUSED_SYNC_T)
828 khelper_cmd = "after-resync-target";
829
830 if (mdev->csums_tfm && mdev->rs_total) {
831 const unsigned long s = mdev->rs_same_csum;
832 const unsigned long t = mdev->rs_total;
833 const int ratio =
834 (t == 0) ? 0 :
835 (t < 100000) ? ((s*100)/t) : (s/(t/100));
Bart Van Assche24c48302011-05-21 18:32:29 +0200836 dev_info(DEV, "%u %% had equal checksums, eliminated: %luK; "
Philipp Reisnerb411b362009-09-25 16:07:19 -0700837 "transferred %luK total %luK\n",
838 ratio,
839 Bit2KB(mdev->rs_same_csum),
840 Bit2KB(mdev->rs_total - mdev->rs_same_csum),
841 Bit2KB(mdev->rs_total));
842 }
843 }
844
845 if (mdev->rs_failed) {
846 dev_info(DEV, " %lu failed blocks\n", mdev->rs_failed);
847
848 if (os.conn == C_SYNC_TARGET || os.conn == C_PAUSED_SYNC_T) {
849 ns.disk = D_INCONSISTENT;
850 ns.pdsk = D_UP_TO_DATE;
851 } else {
852 ns.disk = D_UP_TO_DATE;
853 ns.pdsk = D_INCONSISTENT;
854 }
855 } else {
856 ns.disk = D_UP_TO_DATE;
857 ns.pdsk = D_UP_TO_DATE;
858
859 if (os.conn == C_SYNC_TARGET || os.conn == C_PAUSED_SYNC_T) {
860 if (mdev->p_uuid) {
861 int i;
862 for (i = UI_BITMAP ; i <= UI_HISTORY_END ; i++)
863 _drbd_uuid_set(mdev, i, mdev->p_uuid[i]);
864 drbd_uuid_set(mdev, UI_BITMAP, mdev->ldev->md.uuid[UI_CURRENT]);
865 _drbd_uuid_set(mdev, UI_CURRENT, mdev->p_uuid[UI_CURRENT]);
866 } else {
867 dev_err(DEV, "mdev->p_uuid is NULL! BUG\n");
868 }
869 }
870
Lars Ellenberg62b0da32011-01-20 13:25:21 +0100871 if (!(os.conn == C_VERIFY_S || os.conn == C_VERIFY_T)) {
872 /* for verify runs, we don't update uuids here,
873 * so there would be nothing to report. */
874 drbd_uuid_set_bm(mdev, 0UL);
875 drbd_print_uuids(mdev, "updated UUIDs");
876 if (mdev->p_uuid) {
877 /* Now the two UUID sets are equal, update what we
878 * know of the peer. */
879 int i;
880 for (i = UI_CURRENT ; i <= UI_HISTORY_END ; i++)
881 mdev->p_uuid[i] = mdev->ldev->md.uuid[i];
882 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700883 }
884 }
885
886 _drbd_set_state(mdev, ns, CS_VERBOSE, NULL);
887out_unlock:
888 spin_unlock_irq(&mdev->req_lock);
889 put_ldev(mdev);
890out:
891 mdev->rs_total = 0;
892 mdev->rs_failed = 0;
893 mdev->rs_paused = 0;
Lars Ellenberg26525612010-11-05 09:56:33 +0100894 if (verify_done)
895 mdev->ov_start_sector = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700896
Lars Ellenberg13d42682010-10-13 17:37:54 +0200897 drbd_md_sync(mdev);
898
Philipp Reisnerb411b362009-09-25 16:07:19 -0700899 if (khelper_cmd)
900 drbd_khelper(mdev, khelper_cmd);
901
902 return 1;
903}
904
905/* helper */
906static void move_to_net_ee_or_free(struct drbd_conf *mdev, struct drbd_epoch_entry *e)
907{
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200908 if (drbd_ee_has_active_page(e)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700909 /* This might happen if sendpage() has not finished */
Lars Ellenberg78db8922010-09-13 13:27:10 +0200910 int i = (e->size + PAGE_SIZE -1) >> PAGE_SHIFT;
Lars Ellenberg435f0742010-09-06 12:30:25 +0200911 atomic_add(i, &mdev->pp_in_use_by_net);
912 atomic_sub(i, &mdev->pp_in_use);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700913 spin_lock_irq(&mdev->req_lock);
914 list_add_tail(&e->w.list, &mdev->net_ee);
915 spin_unlock_irq(&mdev->req_lock);
Lars Ellenberg435f0742010-09-06 12:30:25 +0200916 wake_up(&drbd_pp_wait);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700917 } else
918 drbd_free_ee(mdev, e);
919}
920
921/**
922 * w_e_end_data_req() - Worker callback, to send a P_DATA_REPLY packet in response to a P_DATA_REQUEST
923 * @mdev: DRBD device.
924 * @w: work object.
925 * @cancel: The connection will be closed anyways
926 */
927int w_e_end_data_req(struct drbd_conf *mdev, struct drbd_work *w, int cancel)
928{
929 struct drbd_epoch_entry *e = container_of(w, struct drbd_epoch_entry, w);
930 int ok;
931
932 if (unlikely(cancel)) {
933 drbd_free_ee(mdev, e);
934 dec_unacked(mdev);
935 return 1;
936 }
937
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200938 if (likely((e->flags & EE_WAS_ERROR) == 0)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700939 ok = drbd_send_block(mdev, P_DATA_REPLY, e);
940 } else {
941 if (__ratelimit(&drbd_ratelimit_state))
942 dev_err(DEV, "Sending NegDReply. sector=%llus.\n",
943 (unsigned long long)e->sector);
944
945 ok = drbd_send_ack(mdev, P_NEG_DREPLY, e);
946 }
947
948 dec_unacked(mdev);
949
950 move_to_net_ee_or_free(mdev, e);
951
952 if (unlikely(!ok))
953 dev_err(DEV, "drbd_send_block() failed\n");
954 return ok;
955}
956
957/**
958 * w_e_end_rsdata_req() - Worker callback to send a P_RS_DATA_REPLY packet in response to a P_RS_DATA_REQUESTRS
959 * @mdev: DRBD device.
960 * @w: work object.
961 * @cancel: The connection will be closed anyways
962 */
963int w_e_end_rsdata_req(struct drbd_conf *mdev, struct drbd_work *w, int cancel)
964{
965 struct drbd_epoch_entry *e = container_of(w, struct drbd_epoch_entry, w);
966 int ok;
967
968 if (unlikely(cancel)) {
969 drbd_free_ee(mdev, e);
970 dec_unacked(mdev);
971 return 1;
972 }
973
974 if (get_ldev_if_state(mdev, D_FAILED)) {
975 drbd_rs_complete_io(mdev, e->sector);
976 put_ldev(mdev);
977 }
978
Philipp Reisnerd612d302010-12-27 10:53:28 +0100979 if (mdev->state.conn == C_AHEAD) {
980 ok = drbd_send_ack(mdev, P_RS_CANCEL, e);
981 } else if (likely((e->flags & EE_WAS_ERROR) == 0)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700982 if (likely(mdev->state.pdsk >= D_INCONSISTENT)) {
983 inc_rs_pending(mdev);
984 ok = drbd_send_block(mdev, P_RS_DATA_REPLY, e);
985 } else {
986 if (__ratelimit(&drbd_ratelimit_state))
987 dev_err(DEV, "Not sending RSDataReply, "
988 "partner DISKLESS!\n");
989 ok = 1;
990 }
991 } else {
992 if (__ratelimit(&drbd_ratelimit_state))
993 dev_err(DEV, "Sending NegRSDReply. sector %llus.\n",
994 (unsigned long long)e->sector);
995
996 ok = drbd_send_ack(mdev, P_NEG_RS_DREPLY, e);
997
998 /* update resync data with failure */
999 drbd_rs_failed_io(mdev, e->sector, e->size);
1000 }
1001
1002 dec_unacked(mdev);
1003
1004 move_to_net_ee_or_free(mdev, e);
1005
1006 if (unlikely(!ok))
1007 dev_err(DEV, "drbd_send_block() failed\n");
1008 return ok;
1009}
1010
1011int w_e_end_csum_rs_req(struct drbd_conf *mdev, struct drbd_work *w, int cancel)
1012{
1013 struct drbd_epoch_entry *e = container_of(w, struct drbd_epoch_entry, w);
1014 struct digest_info *di;
1015 int digest_size;
1016 void *digest = NULL;
1017 int ok, eq = 0;
1018
1019 if (unlikely(cancel)) {
1020 drbd_free_ee(mdev, e);
1021 dec_unacked(mdev);
1022 return 1;
1023 }
1024
Lars Ellenberg1d53f092010-09-05 01:13:24 +02001025 if (get_ldev(mdev)) {
1026 drbd_rs_complete_io(mdev, e->sector);
1027 put_ldev(mdev);
1028 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001029
Philipp Reisner85719572010-07-21 10:20:17 +02001030 di = e->digest;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001031
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001032 if (likely((e->flags & EE_WAS_ERROR) == 0)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001033 /* quick hack to try to avoid a race against reconfiguration.
1034 * a real fix would be much more involved,
1035 * introducing more locking mechanisms */
1036 if (mdev->csums_tfm) {
1037 digest_size = crypto_hash_digestsize(mdev->csums_tfm);
1038 D_ASSERT(digest_size == di->digest_size);
1039 digest = kmalloc(digest_size, GFP_NOIO);
1040 }
1041 if (digest) {
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001042 drbd_csum_ee(mdev, mdev->csums_tfm, e, digest);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001043 eq = !memcmp(digest, di->digest, digest_size);
1044 kfree(digest);
1045 }
1046
1047 if (eq) {
1048 drbd_set_in_sync(mdev, e->sector, e->size);
Lars Ellenberg676396d2010-03-03 02:08:22 +01001049 /* rs_same_csums unit is BM_BLOCK_SIZE */
1050 mdev->rs_same_csum += e->size >> BM_BLOCK_SHIFT;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001051 ok = drbd_send_ack(mdev, P_RS_IS_IN_SYNC, e);
1052 } else {
1053 inc_rs_pending(mdev);
Philipp Reisner204bba92010-08-23 16:17:13 +02001054 e->block_id = ID_SYNCER; /* By setting block_id, digest pointer becomes invalid! */
1055 e->flags &= ~EE_HAS_DIGEST; /* This e no longer has a digest pointer */
1056 kfree(di);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001057 ok = drbd_send_block(mdev, P_RS_DATA_REPLY, e);
1058 }
1059 } else {
1060 ok = drbd_send_ack(mdev, P_NEG_RS_DREPLY, e);
1061 if (__ratelimit(&drbd_ratelimit_state))
1062 dev_err(DEV, "Sending NegDReply. I guess it gets messy.\n");
1063 }
1064
1065 dec_unacked(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001066 move_to_net_ee_or_free(mdev, e);
1067
1068 if (unlikely(!ok))
1069 dev_err(DEV, "drbd_send_block/ack() failed\n");
1070 return ok;
1071}
1072
Lars Ellenberg53ea4332011-03-08 17:11:40 +01001073/* TODO merge common code with w_e_send_csum */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001074int w_e_end_ov_req(struct drbd_conf *mdev, struct drbd_work *w, int cancel)
1075{
1076 struct drbd_epoch_entry *e = container_of(w, struct drbd_epoch_entry, w);
Lars Ellenberg53ea4332011-03-08 17:11:40 +01001077 sector_t sector = e->sector;
1078 unsigned int size = e->size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001079 int digest_size;
1080 void *digest;
1081 int ok = 1;
1082
1083 if (unlikely(cancel))
1084 goto out;
1085
Philipp Reisnerb411b362009-09-25 16:07:19 -07001086 digest_size = crypto_hash_digestsize(mdev->verify_tfm);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001087 digest = kmalloc(digest_size, GFP_NOIO);
Philipp Reisner8f214202011-03-01 15:52:35 +01001088 if (!digest) {
1089 ok = 0; /* terminate the connection in case the allocation failed */
1090 goto out;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001091 }
1092
Philipp Reisner8f214202011-03-01 15:52:35 +01001093 if (likely(!(e->flags & EE_WAS_ERROR)))
1094 drbd_csum_ee(mdev, mdev->verify_tfm, e, digest);
1095 else
1096 memset(digest, 0, digest_size);
1097
Lars Ellenberg53ea4332011-03-08 17:11:40 +01001098 /* Free e and pages before send.
1099 * In case we block on congestion, we could otherwise run into
1100 * some distributed deadlock, if the other side blocks on
1101 * congestion as well, because our receiver blocks in
1102 * drbd_pp_alloc due to pp_in_use > max_buffers. */
1103 drbd_free_ee(mdev, e);
1104 e = NULL;
Philipp Reisner8f214202011-03-01 15:52:35 +01001105 inc_rs_pending(mdev);
Lars Ellenberg53ea4332011-03-08 17:11:40 +01001106 ok = drbd_send_drequest_csum(mdev, sector, size,
1107 digest, digest_size,
1108 P_OV_REPLY);
Philipp Reisner8f214202011-03-01 15:52:35 +01001109 if (!ok)
1110 dec_rs_pending(mdev);
1111 kfree(digest);
1112
Philipp Reisnerb411b362009-09-25 16:07:19 -07001113out:
Lars Ellenberg53ea4332011-03-08 17:11:40 +01001114 if (e)
1115 drbd_free_ee(mdev, e);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001116 dec_unacked(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001117 return ok;
1118}
1119
1120void drbd_ov_oos_found(struct drbd_conf *mdev, sector_t sector, int size)
1121{
1122 if (mdev->ov_last_oos_start + mdev->ov_last_oos_size == sector) {
1123 mdev->ov_last_oos_size += size>>9;
1124 } else {
1125 mdev->ov_last_oos_start = sector;
1126 mdev->ov_last_oos_size = size>>9;
1127 }
1128 drbd_set_out_of_sync(mdev, sector, size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001129}
1130
1131int w_e_end_ov_reply(struct drbd_conf *mdev, struct drbd_work *w, int cancel)
1132{
1133 struct drbd_epoch_entry *e = container_of(w, struct drbd_epoch_entry, w);
1134 struct digest_info *di;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001135 void *digest;
Lars Ellenberg53ea4332011-03-08 17:11:40 +01001136 sector_t sector = e->sector;
1137 unsigned int size = e->size;
1138 int digest_size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001139 int ok, eq = 0;
1140
1141 if (unlikely(cancel)) {
1142 drbd_free_ee(mdev, e);
1143 dec_unacked(mdev);
1144 return 1;
1145 }
1146
1147 /* after "cancel", because after drbd_disconnect/drbd_rs_cancel_all
1148 * the resync lru has been cleaned up already */
Lars Ellenberg1d53f092010-09-05 01:13:24 +02001149 if (get_ldev(mdev)) {
1150 drbd_rs_complete_io(mdev, e->sector);
1151 put_ldev(mdev);
1152 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001153
Philipp Reisner85719572010-07-21 10:20:17 +02001154 di = e->digest;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001155
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001156 if (likely((e->flags & EE_WAS_ERROR) == 0)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001157 digest_size = crypto_hash_digestsize(mdev->verify_tfm);
1158 digest = kmalloc(digest_size, GFP_NOIO);
1159 if (digest) {
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001160 drbd_csum_ee(mdev, mdev->verify_tfm, e, digest);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001161
1162 D_ASSERT(digest_size == di->digest_size);
1163 eq = !memcmp(digest, di->digest, digest_size);
1164 kfree(digest);
1165 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001166 }
1167
Lars Ellenberg53ea4332011-03-08 17:11:40 +01001168 /* Free e and pages before send.
1169 * In case we block on congestion, we could otherwise run into
1170 * some distributed deadlock, if the other side blocks on
1171 * congestion as well, because our receiver blocks in
1172 * drbd_pp_alloc due to pp_in_use > max_buffers. */
1173 drbd_free_ee(mdev, e);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001174 if (!eq)
Lars Ellenberg53ea4332011-03-08 17:11:40 +01001175 drbd_ov_oos_found(mdev, sector, size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001176 else
1177 ov_oos_print(mdev);
1178
Lars Ellenberg53ea4332011-03-08 17:11:40 +01001179 ok = drbd_send_ack_ex(mdev, P_OV_RESULT, sector, size,
Philipp Reisnerb411b362009-09-25 16:07:19 -07001180 eq ? ID_IN_SYNC : ID_OUT_OF_SYNC);
1181
Lars Ellenberg53ea4332011-03-08 17:11:40 +01001182 dec_unacked(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001183
Lars Ellenbergea5442a2010-11-05 09:48:01 +01001184 --mdev->ov_left;
1185
1186 /* let's advance progress step marks only for every other megabyte */
1187 if ((mdev->ov_left & 0x200) == 0x200)
1188 drbd_advance_rs_marks(mdev, mdev->ov_left);
1189
1190 if (mdev->ov_left == 0) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001191 ov_oos_print(mdev);
1192 drbd_resync_finished(mdev);
1193 }
1194
1195 return ok;
1196}
1197
1198int w_prev_work_done(struct drbd_conf *mdev, struct drbd_work *w, int cancel)
1199{
1200 struct drbd_wq_barrier *b = container_of(w, struct drbd_wq_barrier, w);
1201 complete(&b->done);
1202 return 1;
1203}
1204
1205int w_send_barrier(struct drbd_conf *mdev, struct drbd_work *w, int cancel)
1206{
1207 struct drbd_tl_epoch *b = container_of(w, struct drbd_tl_epoch, w);
1208 struct p_barrier *p = &mdev->data.sbuf.barrier;
1209 int ok = 1;
1210
1211 /* really avoid racing with tl_clear. w.cb may have been referenced
1212 * just before it was reassigned and re-queued, so double check that.
1213 * actually, this race was harmless, since we only try to send the
1214 * barrier packet here, and otherwise do nothing with the object.
1215 * but compare with the head of w_clear_epoch */
1216 spin_lock_irq(&mdev->req_lock);
1217 if (w->cb != w_send_barrier || mdev->state.conn < C_CONNECTED)
1218 cancel = 1;
1219 spin_unlock_irq(&mdev->req_lock);
1220 if (cancel)
1221 return 1;
1222
1223 if (!drbd_get_data_sock(mdev))
1224 return 0;
1225 p->barrier = b->br_number;
1226 /* inc_ap_pending was done where this was queued.
1227 * dec_ap_pending will be done in got_BarrierAck
1228 * or (on connection loss) in w_clear_epoch. */
1229 ok = _drbd_send_cmd(mdev, mdev->data.socket, P_BARRIER,
Philipp Reisner0b70a132010-08-20 13:36:10 +02001230 (struct p_header80 *)p, sizeof(*p), 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001231 drbd_put_data_sock(mdev);
1232
1233 return ok;
1234}
1235
1236int w_send_write_hint(struct drbd_conf *mdev, struct drbd_work *w, int cancel)
1237{
1238 if (cancel)
1239 return 1;
1240 return drbd_send_short_cmd(mdev, P_UNPLUG_REMOTE);
1241}
1242
Philipp Reisner73a01a12010-10-27 14:33:00 +02001243int w_send_oos(struct drbd_conf *mdev, struct drbd_work *w, int cancel)
1244{
1245 struct drbd_request *req = container_of(w, struct drbd_request, w);
1246 int ok;
1247
1248 if (unlikely(cancel)) {
1249 req_mod(req, send_canceled);
1250 return 1;
1251 }
1252
1253 ok = drbd_send_oos(mdev, req);
1254 req_mod(req, oos_handed_to_network);
1255
1256 return ok;
1257}
1258
Philipp Reisnerb411b362009-09-25 16:07:19 -07001259/**
1260 * w_send_dblock() - Worker callback to send a P_DATA packet in order to mirror a write request
1261 * @mdev: DRBD device.
1262 * @w: work object.
1263 * @cancel: The connection will be closed anyways
1264 */
1265int w_send_dblock(struct drbd_conf *mdev, struct drbd_work *w, int cancel)
1266{
1267 struct drbd_request *req = container_of(w, struct drbd_request, w);
1268 int ok;
1269
1270 if (unlikely(cancel)) {
1271 req_mod(req, send_canceled);
1272 return 1;
1273 }
1274
1275 ok = drbd_send_dblock(mdev, req);
1276 req_mod(req, ok ? handed_over_to_network : send_failed);
1277
1278 return ok;
1279}
1280
1281/**
1282 * w_send_read_req() - Worker callback to send a read request (P_DATA_REQUEST) packet
1283 * @mdev: DRBD device.
1284 * @w: work object.
1285 * @cancel: The connection will be closed anyways
1286 */
1287int w_send_read_req(struct drbd_conf *mdev, struct drbd_work *w, int cancel)
1288{
1289 struct drbd_request *req = container_of(w, struct drbd_request, w);
1290 int ok;
1291
1292 if (unlikely(cancel)) {
1293 req_mod(req, send_canceled);
1294 return 1;
1295 }
1296
1297 ok = drbd_send_drequest(mdev, P_DATA_REQUEST, req->sector, req->size,
1298 (unsigned long)req);
1299
1300 if (!ok) {
1301 /* ?? we set C_TIMEOUT or C_BROKEN_PIPE in drbd_send();
1302 * so this is probably redundant */
1303 if (mdev->state.conn >= C_CONNECTED)
1304 drbd_force_state(mdev, NS(conn, C_NETWORK_FAILURE));
1305 }
1306 req_mod(req, ok ? handed_over_to_network : send_failed);
1307
1308 return ok;
1309}
1310
Philipp Reisner265be2d2010-05-31 10:14:17 +02001311int w_restart_disk_io(struct drbd_conf *mdev, struct drbd_work *w, int cancel)
1312{
1313 struct drbd_request *req = container_of(w, struct drbd_request, w);
1314
Philipp Reisner07782862010-08-31 12:00:50 +02001315 if (bio_data_dir(req->master_bio) == WRITE && req->rq_state & RQ_IN_ACT_LOG)
Philipp Reisner265be2d2010-05-31 10:14:17 +02001316 drbd_al_begin_io(mdev, req->sector);
1317 /* Calling drbd_al_begin_io() out of the worker might deadlocks
1318 theoretically. Practically it can not deadlock, since this is
1319 only used when unfreezing IOs. All the extents of the requests
1320 that made it into the TL are already active */
1321
1322 drbd_req_make_private_bio(req, req->master_bio);
1323 req->private_bio->bi_bdev = mdev->ldev->backing_bdev;
1324 generic_make_request(req->private_bio);
1325
1326 return 1;
1327}
1328
Philipp Reisnerb411b362009-09-25 16:07:19 -07001329static int _drbd_may_sync_now(struct drbd_conf *mdev)
1330{
1331 struct drbd_conf *odev = mdev;
1332
1333 while (1) {
1334 if (odev->sync_conf.after == -1)
1335 return 1;
1336 odev = minor_to_mdev(odev->sync_conf.after);
1337 ERR_IF(!odev) return 1;
1338 if ((odev->state.conn >= C_SYNC_SOURCE &&
1339 odev->state.conn <= C_PAUSED_SYNC_T) ||
1340 odev->state.aftr_isp || odev->state.peer_isp ||
1341 odev->state.user_isp)
1342 return 0;
1343 }
1344}
1345
1346/**
1347 * _drbd_pause_after() - Pause resync on all devices that may not resync now
1348 * @mdev: DRBD device.
1349 *
1350 * Called from process context only (admin command and after_state_ch).
1351 */
1352static int _drbd_pause_after(struct drbd_conf *mdev)
1353{
1354 struct drbd_conf *odev;
1355 int i, rv = 0;
1356
1357 for (i = 0; i < minor_count; i++) {
1358 odev = minor_to_mdev(i);
1359 if (!odev)
1360 continue;
1361 if (odev->state.conn == C_STANDALONE && odev->state.disk == D_DISKLESS)
1362 continue;
1363 if (!_drbd_may_sync_now(odev))
1364 rv |= (__drbd_set_state(_NS(odev, aftr_isp, 1), CS_HARD, NULL)
1365 != SS_NOTHING_TO_DO);
1366 }
1367
1368 return rv;
1369}
1370
1371/**
1372 * _drbd_resume_next() - Resume resync on all devices that may resync now
1373 * @mdev: DRBD device.
1374 *
1375 * Called from process context only (admin command and worker).
1376 */
1377static int _drbd_resume_next(struct drbd_conf *mdev)
1378{
1379 struct drbd_conf *odev;
1380 int i, rv = 0;
1381
1382 for (i = 0; i < minor_count; i++) {
1383 odev = minor_to_mdev(i);
1384 if (!odev)
1385 continue;
1386 if (odev->state.conn == C_STANDALONE && odev->state.disk == D_DISKLESS)
1387 continue;
1388 if (odev->state.aftr_isp) {
1389 if (_drbd_may_sync_now(odev))
1390 rv |= (__drbd_set_state(_NS(odev, aftr_isp, 0),
1391 CS_HARD, NULL)
1392 != SS_NOTHING_TO_DO) ;
1393 }
1394 }
1395 return rv;
1396}
1397
1398void resume_next_sg(struct drbd_conf *mdev)
1399{
1400 write_lock_irq(&global_state_lock);
1401 _drbd_resume_next(mdev);
1402 write_unlock_irq(&global_state_lock);
1403}
1404
1405void suspend_other_sg(struct drbd_conf *mdev)
1406{
1407 write_lock_irq(&global_state_lock);
1408 _drbd_pause_after(mdev);
1409 write_unlock_irq(&global_state_lock);
1410}
1411
1412static int sync_after_error(struct drbd_conf *mdev, int o_minor)
1413{
1414 struct drbd_conf *odev;
1415
1416 if (o_minor == -1)
1417 return NO_ERROR;
1418 if (o_minor < -1 || minor_to_mdev(o_minor) == NULL)
1419 return ERR_SYNC_AFTER;
1420
1421 /* check for loops */
1422 odev = minor_to_mdev(o_minor);
1423 while (1) {
1424 if (odev == mdev)
1425 return ERR_SYNC_AFTER_CYCLE;
1426
1427 /* dependency chain ends here, no cycles. */
1428 if (odev->sync_conf.after == -1)
1429 return NO_ERROR;
1430
1431 /* follow the dependency chain */
1432 odev = minor_to_mdev(odev->sync_conf.after);
1433 }
1434}
1435
1436int drbd_alter_sa(struct drbd_conf *mdev, int na)
1437{
1438 int changes;
1439 int retcode;
1440
1441 write_lock_irq(&global_state_lock);
1442 retcode = sync_after_error(mdev, na);
1443 if (retcode == NO_ERROR) {
1444 mdev->sync_conf.after = na;
1445 do {
1446 changes = _drbd_pause_after(mdev);
1447 changes |= _drbd_resume_next(mdev);
1448 } while (changes);
1449 }
1450 write_unlock_irq(&global_state_lock);
1451 return retcode;
1452}
1453
Lars Ellenberg9bd28d32010-11-05 09:55:18 +01001454void drbd_rs_controller_reset(struct drbd_conf *mdev)
1455{
1456 atomic_set(&mdev->rs_sect_in, 0);
1457 atomic_set(&mdev->rs_sect_ev, 0);
1458 mdev->rs_in_flight = 0;
1459 mdev->rs_planed = 0;
1460 spin_lock(&mdev->peer_seq_lock);
1461 fifo_set(&mdev->rs_plan_s, 0);
1462 spin_unlock(&mdev->peer_seq_lock);
1463}
1464
Philipp Reisnerb411b362009-09-25 16:07:19 -07001465/**
1466 * drbd_start_resync() - Start the resync process
1467 * @mdev: DRBD device.
1468 * @side: Either C_SYNC_SOURCE or C_SYNC_TARGET
1469 *
1470 * This function might bring you directly into one of the
1471 * C_PAUSED_SYNC_* states.
1472 */
1473void drbd_start_resync(struct drbd_conf *mdev, enum drbd_conns side)
1474{
1475 union drbd_state ns;
1476 int r;
1477
Philipp Reisnerc4752ef2010-10-27 17:32:36 +02001478 if (mdev->state.conn >= C_SYNC_SOURCE && mdev->state.conn < C_AHEAD) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001479 dev_err(DEV, "Resync already running!\n");
1480 return;
1481 }
1482
Philipp Reisner59817f42010-10-29 12:44:20 +02001483 if (mdev->state.conn < C_AHEAD) {
1484 /* In case a previous resync run was aborted by an IO error/detach on the peer. */
1485 drbd_rs_cancel_all(mdev);
1486 /* This should be done when we abort the resync. We definitely do not
1487 want to have this for connections going back and forth between
1488 Ahead/Behind and SyncSource/SyncTarget */
1489 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001490
1491 if (side == C_SYNC_TARGET) {
1492 /* Since application IO was locked out during C_WF_BITMAP_T and
1493 C_WF_SYNC_UUID we are still unmodified. Before going to C_SYNC_TARGET
1494 we check that we might make the data inconsistent. */
1495 r = drbd_khelper(mdev, "before-resync-target");
1496 r = (r >> 8) & 0xff;
1497 if (r > 0) {
1498 dev_info(DEV, "before-resync-target handler returned %d, "
1499 "dropping connection.\n", r);
1500 drbd_force_state(mdev, NS(conn, C_DISCONNECTING));
1501 return;
1502 }
Philipp Reisner09b9e792010-12-03 16:04:24 +01001503 } else /* C_SYNC_SOURCE */ {
1504 r = drbd_khelper(mdev, "before-resync-source");
1505 r = (r >> 8) & 0xff;
1506 if (r > 0) {
1507 if (r == 3) {
1508 dev_info(DEV, "before-resync-source handler returned %d, "
1509 "ignoring. Old userland tools?", r);
1510 } else {
1511 dev_info(DEV, "before-resync-source handler returned %d, "
1512 "dropping connection.\n", r);
1513 drbd_force_state(mdev, NS(conn, C_DISCONNECTING));
1514 return;
1515 }
1516 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001517 }
1518
1519 drbd_state_lock(mdev);
1520
1521 if (!get_ldev_if_state(mdev, D_NEGOTIATING)) {
1522 drbd_state_unlock(mdev);
1523 return;
1524 }
1525
Philipp Reisnerb411b362009-09-25 16:07:19 -07001526 write_lock_irq(&global_state_lock);
1527 ns = mdev->state;
1528
1529 ns.aftr_isp = !_drbd_may_sync_now(mdev);
1530
1531 ns.conn = side;
1532
1533 if (side == C_SYNC_TARGET)
1534 ns.disk = D_INCONSISTENT;
1535 else /* side == C_SYNC_SOURCE */
1536 ns.pdsk = D_INCONSISTENT;
1537
1538 r = __drbd_set_state(mdev, ns, CS_VERBOSE, NULL);
1539 ns = mdev->state;
1540
1541 if (ns.conn < C_CONNECTED)
1542 r = SS_UNKNOWN_ERROR;
1543
1544 if (r == SS_SUCCESS) {
Lars Ellenberg1d7734a2010-08-11 21:21:50 +02001545 unsigned long tw = drbd_bm_total_weight(mdev);
1546 unsigned long now = jiffies;
1547 int i;
1548
Philipp Reisnerb411b362009-09-25 16:07:19 -07001549 mdev->rs_failed = 0;
1550 mdev->rs_paused = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001551 mdev->rs_same_csum = 0;
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02001552 mdev->rs_last_events = 0;
1553 mdev->rs_last_sect_ev = 0;
Lars Ellenberg1d7734a2010-08-11 21:21:50 +02001554 mdev->rs_total = tw;
1555 mdev->rs_start = now;
1556 for (i = 0; i < DRBD_SYNC_MARKS; i++) {
1557 mdev->rs_mark_left[i] = tw;
1558 mdev->rs_mark_time[i] = now;
1559 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001560 _drbd_pause_after(mdev);
1561 }
1562 write_unlock_irq(&global_state_lock);
Lars Ellenberg5a22db82010-12-17 21:14:23 +01001563
Philipp Reisnerb411b362009-09-25 16:07:19 -07001564 if (r == SS_SUCCESS) {
1565 dev_info(DEV, "Began resync as %s (will sync %lu KB [%lu bits set]).\n",
1566 drbd_conn_str(ns.conn),
1567 (unsigned long) mdev->rs_total << (BM_BLOCK_SHIFT-10),
1568 (unsigned long) mdev->rs_total);
Lars Ellenberg6c922ed2011-01-12 11:51:13 +01001569 if (side == C_SYNC_TARGET)
1570 mdev->bm_resync_fo = 0;
1571
1572 /* Since protocol 96, we must serialize drbd_gen_and_send_sync_uuid
1573 * with w_send_oos, or the sync target will get confused as to
1574 * how much bits to resync. We cannot do that always, because for an
1575 * empty resync and protocol < 95, we need to do it here, as we call
1576 * drbd_resync_finished from here in that case.
1577 * We drbd_gen_and_send_sync_uuid here for protocol < 96,
1578 * and from after_state_ch otherwise. */
1579 if (side == C_SYNC_SOURCE && mdev->agreed_pro_version < 96)
1580 drbd_gen_and_send_sync_uuid(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001581
Lars Ellenbergaf85e8e2010-10-07 16:07:55 +02001582 if (mdev->agreed_pro_version < 95 && mdev->rs_total == 0) {
1583 /* This still has a race (about when exactly the peers
1584 * detect connection loss) that can lead to a full sync
1585 * on next handshake. In 8.3.9 we fixed this with explicit
1586 * resync-finished notifications, but the fix
1587 * introduces a protocol change. Sleeping for some
1588 * time longer than the ping interval + timeout on the
1589 * SyncSource, to give the SyncTarget the chance to
1590 * detect connection loss, then waiting for a ping
1591 * response (implicit in drbd_resync_finished) reduces
1592 * the race considerably, but does not solve it. */
1593 if (side == C_SYNC_SOURCE)
1594 schedule_timeout_interruptible(
1595 mdev->net_conf->ping_int * HZ +
1596 mdev->net_conf->ping_timeo*HZ/9);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001597 drbd_resync_finished(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001598 }
1599
Lars Ellenberg9bd28d32010-11-05 09:55:18 +01001600 drbd_rs_controller_reset(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001601 /* ns.conn may already be != mdev->state.conn,
1602 * we may have been paused in between, or become paused until
1603 * the timer triggers.
1604 * No matter, that is handled in resync_timer_fn() */
1605 if (ns.conn == C_SYNC_TARGET)
1606 mod_timer(&mdev->resync_timer, jiffies);
1607
1608 drbd_md_sync(mdev);
1609 }
Lars Ellenberg5a22db82010-12-17 21:14:23 +01001610 put_ldev(mdev);
Philipp Reisnerd0c3f602010-03-02 15:06:45 +01001611 drbd_state_unlock(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001612}
1613
1614int drbd_worker(struct drbd_thread *thi)
1615{
1616 struct drbd_conf *mdev = thi->mdev;
1617 struct drbd_work *w = NULL;
1618 LIST_HEAD(work_list);
1619 int intr = 0, i;
1620
1621 sprintf(current->comm, "drbd%d_worker", mdev_to_minor(mdev));
1622
1623 while (get_t_state(thi) == Running) {
1624 drbd_thread_current_set_cpu(mdev);
1625
1626 if (down_trylock(&mdev->data.work.s)) {
1627 mutex_lock(&mdev->data.mutex);
1628 if (mdev->data.socket && !mdev->net_conf->no_cork)
1629 drbd_tcp_uncork(mdev->data.socket);
1630 mutex_unlock(&mdev->data.mutex);
1631
1632 intr = down_interruptible(&mdev->data.work.s);
1633
1634 mutex_lock(&mdev->data.mutex);
1635 if (mdev->data.socket && !mdev->net_conf->no_cork)
1636 drbd_tcp_cork(mdev->data.socket);
1637 mutex_unlock(&mdev->data.mutex);
1638 }
1639
1640 if (intr) {
1641 D_ASSERT(intr == -EINTR);
1642 flush_signals(current);
1643 ERR_IF (get_t_state(thi) == Running)
1644 continue;
1645 break;
1646 }
1647
1648 if (get_t_state(thi) != Running)
1649 break;
1650 /* With this break, we have done a down() but not consumed
1651 the entry from the list. The cleanup code takes care of
1652 this... */
1653
1654 w = NULL;
1655 spin_lock_irq(&mdev->data.work.q_lock);
1656 ERR_IF(list_empty(&mdev->data.work.q)) {
1657 /* something terribly wrong in our logic.
1658 * we were able to down() the semaphore,
1659 * but the list is empty... doh.
1660 *
1661 * what is the best thing to do now?
1662 * try again from scratch, restarting the receiver,
1663 * asender, whatnot? could break even more ugly,
1664 * e.g. when we are primary, but no good local data.
1665 *
1666 * I'll try to get away just starting over this loop.
1667 */
1668 spin_unlock_irq(&mdev->data.work.q_lock);
1669 continue;
1670 }
1671 w = list_entry(mdev->data.work.q.next, struct drbd_work, list);
1672 list_del_init(&w->list);
1673 spin_unlock_irq(&mdev->data.work.q_lock);
1674
1675 if (!w->cb(mdev, w, mdev->state.conn < C_CONNECTED)) {
1676 /* dev_warn(DEV, "worker: a callback failed! \n"); */
1677 if (mdev->state.conn >= C_CONNECTED)
1678 drbd_force_state(mdev,
1679 NS(conn, C_NETWORK_FAILURE));
1680 }
1681 }
1682 D_ASSERT(test_bit(DEVICE_DYING, &mdev->flags));
1683 D_ASSERT(test_bit(CONFIG_PENDING, &mdev->flags));
1684
1685 spin_lock_irq(&mdev->data.work.q_lock);
1686 i = 0;
1687 while (!list_empty(&mdev->data.work.q)) {
1688 list_splice_init(&mdev->data.work.q, &work_list);
1689 spin_unlock_irq(&mdev->data.work.q_lock);
1690
1691 while (!list_empty(&work_list)) {
1692 w = list_entry(work_list.next, struct drbd_work, list);
1693 list_del_init(&w->list);
1694 w->cb(mdev, w, 1);
1695 i++; /* dead debugging code */
1696 }
1697
1698 spin_lock_irq(&mdev->data.work.q_lock);
1699 }
1700 sema_init(&mdev->data.work.s, 0);
1701 /* DANGEROUS race: if someone did queue his work within the spinlock,
1702 * but up() ed outside the spinlock, we could get an up() on the
1703 * semaphore without corresponding list entry.
1704 * So don't do that.
1705 */
1706 spin_unlock_irq(&mdev->data.work.q_lock);
1707
1708 D_ASSERT(mdev->state.disk == D_DISKLESS && mdev->state.conn == C_STANDALONE);
1709 /* _drbd_set_state only uses stop_nowait.
1710 * wait here for the Exiting receiver. */
1711 drbd_thread_stop(&mdev->receiver);
1712 drbd_mdev_cleanup(mdev);
1713
1714 dev_info(DEV, "worker terminated\n");
1715
1716 clear_bit(DEVICE_DYING, &mdev->flags);
1717 clear_bit(CONFIG_PENDING, &mdev->flags);
1718 wake_up(&mdev->state_wait);
1719
1720 return 0;
1721}