blob: 7b3d4dc4af84b96d085931a8c055fa7b4316e493 [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
47/* defined here:
48 drbd_md_io_complete
Lars Ellenberg45bb9122010-05-14 17:10:48 +020049 drbd_endio_sec
Philipp Reisnerb411b362009-09-25 16:07:19 -070050 drbd_endio_pri
51
52 * more endio handlers:
53 atodb_endio in drbd_actlog.c
54 drbd_bm_async_io_complete in drbd_bitmap.c
55
56 * For all these callbacks, note the following:
57 * The callbacks will be called in irq context by the IDE drivers,
58 * and in Softirqs/Tasklets/BH context by the SCSI drivers.
59 * Try to get the locking right :)
60 *
61 */
62
63
64/* About the global_state_lock
65 Each state transition on an device holds a read lock. In case we have
66 to evaluate the sync after dependencies, we grab a write lock, because
67 we need stable states on all devices for that. */
68rwlock_t global_state_lock;
69
70/* used for synchronous meta data and bitmap IO
71 * submitted by drbd_md_sync_page_io()
72 */
73void drbd_md_io_complete(struct bio *bio, int error)
74{
75 struct drbd_md_io *md_io;
76
77 md_io = (struct drbd_md_io *)bio->bi_private;
78 md_io->error = error;
79
Philipp Reisnerb411b362009-09-25 16:07:19 -070080 complete(&md_io->event);
81}
82
83/* reads on behalf of the partner,
84 * "submitted" by the receiver
85 */
Lars Ellenberg45bb9122010-05-14 17:10:48 +020086void drbd_endio_read_sec_final(struct drbd_epoch_entry *e) __releases(local)
Philipp Reisnerb411b362009-09-25 16:07:19 -070087{
88 unsigned long flags = 0;
Lars Ellenberg45bb9122010-05-14 17:10:48 +020089 struct drbd_conf *mdev = e->mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -070090
91 D_ASSERT(e->block_id != ID_VACANT);
92
Philipp Reisnerb411b362009-09-25 16:07:19 -070093 spin_lock_irqsave(&mdev->req_lock, flags);
94 mdev->read_cnt += e->size >> 9;
95 list_del(&e->w.list);
96 if (list_empty(&mdev->read_ee))
97 wake_up(&mdev->ee_wait);
Lars Ellenberg45bb9122010-05-14 17:10:48 +020098 if (test_bit(__EE_WAS_ERROR, &e->flags))
Andreas Gruenbacher81e84652010-12-09 15:03:57 +010099 __drbd_chk_io_error(mdev, false);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700100 spin_unlock_irqrestore(&mdev->req_lock, flags);
101
Philipp Reisnerb411b362009-09-25 16:07:19 -0700102 drbd_queue_work(&mdev->data.work, &e->w);
103 put_ldev(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700104}
105
106/* writes on behalf of the partner, or resync writes,
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200107 * "submitted" by the receiver, final stage. */
108static void drbd_endio_write_sec_final(struct drbd_epoch_entry *e) __releases(local)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700109{
110 unsigned long flags = 0;
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200111 struct drbd_conf *mdev = e->mdev;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700112 sector_t e_sector;
113 int do_wake;
114 int is_syncer_req;
115 int do_al_complete_io;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700116
Philipp Reisnerb411b362009-09-25 16:07:19 -0700117 D_ASSERT(e->block_id != ID_VACANT);
118
Philipp Reisnerb411b362009-09-25 16:07:19 -0700119 /* after we moved e to done_ee,
120 * we may no longer access it,
121 * it may be freed/reused already!
122 * (as soon as we release the req_lock) */
123 e_sector = e->sector;
124 do_al_complete_io = e->flags & EE_CALL_AL_COMPLETE_IO;
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200125 is_syncer_req = is_syncer_block_id(e->block_id);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700126
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200127 spin_lock_irqsave(&mdev->req_lock, flags);
128 mdev->writ_cnt += e->size >> 9;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700129 list_del(&e->w.list); /* has been on active_ee or sync_ee */
130 list_add_tail(&e->w.list, &mdev->done_ee);
131
Philipp Reisnerb411b362009-09-25 16:07:19 -0700132 /* No hlist_del_init(&e->colision) here, we did not send the Ack yet,
133 * neither did we wake possibly waiting conflicting requests.
134 * done from "drbd_process_done_ee" within the appropriate w.cb
135 * (e_end_block/e_end_resync_block) or from _drbd_clear_done_ee */
136
137 do_wake = is_syncer_req
138 ? list_empty(&mdev->sync_ee)
139 : list_empty(&mdev->active_ee);
140
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200141 if (test_bit(__EE_WAS_ERROR, &e->flags))
Andreas Gruenbacher81e84652010-12-09 15:03:57 +0100142 __drbd_chk_io_error(mdev, false);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700143 spin_unlock_irqrestore(&mdev->req_lock, flags);
144
145 if (is_syncer_req)
146 drbd_rs_complete_io(mdev, e_sector);
147
148 if (do_wake)
149 wake_up(&mdev->ee_wait);
150
151 if (do_al_complete_io)
152 drbd_al_complete_io(mdev, e_sector);
153
154 wake_asender(mdev);
155 put_ldev(mdev);
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200156}
Philipp Reisnerb411b362009-09-25 16:07:19 -0700157
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200158/* writes on behalf of the partner, or resync writes,
159 * "submitted" by the receiver.
160 */
161void drbd_endio_sec(struct bio *bio, int error)
162{
163 struct drbd_epoch_entry *e = bio->bi_private;
164 struct drbd_conf *mdev = e->mdev;
165 int uptodate = bio_flagged(bio, BIO_UPTODATE);
166 int is_write = bio_data_dir(bio) == WRITE;
167
Lars Ellenberg07194272010-12-20 15:38:07 +0100168 if (error && __ratelimit(&drbd_ratelimit_state))
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200169 dev_warn(DEV, "%s: error=%d s=%llus\n",
170 is_write ? "write" : "read", error,
171 (unsigned long long)e->sector);
172 if (!error && !uptodate) {
Lars Ellenberg07194272010-12-20 15:38:07 +0100173 if (__ratelimit(&drbd_ratelimit_state))
174 dev_warn(DEV, "%s: setting error to -EIO s=%llus\n",
175 is_write ? "write" : "read",
176 (unsigned long long)e->sector);
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200177 /* strange behavior of some lower level drivers...
178 * fail the request by clearing the uptodate flag,
179 * but do not return any error?! */
180 error = -EIO;
181 }
182
183 if (error)
184 set_bit(__EE_WAS_ERROR, &e->flags);
185
186 bio_put(bio); /* no need for the bio anymore */
187 if (atomic_dec_and_test(&e->pending_bios)) {
188 if (is_write)
189 drbd_endio_write_sec_final(e);
190 else
191 drbd_endio_read_sec_final(e);
192 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700193}
194
195/* read, readA or write requests on R_PRIMARY coming from drbd_make_request
196 */
197void drbd_endio_pri(struct bio *bio, int error)
198{
Lars Ellenberga1154132010-11-13 20:42:29 +0100199 unsigned long flags;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700200 struct drbd_request *req = bio->bi_private;
201 struct drbd_conf *mdev = req->mdev;
Lars Ellenberga1154132010-11-13 20:42:29 +0100202 struct bio_and_error m;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700203 enum drbd_req_event what;
204 int uptodate = bio_flagged(bio, BIO_UPTODATE);
205
Philipp Reisnerb411b362009-09-25 16:07:19 -0700206 if (!error && !uptodate) {
207 dev_warn(DEV, "p %s: setting error to -EIO\n",
208 bio_data_dir(bio) == WRITE ? "write" : "read");
209 /* strange behavior of some lower level drivers...
210 * fail the request by clearing the uptodate flag,
211 * but do not return any error?! */
212 error = -EIO;
213 }
214
Philipp Reisnerb411b362009-09-25 16:07:19 -0700215 /* to avoid recursion in __req_mod */
216 if (unlikely(error)) {
217 what = (bio_data_dir(bio) == WRITE)
218 ? write_completed_with_error
Lars Ellenberg5c3c7e62010-04-10 02:10:09 +0200219 : (bio_rw(bio) == READ)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700220 ? read_completed_with_error
221 : read_ahead_completed_with_error;
222 } else
223 what = completed_ok;
224
225 bio_put(req->private_bio);
226 req->private_bio = ERR_PTR(error);
227
Lars Ellenberga1154132010-11-13 20:42:29 +0100228 /* not req_mod(), we need irqsave here! */
229 spin_lock_irqsave(&mdev->req_lock, flags);
230 __req_mod(req, what, &m);
231 spin_unlock_irqrestore(&mdev->req_lock, flags);
232
233 if (m.bio)
234 complete_master_bio(mdev, &m);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700235}
236
Philipp Reisnerb411b362009-09-25 16:07:19 -0700237int w_read_retry_remote(struct drbd_conf *mdev, struct drbd_work *w, int cancel)
238{
239 struct drbd_request *req = container_of(w, struct drbd_request, w);
240
241 /* We should not detach for read io-error,
242 * but try to WRITE the P_DATA_REPLY to the failed location,
243 * to give the disk the chance to relocate that block */
244
245 spin_lock_irq(&mdev->req_lock);
Lars Ellenbergd255e5f2010-05-27 09:45:45 +0200246 if (cancel || mdev->state.pdsk != D_UP_TO_DATE) {
247 _req_mod(req, read_retry_remote_canceled);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700248 spin_unlock_irq(&mdev->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700249 return 1;
250 }
251 spin_unlock_irq(&mdev->req_lock);
252
253 return w_send_read_req(mdev, w, 0);
254}
255
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200256void drbd_csum_ee(struct drbd_conf *mdev, struct crypto_hash *tfm, struct drbd_epoch_entry *e, void *digest)
257{
258 struct hash_desc desc;
259 struct scatterlist sg;
260 struct page *page = e->pages;
261 struct page *tmp;
262 unsigned len;
263
264 desc.tfm = tfm;
265 desc.flags = 0;
266
267 sg_init_table(&sg, 1);
268 crypto_hash_init(&desc);
269
270 while ((tmp = page_chain_next(page))) {
271 /* all but the last page will be fully used */
272 sg_set_page(&sg, page, PAGE_SIZE, 0);
273 crypto_hash_update(&desc, &sg, sg.length);
274 page = tmp;
275 }
276 /* and now the last, possibly only partially used page */
277 len = e->size & (PAGE_SIZE - 1);
278 sg_set_page(&sg, page, len ?: PAGE_SIZE, 0);
279 crypto_hash_update(&desc, &sg, sg.length);
280 crypto_hash_final(&desc, digest);
281}
282
283void drbd_csum_bio(struct drbd_conf *mdev, struct crypto_hash *tfm, struct bio *bio, void *digest)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700284{
285 struct hash_desc desc;
286 struct scatterlist sg;
287 struct bio_vec *bvec;
288 int i;
289
290 desc.tfm = tfm;
291 desc.flags = 0;
292
293 sg_init_table(&sg, 1);
294 crypto_hash_init(&desc);
295
296 __bio_for_each_segment(bvec, bio, i, 0) {
297 sg_set_page(&sg, bvec->bv_page, bvec->bv_len, bvec->bv_offset);
298 crypto_hash_update(&desc, &sg, sg.length);
299 }
300 crypto_hash_final(&desc, digest);
301}
302
303static int w_e_send_csum(struct drbd_conf *mdev, struct drbd_work *w, int cancel)
304{
305 struct drbd_epoch_entry *e = container_of(w, struct drbd_epoch_entry, w);
306 int digest_size;
307 void *digest;
308 int ok;
309
310 D_ASSERT(e->block_id == DRBD_MAGIC + 0xbeef);
311
312 if (unlikely(cancel)) {
313 drbd_free_ee(mdev, e);
314 return 1;
315 }
316
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200317 if (likely((e->flags & EE_WAS_ERROR) == 0)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700318 digest_size = crypto_hash_digestsize(mdev->csums_tfm);
319 digest = kmalloc(digest_size, GFP_NOIO);
320 if (digest) {
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200321 drbd_csum_ee(mdev, mdev->csums_tfm, e, digest);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700322
323 inc_rs_pending(mdev);
324 ok = drbd_send_drequest_csum(mdev,
325 e->sector,
326 e->size,
327 digest,
328 digest_size,
329 P_CSUM_RS_REQUEST);
330 kfree(digest);
331 } else {
332 dev_err(DEV, "kmalloc() of digest failed.\n");
333 ok = 0;
334 }
335 } else
336 ok = 1;
337
338 drbd_free_ee(mdev, e);
339
340 if (unlikely(!ok))
341 dev_err(DEV, "drbd_send_drequest(..., csum) failed\n");
342 return ok;
343}
344
345#define GFP_TRY (__GFP_HIGHMEM | __GFP_NOWARN)
346
347static int read_for_csum(struct drbd_conf *mdev, sector_t sector, int size)
348{
349 struct drbd_epoch_entry *e;
350
351 if (!get_ldev(mdev))
Lars Ellenberg80a40e42010-08-11 23:28:00 +0200352 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700353
Philipp Reisnere3555d82010-11-07 15:56:29 +0100354 if (drbd_rs_should_slow_down(mdev, sector))
Lars Ellenberg0f0601f2010-08-11 23:40:24 +0200355 goto defer;
356
Philipp Reisnerb411b362009-09-25 16:07:19 -0700357 /* GFP_TRY, because if there is no memory available right now, this may
358 * be rescheduled for later. It is "only" background resync, after all. */
359 e = drbd_alloc_ee(mdev, DRBD_MAGIC+0xbeef, sector, size, GFP_TRY);
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200360 if (!e)
Lars Ellenberg80a40e42010-08-11 23:28:00 +0200361 goto defer;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700362
Lars Ellenberg80a40e42010-08-11 23:28:00 +0200363 e->w.cb = w_e_send_csum;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700364 spin_lock_irq(&mdev->req_lock);
365 list_add(&e->w.list, &mdev->read_ee);
366 spin_unlock_irq(&mdev->req_lock);
367
Lars Ellenberg0f0601f2010-08-11 23:40:24 +0200368 atomic_add(size >> 9, &mdev->rs_sect_ev);
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200369 if (drbd_submit_ee(mdev, e, READ, DRBD_FAULT_RS_RD) == 0)
Lars Ellenberg80a40e42010-08-11 23:28:00 +0200370 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700371
Lars Ellenberg22cc37a2010-09-14 20:40:41 +0200372 /* drbd_submit_ee currently fails for one reason only:
373 * not being able to allocate enough bios.
374 * Is dropping the connection going to help? */
375 spin_lock_irq(&mdev->req_lock);
376 list_del(&e->w.list);
377 spin_unlock_irq(&mdev->req_lock);
378
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200379 drbd_free_ee(mdev, e);
Lars Ellenberg80a40e42010-08-11 23:28:00 +0200380defer:
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200381 put_ldev(mdev);
Lars Ellenberg80a40e42010-08-11 23:28:00 +0200382 return -EAGAIN;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700383}
384
Philipp Reisner794abb72010-12-27 11:51:23 +0100385int w_resync_timer(struct drbd_conf *mdev, struct drbd_work *w, int cancel)
386{
387 switch (mdev->state.conn) {
388 case C_VERIFY_S:
389 w_make_ov_request(mdev, w, cancel);
390 break;
391 case C_SYNC_TARGET:
392 w_make_resync_request(mdev, w, cancel);
393 break;
394 }
395
396 return 1;
397}
398
Philipp Reisnerb411b362009-09-25 16:07:19 -0700399void resync_timer_fn(unsigned long data)
400{
Philipp Reisnerb411b362009-09-25 16:07:19 -0700401 struct drbd_conf *mdev = (struct drbd_conf *) data;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700402
Philipp Reisner794abb72010-12-27 11:51:23 +0100403 if (list_empty(&mdev->resync_work.list))
Philipp Reisnerb411b362009-09-25 16:07:19 -0700404 drbd_queue_work(&mdev->data.work, &mdev->resync_work);
405}
406
Philipp Reisner778f2712010-07-06 11:14:00 +0200407static void fifo_set(struct fifo_buffer *fb, int value)
408{
409 int i;
410
411 for (i = 0; i < fb->size; i++)
Philipp Reisnerf10f2622010-10-05 16:50:17 +0200412 fb->values[i] = value;
Philipp Reisner778f2712010-07-06 11:14:00 +0200413}
414
415static int fifo_push(struct fifo_buffer *fb, int value)
416{
417 int ov;
418
419 ov = fb->values[fb->head_index];
420 fb->values[fb->head_index++] = value;
421
422 if (fb->head_index >= fb->size)
423 fb->head_index = 0;
424
425 return ov;
426}
427
428static void fifo_add_val(struct fifo_buffer *fb, int value)
429{
430 int i;
431
432 for (i = 0; i < fb->size; i++)
433 fb->values[i] += value;
434}
435
Philipp Reisner9d77a5f2010-11-07 18:02:56 +0100436static int drbd_rs_controller(struct drbd_conf *mdev)
Philipp Reisner778f2712010-07-06 11:14:00 +0200437{
438 unsigned int sect_in; /* Number of sectors that came in since the last turn */
439 unsigned int want; /* The number of sectors we want in the proxy */
440 int req_sect; /* Number of sectors to request in this turn */
441 int correction; /* Number of sectors more we need in the proxy*/
442 int cps; /* correction per invocation of drbd_rs_controller() */
443 int steps; /* Number of time steps to plan ahead */
444 int curr_corr;
445 int max_sect;
446
447 sect_in = atomic_xchg(&mdev->rs_sect_in, 0); /* Number of sectors that came in */
448 mdev->rs_in_flight -= sect_in;
449
450 spin_lock(&mdev->peer_seq_lock); /* get an atomic view on mdev->rs_plan_s */
451
452 steps = mdev->rs_plan_s.size; /* (mdev->sync_conf.c_plan_ahead * 10 * SLEEP_TIME) / HZ; */
453
454 if (mdev->rs_in_flight + sect_in == 0) { /* At start of resync */
455 want = ((mdev->sync_conf.rate * 2 * SLEEP_TIME) / HZ) * steps;
456 } else { /* normal path */
457 want = mdev->sync_conf.c_fill_target ? mdev->sync_conf.c_fill_target :
458 sect_in * mdev->sync_conf.c_delay_target * HZ / (SLEEP_TIME * 10);
459 }
460
461 correction = want - mdev->rs_in_flight - mdev->rs_planed;
462
463 /* Plan ahead */
464 cps = correction / steps;
465 fifo_add_val(&mdev->rs_plan_s, cps);
466 mdev->rs_planed += cps * steps;
467
468 /* What we do in this step */
469 curr_corr = fifo_push(&mdev->rs_plan_s, 0);
470 spin_unlock(&mdev->peer_seq_lock);
471 mdev->rs_planed -= curr_corr;
472
473 req_sect = sect_in + curr_corr;
474 if (req_sect < 0)
475 req_sect = 0;
476
477 max_sect = (mdev->sync_conf.c_max_rate * 2 * SLEEP_TIME) / HZ;
478 if (req_sect > max_sect)
479 req_sect = max_sect;
480
481 /*
482 dev_warn(DEV, "si=%u if=%d wa=%u co=%d st=%d cps=%d pl=%d cc=%d rs=%d\n",
483 sect_in, mdev->rs_in_flight, want, correction,
484 steps, cps, mdev->rs_planed, curr_corr, req_sect);
485 */
486
487 return req_sect;
488}
489
Philipp Reisner9d77a5f2010-11-07 18:02:56 +0100490static int drbd_rs_number_requests(struct drbd_conf *mdev)
Lars Ellenberge65f4402010-11-05 10:04:07 +0100491{
492 int number;
493 if (mdev->rs_plan_s.size) { /* mdev->sync_conf.c_plan_ahead */
494 number = drbd_rs_controller(mdev) >> (BM_BLOCK_SHIFT - 9);
495 mdev->c_sync_rate = number * HZ * (BM_BLOCK_SIZE / 1024) / SLEEP_TIME;
496 } else {
497 mdev->c_sync_rate = mdev->sync_conf.rate;
498 number = SLEEP_TIME * mdev->c_sync_rate / ((BM_BLOCK_SIZE / 1024) * HZ);
499 }
500
Lars Ellenberge65f4402010-11-05 10:04:07 +0100501 /* ignore the amount of pending requests, the resync controller should
502 * throttle down to incoming reply rate soon enough anyways. */
503 return number;
504}
505
Philipp Reisner9d77a5f2010-11-07 18:02:56 +0100506static int w_make_resync_request(struct drbd_conf *mdev,
507 struct drbd_work *w, int cancel)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700508{
509 unsigned long bit;
510 sector_t sector;
511 const sector_t capacity = drbd_get_capacity(mdev->this_bdev);
Lars Ellenberg1816a2b2010-11-11 15:19:07 +0100512 int max_bio_size;
Lars Ellenberge65f4402010-11-05 10:04:07 +0100513 int number, rollback_i, size;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700514 int align, queued, sndbuf;
Lars Ellenberg0f0601f2010-08-11 23:40:24 +0200515 int i = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700516
517 if (unlikely(cancel))
518 return 1;
519
Lars Ellenbergaf85e8e2010-10-07 16:07:55 +0200520 if (mdev->rs_total == 0) {
521 /* empty resync? */
522 drbd_resync_finished(mdev);
523 return 1;
524 }
525
Philipp Reisnerb411b362009-09-25 16:07:19 -0700526 if (!get_ldev(mdev)) {
527 /* Since we only need to access mdev->rsync a
528 get_ldev_if_state(mdev,D_FAILED) would be sufficient, but
529 to continue resync with a broken disk makes no sense at
530 all */
531 dev_err(DEV, "Disk broke down during resync!\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700532 return 1;
533 }
534
Lars Ellenbergbb3d0002010-05-14 19:08:55 +0200535 /* starting with drbd 8.3.8, we can handle multi-bio EEs,
536 * if it should be necessary */
Lars Ellenberg1816a2b2010-11-11 15:19:07 +0100537 max_bio_size =
538 mdev->agreed_pro_version < 94 ? queue_max_hw_sectors(mdev->rq_queue) << 9 :
539 mdev->agreed_pro_version < 95 ? DRBD_MAX_SIZE_H80_PACKET : DRBD_MAX_BIO_SIZE;
Lars Ellenbergbb3d0002010-05-14 19:08:55 +0200540
Lars Ellenberge65f4402010-11-05 10:04:07 +0100541 number = drbd_rs_number_requests(mdev);
542 if (number == 0)
Lars Ellenberg0f0601f2010-08-11 23:40:24 +0200543 goto requeue;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700544
Philipp Reisnerb411b362009-09-25 16:07:19 -0700545 for (i = 0; i < number; i++) {
546 /* Stop generating RS requests, when half of the send buffer is filled */
547 mutex_lock(&mdev->data.mutex);
548 if (mdev->data.socket) {
549 queued = mdev->data.socket->sk->sk_wmem_queued;
550 sndbuf = mdev->data.socket->sk->sk_sndbuf;
551 } else {
552 queued = 1;
553 sndbuf = 0;
554 }
555 mutex_unlock(&mdev->data.mutex);
556 if (queued > sndbuf / 2)
557 goto requeue;
558
559next_sector:
560 size = BM_BLOCK_SIZE;
561 bit = drbd_bm_find_next(mdev, mdev->bm_resync_fo);
562
Lars Ellenberg4b0715f2010-12-14 15:13:04 +0100563 if (bit == DRBD_END_OF_BITMAP) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700564 mdev->bm_resync_fo = drbd_bm_bits(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700565 put_ldev(mdev);
566 return 1;
567 }
568
569 sector = BM_BIT_TO_SECT(bit);
570
Philipp Reisnere3555d82010-11-07 15:56:29 +0100571 if (drbd_rs_should_slow_down(mdev, sector) ||
572 drbd_try_rs_begin_io(mdev, sector)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700573 mdev->bm_resync_fo = bit;
574 goto requeue;
575 }
576 mdev->bm_resync_fo = bit + 1;
577
578 if (unlikely(drbd_bm_test_bit(mdev, bit) == 0)) {
579 drbd_rs_complete_io(mdev, sector);
580 goto next_sector;
581 }
582
Lars Ellenberg1816a2b2010-11-11 15:19:07 +0100583#if DRBD_MAX_BIO_SIZE > BM_BLOCK_SIZE
Philipp Reisnerb411b362009-09-25 16:07:19 -0700584 /* try to find some adjacent bits.
585 * we stop if we have already the maximum req size.
586 *
587 * Additionally always align bigger requests, in order to
588 * be prepared for all stripe sizes of software RAIDs.
Philipp Reisnerb411b362009-09-25 16:07:19 -0700589 */
590 align = 1;
Philipp Reisnerd2074502010-07-22 15:27:27 +0200591 rollback_i = i;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700592 for (;;) {
Lars Ellenberg1816a2b2010-11-11 15:19:07 +0100593 if (size + BM_BLOCK_SIZE > max_bio_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700594 break;
595
596 /* Be always aligned */
597 if (sector & ((1<<(align+3))-1))
598 break;
599
600 /* do not cross extent boundaries */
601 if (((bit+1) & BM_BLOCKS_PER_BM_EXT_MASK) == 0)
602 break;
603 /* now, is it actually dirty, after all?
604 * caution, drbd_bm_test_bit is tri-state for some
605 * obscure reason; ( b == 0 ) would get the out-of-band
606 * only accidentally right because of the "oddly sized"
607 * adjustment below */
608 if (drbd_bm_test_bit(mdev, bit+1) != 1)
609 break;
610 bit++;
611 size += BM_BLOCK_SIZE;
612 if ((BM_BLOCK_SIZE << align) <= size)
613 align++;
614 i++;
615 }
616 /* if we merged some,
617 * reset the offset to start the next drbd_bm_find_next from */
618 if (size > BM_BLOCK_SIZE)
619 mdev->bm_resync_fo = bit + 1;
620#endif
621
622 /* adjust very last sectors, in case we are oddly sized */
623 if (sector + (size>>9) > capacity)
624 size = (capacity-sector)<<9;
625 if (mdev->agreed_pro_version >= 89 && mdev->csums_tfm) {
626 switch (read_for_csum(mdev, sector, size)) {
Lars Ellenberg80a40e42010-08-11 23:28:00 +0200627 case -EIO: /* Disk failure */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700628 put_ldev(mdev);
629 return 0;
Lars Ellenberg80a40e42010-08-11 23:28:00 +0200630 case -EAGAIN: /* allocation failed, or ldev busy */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700631 drbd_rs_complete_io(mdev, sector);
632 mdev->bm_resync_fo = BM_SECT_TO_BIT(sector);
Philipp Reisnerd2074502010-07-22 15:27:27 +0200633 i = rollback_i;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700634 goto requeue;
Lars Ellenberg80a40e42010-08-11 23:28:00 +0200635 case 0:
636 /* everything ok */
637 break;
638 default:
639 BUG();
Philipp Reisnerb411b362009-09-25 16:07:19 -0700640 }
641 } else {
642 inc_rs_pending(mdev);
643 if (!drbd_send_drequest(mdev, P_RS_DATA_REQUEST,
644 sector, size, ID_SYNCER)) {
645 dev_err(DEV, "drbd_send_drequest() failed, aborting...\n");
646 dec_rs_pending(mdev);
647 put_ldev(mdev);
648 return 0;
649 }
650 }
651 }
652
653 if (mdev->bm_resync_fo >= drbd_bm_bits(mdev)) {
654 /* last syncer _request_ was sent,
655 * but the P_RS_DATA_REPLY not yet received. sync will end (and
656 * next sync group will resume), as soon as we receive the last
657 * resync data block, and the last bit is cleared.
658 * until then resync "work" is "inactive" ...
659 */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700660 put_ldev(mdev);
661 return 1;
662 }
663
664 requeue:
Philipp Reisner778f2712010-07-06 11:14:00 +0200665 mdev->rs_in_flight += (i << (BM_BLOCK_SHIFT - 9));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700666 mod_timer(&mdev->resync_timer, jiffies + SLEEP_TIME);
667 put_ldev(mdev);
668 return 1;
669}
670
671static int w_make_ov_request(struct drbd_conf *mdev, struct drbd_work *w, int cancel)
672{
673 int number, i, size;
674 sector_t sector;
675 const sector_t capacity = drbd_get_capacity(mdev->this_bdev);
676
677 if (unlikely(cancel))
678 return 1;
679
Lars Ellenberg2649f082010-11-05 10:05:47 +0100680 number = drbd_rs_number_requests(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700681
682 sector = mdev->ov_position;
683 for (i = 0; i < number; i++) {
684 if (sector >= capacity) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700685 return 1;
686 }
687
688 size = BM_BLOCK_SIZE;
689
Philipp Reisnere3555d82010-11-07 15:56:29 +0100690 if (drbd_rs_should_slow_down(mdev, sector) ||
691 drbd_try_rs_begin_io(mdev, sector)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700692 mdev->ov_position = sector;
693 goto requeue;
694 }
695
696 if (sector + (size>>9) > capacity)
697 size = (capacity-sector)<<9;
698
699 inc_rs_pending(mdev);
700 if (!drbd_send_ov_request(mdev, sector, size)) {
701 dec_rs_pending(mdev);
702 return 0;
703 }
704 sector += BM_SECT_PER_BIT;
705 }
706 mdev->ov_position = sector;
707
708 requeue:
Lars Ellenberg2649f082010-11-05 10:05:47 +0100709 mdev->rs_in_flight += (i << (BM_BLOCK_SHIFT - 9));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700710 mod_timer(&mdev->resync_timer, jiffies + SLEEP_TIME);
711 return 1;
712}
713
Philipp Reisnerc4752ef2010-10-27 17:32:36 +0200714
Philipp Reisner370a43e2011-01-14 16:03:11 +0100715void start_resync_timer_fn(unsigned long data)
716{
717 struct drbd_conf *mdev = (struct drbd_conf *) data;
718
719 drbd_queue_work(&mdev->data.work, &mdev->start_resync_work);
720}
721
Philipp Reisnerc4752ef2010-10-27 17:32:36 +0200722int w_start_resync(struct drbd_conf *mdev, struct drbd_work *w, int cancel)
723{
Philipp Reisner370a43e2011-01-14 16:03:11 +0100724 if (atomic_read(&mdev->unacked_cnt) || atomic_read(&mdev->rs_pending_cnt)) {
725 dev_warn(DEV, "w_start_resync later...\n");
726 mdev->start_resync_timer.expires = jiffies + HZ/10;
727 add_timer(&mdev->start_resync_timer);
728 return 1;
729 }
Philipp Reisnerc4752ef2010-10-27 17:32:36 +0200730
Philipp Reisner370a43e2011-01-14 16:03:11 +0100731 drbd_start_resync(mdev, C_SYNC_SOURCE);
732 clear_bit(AHEAD_TO_SYNC_SOURCE, &mdev->current_epoch->flags);
Philipp Reisnerc4752ef2010-10-27 17:32:36 +0200733 return 1;
734}
735
Philipp Reisnerb411b362009-09-25 16:07:19 -0700736int w_ov_finished(struct drbd_conf *mdev, struct drbd_work *w, int cancel)
737{
738 kfree(w);
739 ov_oos_print(mdev);
740 drbd_resync_finished(mdev);
741
742 return 1;
743}
744
745static int w_resync_finished(struct drbd_conf *mdev, struct drbd_work *w, int cancel)
746{
747 kfree(w);
748
749 drbd_resync_finished(mdev);
750
751 return 1;
752}
753
Lars Ellenbergaf85e8e2010-10-07 16:07:55 +0200754static void ping_peer(struct drbd_conf *mdev)
755{
756 clear_bit(GOT_PING_ACK, &mdev->flags);
757 request_ping(mdev);
758 wait_event(mdev->misc_wait,
759 test_bit(GOT_PING_ACK, &mdev->flags) || mdev->state.conn < C_CONNECTED);
760}
761
Philipp Reisnerb411b362009-09-25 16:07:19 -0700762int drbd_resync_finished(struct drbd_conf *mdev)
763{
764 unsigned long db, dt, dbdt;
765 unsigned long n_oos;
766 union drbd_state os, ns;
767 struct drbd_work *w;
768 char *khelper_cmd = NULL;
Lars Ellenberg26525612010-11-05 09:56:33 +0100769 int verify_done = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700770
771 /* Remove all elements from the resync LRU. Since future actions
772 * might set bits in the (main) bitmap, then the entries in the
773 * resync LRU would be wrong. */
774 if (drbd_rs_del_all(mdev)) {
775 /* In case this is not possible now, most probably because
776 * there are P_RS_DATA_REPLY Packets lingering on the worker's
777 * queue (or even the read operations for those packets
778 * is not finished by now). Retry in 100ms. */
779
Philipp Reisner20ee6392011-01-18 15:28:59 +0100780 schedule_timeout_interruptible(HZ / 10);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700781 w = kmalloc(sizeof(struct drbd_work), GFP_ATOMIC);
782 if (w) {
783 w->cb = w_resync_finished;
784 drbd_queue_work(&mdev->data.work, w);
785 return 1;
786 }
787 dev_err(DEV, "Warn failed to drbd_rs_del_all() and to kmalloc(w).\n");
788 }
789
790 dt = (jiffies - mdev->rs_start - mdev->rs_paused) / HZ;
791 if (dt <= 0)
792 dt = 1;
793 db = mdev->rs_total;
794 dbdt = Bit2KB(db/dt);
795 mdev->rs_paused /= HZ;
796
797 if (!get_ldev(mdev))
798 goto out;
799
Lars Ellenbergaf85e8e2010-10-07 16:07:55 +0200800 ping_peer(mdev);
801
Philipp Reisnerb411b362009-09-25 16:07:19 -0700802 spin_lock_irq(&mdev->req_lock);
803 os = mdev->state;
804
Lars Ellenberg26525612010-11-05 09:56:33 +0100805 verify_done = (os.conn == C_VERIFY_S || os.conn == C_VERIFY_T);
806
Philipp Reisnerb411b362009-09-25 16:07:19 -0700807 /* This protects us against multiple calls (that can happen in the presence
808 of application IO), and against connectivity loss just before we arrive here. */
809 if (os.conn <= C_CONNECTED)
810 goto out_unlock;
811
812 ns = os;
813 ns.conn = C_CONNECTED;
814
815 dev_info(DEV, "%s done (total %lu sec; paused %lu sec; %lu K/sec)\n",
Lars Ellenberg26525612010-11-05 09:56:33 +0100816 verify_done ? "Online verify " : "Resync",
Philipp Reisnerb411b362009-09-25 16:07:19 -0700817 dt + mdev->rs_paused, mdev->rs_paused, dbdt);
818
819 n_oos = drbd_bm_total_weight(mdev);
820
821 if (os.conn == C_VERIFY_S || os.conn == C_VERIFY_T) {
822 if (n_oos) {
823 dev_alert(DEV, "Online verify found %lu %dk block out of sync!\n",
824 n_oos, Bit2KB(1));
825 khelper_cmd = "out-of-sync";
826 }
827 } else {
828 D_ASSERT((n_oos - mdev->rs_failed) == 0);
829
830 if (os.conn == C_SYNC_TARGET || os.conn == C_PAUSED_SYNC_T)
831 khelper_cmd = "after-resync-target";
832
833 if (mdev->csums_tfm && mdev->rs_total) {
834 const unsigned long s = mdev->rs_same_csum;
835 const unsigned long t = mdev->rs_total;
836 const int ratio =
837 (t == 0) ? 0 :
838 (t < 100000) ? ((s*100)/t) : (s/(t/100));
839 dev_info(DEV, "%u %% had equal check sums, eliminated: %luK; "
840 "transferred %luK total %luK\n",
841 ratio,
842 Bit2KB(mdev->rs_same_csum),
843 Bit2KB(mdev->rs_total - mdev->rs_same_csum),
844 Bit2KB(mdev->rs_total));
845 }
846 }
847
848 if (mdev->rs_failed) {
849 dev_info(DEV, " %lu failed blocks\n", mdev->rs_failed);
850
851 if (os.conn == C_SYNC_TARGET || os.conn == C_PAUSED_SYNC_T) {
852 ns.disk = D_INCONSISTENT;
853 ns.pdsk = D_UP_TO_DATE;
854 } else {
855 ns.disk = D_UP_TO_DATE;
856 ns.pdsk = D_INCONSISTENT;
857 }
858 } else {
859 ns.disk = D_UP_TO_DATE;
860 ns.pdsk = D_UP_TO_DATE;
861
862 if (os.conn == C_SYNC_TARGET || os.conn == C_PAUSED_SYNC_T) {
863 if (mdev->p_uuid) {
864 int i;
865 for (i = UI_BITMAP ; i <= UI_HISTORY_END ; i++)
866 _drbd_uuid_set(mdev, i, mdev->p_uuid[i]);
867 drbd_uuid_set(mdev, UI_BITMAP, mdev->ldev->md.uuid[UI_CURRENT]);
868 _drbd_uuid_set(mdev, UI_CURRENT, mdev->p_uuid[UI_CURRENT]);
869 } else {
870 dev_err(DEV, "mdev->p_uuid is NULL! BUG\n");
871 }
872 }
873
874 drbd_uuid_set_bm(mdev, 0UL);
875
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 }
883 }
884
885 _drbd_set_state(mdev, ns, CS_VERBOSE, NULL);
886out_unlock:
887 spin_unlock_irq(&mdev->req_lock);
888 put_ldev(mdev);
889out:
890 mdev->rs_total = 0;
891 mdev->rs_failed = 0;
892 mdev->rs_paused = 0;
Lars Ellenberg26525612010-11-05 09:56:33 +0100893 if (verify_done)
894 mdev->ov_start_sector = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700895
Lars Ellenberg13d42682010-10-13 17:37:54 +0200896 drbd_md_sync(mdev);
897
Philipp Reisnerb411b362009-09-25 16:07:19 -0700898 if (khelper_cmd)
899 drbd_khelper(mdev, khelper_cmd);
900
901 return 1;
902}
903
904/* helper */
905static void move_to_net_ee_or_free(struct drbd_conf *mdev, struct drbd_epoch_entry *e)
906{
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200907 if (drbd_ee_has_active_page(e)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700908 /* This might happen if sendpage() has not finished */
Lars Ellenberg78db8922010-09-13 13:27:10 +0200909 int i = (e->size + PAGE_SIZE -1) >> PAGE_SHIFT;
Lars Ellenberg435f0742010-09-06 12:30:25 +0200910 atomic_add(i, &mdev->pp_in_use_by_net);
911 atomic_sub(i, &mdev->pp_in_use);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700912 spin_lock_irq(&mdev->req_lock);
913 list_add_tail(&e->w.list, &mdev->net_ee);
914 spin_unlock_irq(&mdev->req_lock);
Lars Ellenberg435f0742010-09-06 12:30:25 +0200915 wake_up(&drbd_pp_wait);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700916 } else
917 drbd_free_ee(mdev, e);
918}
919
920/**
921 * w_e_end_data_req() - Worker callback, to send a P_DATA_REPLY packet in response to a P_DATA_REQUEST
922 * @mdev: DRBD device.
923 * @w: work object.
924 * @cancel: The connection will be closed anyways
925 */
926int w_e_end_data_req(struct drbd_conf *mdev, struct drbd_work *w, int cancel)
927{
928 struct drbd_epoch_entry *e = container_of(w, struct drbd_epoch_entry, w);
929 int ok;
930
931 if (unlikely(cancel)) {
932 drbd_free_ee(mdev, e);
933 dec_unacked(mdev);
934 return 1;
935 }
936
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200937 if (likely((e->flags & EE_WAS_ERROR) == 0)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700938 ok = drbd_send_block(mdev, P_DATA_REPLY, e);
939 } else {
940 if (__ratelimit(&drbd_ratelimit_state))
941 dev_err(DEV, "Sending NegDReply. sector=%llus.\n",
942 (unsigned long long)e->sector);
943
944 ok = drbd_send_ack(mdev, P_NEG_DREPLY, e);
945 }
946
947 dec_unacked(mdev);
948
949 move_to_net_ee_or_free(mdev, e);
950
951 if (unlikely(!ok))
952 dev_err(DEV, "drbd_send_block() failed\n");
953 return ok;
954}
955
956/**
957 * w_e_end_rsdata_req() - Worker callback to send a P_RS_DATA_REPLY packet in response to a P_RS_DATA_REQUESTRS
958 * @mdev: DRBD device.
959 * @w: work object.
960 * @cancel: The connection will be closed anyways
961 */
962int w_e_end_rsdata_req(struct drbd_conf *mdev, struct drbd_work *w, int cancel)
963{
964 struct drbd_epoch_entry *e = container_of(w, struct drbd_epoch_entry, w);
965 int ok;
966
967 if (unlikely(cancel)) {
968 drbd_free_ee(mdev, e);
969 dec_unacked(mdev);
970 return 1;
971 }
972
973 if (get_ldev_if_state(mdev, D_FAILED)) {
974 drbd_rs_complete_io(mdev, e->sector);
975 put_ldev(mdev);
976 }
977
Philipp Reisnerd612d302010-12-27 10:53:28 +0100978 if (mdev->state.conn == C_AHEAD) {
979 ok = drbd_send_ack(mdev, P_RS_CANCEL, e);
980 } else if (likely((e->flags & EE_WAS_ERROR) == 0)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700981 if (likely(mdev->state.pdsk >= D_INCONSISTENT)) {
982 inc_rs_pending(mdev);
983 ok = drbd_send_block(mdev, P_RS_DATA_REPLY, e);
984 } else {
985 if (__ratelimit(&drbd_ratelimit_state))
986 dev_err(DEV, "Not sending RSDataReply, "
987 "partner DISKLESS!\n");
988 ok = 1;
989 }
990 } else {
991 if (__ratelimit(&drbd_ratelimit_state))
992 dev_err(DEV, "Sending NegRSDReply. sector %llus.\n",
993 (unsigned long long)e->sector);
994
995 ok = drbd_send_ack(mdev, P_NEG_RS_DREPLY, e);
996
997 /* update resync data with failure */
998 drbd_rs_failed_io(mdev, e->sector, e->size);
999 }
1000
1001 dec_unacked(mdev);
1002
1003 move_to_net_ee_or_free(mdev, e);
1004
1005 if (unlikely(!ok))
1006 dev_err(DEV, "drbd_send_block() failed\n");
1007 return ok;
1008}
1009
1010int w_e_end_csum_rs_req(struct drbd_conf *mdev, struct drbd_work *w, int cancel)
1011{
1012 struct drbd_epoch_entry *e = container_of(w, struct drbd_epoch_entry, w);
1013 struct digest_info *di;
1014 int digest_size;
1015 void *digest = NULL;
1016 int ok, eq = 0;
1017
1018 if (unlikely(cancel)) {
1019 drbd_free_ee(mdev, e);
1020 dec_unacked(mdev);
1021 return 1;
1022 }
1023
Lars Ellenberg1d53f092010-09-05 01:13:24 +02001024 if (get_ldev(mdev)) {
1025 drbd_rs_complete_io(mdev, e->sector);
1026 put_ldev(mdev);
1027 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001028
Philipp Reisner85719572010-07-21 10:20:17 +02001029 di = e->digest;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001030
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001031 if (likely((e->flags & EE_WAS_ERROR) == 0)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001032 /* quick hack to try to avoid a race against reconfiguration.
1033 * a real fix would be much more involved,
1034 * introducing more locking mechanisms */
1035 if (mdev->csums_tfm) {
1036 digest_size = crypto_hash_digestsize(mdev->csums_tfm);
1037 D_ASSERT(digest_size == di->digest_size);
1038 digest = kmalloc(digest_size, GFP_NOIO);
1039 }
1040 if (digest) {
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001041 drbd_csum_ee(mdev, mdev->csums_tfm, e, digest);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001042 eq = !memcmp(digest, di->digest, digest_size);
1043 kfree(digest);
1044 }
1045
1046 if (eq) {
1047 drbd_set_in_sync(mdev, e->sector, e->size);
Lars Ellenberg676396d2010-03-03 02:08:22 +01001048 /* rs_same_csums unit is BM_BLOCK_SIZE */
1049 mdev->rs_same_csum += e->size >> BM_BLOCK_SHIFT;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001050 ok = drbd_send_ack(mdev, P_RS_IS_IN_SYNC, e);
1051 } else {
1052 inc_rs_pending(mdev);
Philipp Reisner204bba92010-08-23 16:17:13 +02001053 e->block_id = ID_SYNCER; /* By setting block_id, digest pointer becomes invalid! */
1054 e->flags &= ~EE_HAS_DIGEST; /* This e no longer has a digest pointer */
1055 kfree(di);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001056 ok = drbd_send_block(mdev, P_RS_DATA_REPLY, e);
1057 }
1058 } else {
1059 ok = drbd_send_ack(mdev, P_NEG_RS_DREPLY, e);
1060 if (__ratelimit(&drbd_ratelimit_state))
1061 dev_err(DEV, "Sending NegDReply. I guess it gets messy.\n");
1062 }
1063
1064 dec_unacked(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001065 move_to_net_ee_or_free(mdev, e);
1066
1067 if (unlikely(!ok))
1068 dev_err(DEV, "drbd_send_block/ack() failed\n");
1069 return ok;
1070}
1071
1072int w_e_end_ov_req(struct drbd_conf *mdev, struct drbd_work *w, int cancel)
1073{
1074 struct drbd_epoch_entry *e = container_of(w, struct drbd_epoch_entry, w);
1075 int digest_size;
1076 void *digest;
1077 int ok = 1;
1078
1079 if (unlikely(cancel))
1080 goto out;
1081
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001082 if (unlikely((e->flags & EE_WAS_ERROR) != 0))
Philipp Reisnerb411b362009-09-25 16:07:19 -07001083 goto out;
1084
1085 digest_size = crypto_hash_digestsize(mdev->verify_tfm);
1086 /* FIXME if this allocation fails, online verify will not terminate! */
1087 digest = kmalloc(digest_size, GFP_NOIO);
1088 if (digest) {
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001089 drbd_csum_ee(mdev, mdev->verify_tfm, e, digest);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001090 inc_rs_pending(mdev);
1091 ok = drbd_send_drequest_csum(mdev, e->sector, e->size,
1092 digest, digest_size, P_OV_REPLY);
1093 if (!ok)
1094 dec_rs_pending(mdev);
1095 kfree(digest);
1096 }
1097
1098out:
1099 drbd_free_ee(mdev, e);
1100
1101 dec_unacked(mdev);
1102
1103 return ok;
1104}
1105
1106void drbd_ov_oos_found(struct drbd_conf *mdev, sector_t sector, int size)
1107{
1108 if (mdev->ov_last_oos_start + mdev->ov_last_oos_size == sector) {
1109 mdev->ov_last_oos_size += size>>9;
1110 } else {
1111 mdev->ov_last_oos_start = sector;
1112 mdev->ov_last_oos_size = size>>9;
1113 }
1114 drbd_set_out_of_sync(mdev, sector, size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001115}
1116
1117int w_e_end_ov_reply(struct drbd_conf *mdev, struct drbd_work *w, int cancel)
1118{
1119 struct drbd_epoch_entry *e = container_of(w, struct drbd_epoch_entry, w);
1120 struct digest_info *di;
1121 int digest_size;
1122 void *digest;
1123 int ok, eq = 0;
1124
1125 if (unlikely(cancel)) {
1126 drbd_free_ee(mdev, e);
1127 dec_unacked(mdev);
1128 return 1;
1129 }
1130
1131 /* after "cancel", because after drbd_disconnect/drbd_rs_cancel_all
1132 * the resync lru has been cleaned up already */
Lars Ellenberg1d53f092010-09-05 01:13:24 +02001133 if (get_ldev(mdev)) {
1134 drbd_rs_complete_io(mdev, e->sector);
1135 put_ldev(mdev);
1136 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001137
Philipp Reisner85719572010-07-21 10:20:17 +02001138 di = e->digest;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001139
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001140 if (likely((e->flags & EE_WAS_ERROR) == 0)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001141 digest_size = crypto_hash_digestsize(mdev->verify_tfm);
1142 digest = kmalloc(digest_size, GFP_NOIO);
1143 if (digest) {
Lars Ellenberg45bb9122010-05-14 17:10:48 +02001144 drbd_csum_ee(mdev, mdev->verify_tfm, e, digest);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001145
1146 D_ASSERT(digest_size == di->digest_size);
1147 eq = !memcmp(digest, di->digest, digest_size);
1148 kfree(digest);
1149 }
1150 } else {
1151 ok = drbd_send_ack(mdev, P_NEG_RS_DREPLY, e);
1152 if (__ratelimit(&drbd_ratelimit_state))
1153 dev_err(DEV, "Sending NegDReply. I guess it gets messy.\n");
1154 }
1155
1156 dec_unacked(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001157 if (!eq)
1158 drbd_ov_oos_found(mdev, e->sector, e->size);
1159 else
1160 ov_oos_print(mdev);
1161
1162 ok = drbd_send_ack_ex(mdev, P_OV_RESULT, e->sector, e->size,
1163 eq ? ID_IN_SYNC : ID_OUT_OF_SYNC);
1164
1165 drbd_free_ee(mdev, e);
1166
Lars Ellenbergea5442a2010-11-05 09:48:01 +01001167 --mdev->ov_left;
1168
1169 /* let's advance progress step marks only for every other megabyte */
1170 if ((mdev->ov_left & 0x200) == 0x200)
1171 drbd_advance_rs_marks(mdev, mdev->ov_left);
1172
1173 if (mdev->ov_left == 0) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001174 ov_oos_print(mdev);
1175 drbd_resync_finished(mdev);
1176 }
1177
1178 return ok;
1179}
1180
1181int w_prev_work_done(struct drbd_conf *mdev, struct drbd_work *w, int cancel)
1182{
1183 struct drbd_wq_barrier *b = container_of(w, struct drbd_wq_barrier, w);
1184 complete(&b->done);
1185 return 1;
1186}
1187
1188int w_send_barrier(struct drbd_conf *mdev, struct drbd_work *w, int cancel)
1189{
1190 struct drbd_tl_epoch *b = container_of(w, struct drbd_tl_epoch, w);
1191 struct p_barrier *p = &mdev->data.sbuf.barrier;
1192 int ok = 1;
1193
1194 /* really avoid racing with tl_clear. w.cb may have been referenced
1195 * just before it was reassigned and re-queued, so double check that.
1196 * actually, this race was harmless, since we only try to send the
1197 * barrier packet here, and otherwise do nothing with the object.
1198 * but compare with the head of w_clear_epoch */
1199 spin_lock_irq(&mdev->req_lock);
1200 if (w->cb != w_send_barrier || mdev->state.conn < C_CONNECTED)
1201 cancel = 1;
1202 spin_unlock_irq(&mdev->req_lock);
1203 if (cancel)
1204 return 1;
1205
1206 if (!drbd_get_data_sock(mdev))
1207 return 0;
1208 p->barrier = b->br_number;
1209 /* inc_ap_pending was done where this was queued.
1210 * dec_ap_pending will be done in got_BarrierAck
1211 * or (on connection loss) in w_clear_epoch. */
1212 ok = _drbd_send_cmd(mdev, mdev->data.socket, P_BARRIER,
Philipp Reisner0b70a132010-08-20 13:36:10 +02001213 (struct p_header80 *)p, sizeof(*p), 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001214 drbd_put_data_sock(mdev);
1215
1216 return ok;
1217}
1218
1219int w_send_write_hint(struct drbd_conf *mdev, struct drbd_work *w, int cancel)
1220{
1221 if (cancel)
1222 return 1;
1223 return drbd_send_short_cmd(mdev, P_UNPLUG_REMOTE);
1224}
1225
Philipp Reisner73a01a12010-10-27 14:33:00 +02001226int w_send_oos(struct drbd_conf *mdev, struct drbd_work *w, int cancel)
1227{
1228 struct drbd_request *req = container_of(w, struct drbd_request, w);
1229 int ok;
1230
1231 if (unlikely(cancel)) {
1232 req_mod(req, send_canceled);
1233 return 1;
1234 }
1235
1236 ok = drbd_send_oos(mdev, req);
1237 req_mod(req, oos_handed_to_network);
1238
1239 return ok;
1240}
1241
Philipp Reisnerb411b362009-09-25 16:07:19 -07001242/**
1243 * w_send_dblock() - Worker callback to send a P_DATA packet in order to mirror a write request
1244 * @mdev: DRBD device.
1245 * @w: work object.
1246 * @cancel: The connection will be closed anyways
1247 */
1248int w_send_dblock(struct drbd_conf *mdev, struct drbd_work *w, int cancel)
1249{
1250 struct drbd_request *req = container_of(w, struct drbd_request, w);
1251 int ok;
1252
1253 if (unlikely(cancel)) {
1254 req_mod(req, send_canceled);
1255 return 1;
1256 }
1257
1258 ok = drbd_send_dblock(mdev, req);
1259 req_mod(req, ok ? handed_over_to_network : send_failed);
1260
1261 return ok;
1262}
1263
1264/**
1265 * w_send_read_req() - Worker callback to send a read request (P_DATA_REQUEST) packet
1266 * @mdev: DRBD device.
1267 * @w: work object.
1268 * @cancel: The connection will be closed anyways
1269 */
1270int w_send_read_req(struct drbd_conf *mdev, struct drbd_work *w, int cancel)
1271{
1272 struct drbd_request *req = container_of(w, struct drbd_request, w);
1273 int ok;
1274
1275 if (unlikely(cancel)) {
1276 req_mod(req, send_canceled);
1277 return 1;
1278 }
1279
1280 ok = drbd_send_drequest(mdev, P_DATA_REQUEST, req->sector, req->size,
1281 (unsigned long)req);
1282
1283 if (!ok) {
1284 /* ?? we set C_TIMEOUT or C_BROKEN_PIPE in drbd_send();
1285 * so this is probably redundant */
1286 if (mdev->state.conn >= C_CONNECTED)
1287 drbd_force_state(mdev, NS(conn, C_NETWORK_FAILURE));
1288 }
1289 req_mod(req, ok ? handed_over_to_network : send_failed);
1290
1291 return ok;
1292}
1293
Philipp Reisner265be2d2010-05-31 10:14:17 +02001294int w_restart_disk_io(struct drbd_conf *mdev, struct drbd_work *w, int cancel)
1295{
1296 struct drbd_request *req = container_of(w, struct drbd_request, w);
1297
Philipp Reisner07782862010-08-31 12:00:50 +02001298 if (bio_data_dir(req->master_bio) == WRITE && req->rq_state & RQ_IN_ACT_LOG)
Philipp Reisner265be2d2010-05-31 10:14:17 +02001299 drbd_al_begin_io(mdev, req->sector);
1300 /* Calling drbd_al_begin_io() out of the worker might deadlocks
1301 theoretically. Practically it can not deadlock, since this is
1302 only used when unfreezing IOs. All the extents of the requests
1303 that made it into the TL are already active */
1304
1305 drbd_req_make_private_bio(req, req->master_bio);
1306 req->private_bio->bi_bdev = mdev->ldev->backing_bdev;
1307 generic_make_request(req->private_bio);
1308
1309 return 1;
1310}
1311
Philipp Reisnerb411b362009-09-25 16:07:19 -07001312static int _drbd_may_sync_now(struct drbd_conf *mdev)
1313{
1314 struct drbd_conf *odev = mdev;
1315
1316 while (1) {
1317 if (odev->sync_conf.after == -1)
1318 return 1;
1319 odev = minor_to_mdev(odev->sync_conf.after);
1320 ERR_IF(!odev) return 1;
1321 if ((odev->state.conn >= C_SYNC_SOURCE &&
1322 odev->state.conn <= C_PAUSED_SYNC_T) ||
1323 odev->state.aftr_isp || odev->state.peer_isp ||
1324 odev->state.user_isp)
1325 return 0;
1326 }
1327}
1328
1329/**
1330 * _drbd_pause_after() - Pause resync on all devices that may not resync now
1331 * @mdev: DRBD device.
1332 *
1333 * Called from process context only (admin command and after_state_ch).
1334 */
1335static int _drbd_pause_after(struct drbd_conf *mdev)
1336{
1337 struct drbd_conf *odev;
1338 int i, rv = 0;
1339
1340 for (i = 0; i < minor_count; i++) {
1341 odev = minor_to_mdev(i);
1342 if (!odev)
1343 continue;
1344 if (odev->state.conn == C_STANDALONE && odev->state.disk == D_DISKLESS)
1345 continue;
1346 if (!_drbd_may_sync_now(odev))
1347 rv |= (__drbd_set_state(_NS(odev, aftr_isp, 1), CS_HARD, NULL)
1348 != SS_NOTHING_TO_DO);
1349 }
1350
1351 return rv;
1352}
1353
1354/**
1355 * _drbd_resume_next() - Resume resync on all devices that may resync now
1356 * @mdev: DRBD device.
1357 *
1358 * Called from process context only (admin command and worker).
1359 */
1360static int _drbd_resume_next(struct drbd_conf *mdev)
1361{
1362 struct drbd_conf *odev;
1363 int i, rv = 0;
1364
1365 for (i = 0; i < minor_count; i++) {
1366 odev = minor_to_mdev(i);
1367 if (!odev)
1368 continue;
1369 if (odev->state.conn == C_STANDALONE && odev->state.disk == D_DISKLESS)
1370 continue;
1371 if (odev->state.aftr_isp) {
1372 if (_drbd_may_sync_now(odev))
1373 rv |= (__drbd_set_state(_NS(odev, aftr_isp, 0),
1374 CS_HARD, NULL)
1375 != SS_NOTHING_TO_DO) ;
1376 }
1377 }
1378 return rv;
1379}
1380
1381void resume_next_sg(struct drbd_conf *mdev)
1382{
1383 write_lock_irq(&global_state_lock);
1384 _drbd_resume_next(mdev);
1385 write_unlock_irq(&global_state_lock);
1386}
1387
1388void suspend_other_sg(struct drbd_conf *mdev)
1389{
1390 write_lock_irq(&global_state_lock);
1391 _drbd_pause_after(mdev);
1392 write_unlock_irq(&global_state_lock);
1393}
1394
1395static int sync_after_error(struct drbd_conf *mdev, int o_minor)
1396{
1397 struct drbd_conf *odev;
1398
1399 if (o_minor == -1)
1400 return NO_ERROR;
1401 if (o_minor < -1 || minor_to_mdev(o_minor) == NULL)
1402 return ERR_SYNC_AFTER;
1403
1404 /* check for loops */
1405 odev = minor_to_mdev(o_minor);
1406 while (1) {
1407 if (odev == mdev)
1408 return ERR_SYNC_AFTER_CYCLE;
1409
1410 /* dependency chain ends here, no cycles. */
1411 if (odev->sync_conf.after == -1)
1412 return NO_ERROR;
1413
1414 /* follow the dependency chain */
1415 odev = minor_to_mdev(odev->sync_conf.after);
1416 }
1417}
1418
1419int drbd_alter_sa(struct drbd_conf *mdev, int na)
1420{
1421 int changes;
1422 int retcode;
1423
1424 write_lock_irq(&global_state_lock);
1425 retcode = sync_after_error(mdev, na);
1426 if (retcode == NO_ERROR) {
1427 mdev->sync_conf.after = na;
1428 do {
1429 changes = _drbd_pause_after(mdev);
1430 changes |= _drbd_resume_next(mdev);
1431 } while (changes);
1432 }
1433 write_unlock_irq(&global_state_lock);
1434 return retcode;
1435}
1436
Lars Ellenberg9bd28d32010-11-05 09:55:18 +01001437void drbd_rs_controller_reset(struct drbd_conf *mdev)
1438{
1439 atomic_set(&mdev->rs_sect_in, 0);
1440 atomic_set(&mdev->rs_sect_ev, 0);
1441 mdev->rs_in_flight = 0;
1442 mdev->rs_planed = 0;
1443 spin_lock(&mdev->peer_seq_lock);
1444 fifo_set(&mdev->rs_plan_s, 0);
1445 spin_unlock(&mdev->peer_seq_lock);
1446}
1447
Philipp Reisnerb411b362009-09-25 16:07:19 -07001448/**
1449 * drbd_start_resync() - Start the resync process
1450 * @mdev: DRBD device.
1451 * @side: Either C_SYNC_SOURCE or C_SYNC_TARGET
1452 *
1453 * This function might bring you directly into one of the
1454 * C_PAUSED_SYNC_* states.
1455 */
1456void drbd_start_resync(struct drbd_conf *mdev, enum drbd_conns side)
1457{
1458 union drbd_state ns;
1459 int r;
1460
Philipp Reisnerc4752ef2010-10-27 17:32:36 +02001461 if (mdev->state.conn >= C_SYNC_SOURCE && mdev->state.conn < C_AHEAD) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001462 dev_err(DEV, "Resync already running!\n");
1463 return;
1464 }
1465
Philipp Reisner59817f42010-10-29 12:44:20 +02001466 if (mdev->state.conn < C_AHEAD) {
1467 /* In case a previous resync run was aborted by an IO error/detach on the peer. */
1468 drbd_rs_cancel_all(mdev);
1469 /* This should be done when we abort the resync. We definitely do not
1470 want to have this for connections going back and forth between
1471 Ahead/Behind and SyncSource/SyncTarget */
1472 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001473
1474 if (side == C_SYNC_TARGET) {
1475 /* Since application IO was locked out during C_WF_BITMAP_T and
1476 C_WF_SYNC_UUID we are still unmodified. Before going to C_SYNC_TARGET
1477 we check that we might make the data inconsistent. */
1478 r = drbd_khelper(mdev, "before-resync-target");
1479 r = (r >> 8) & 0xff;
1480 if (r > 0) {
1481 dev_info(DEV, "before-resync-target handler returned %d, "
1482 "dropping connection.\n", r);
1483 drbd_force_state(mdev, NS(conn, C_DISCONNECTING));
1484 return;
1485 }
Philipp Reisner09b9e792010-12-03 16:04:24 +01001486 } else /* C_SYNC_SOURCE */ {
1487 r = drbd_khelper(mdev, "before-resync-source");
1488 r = (r >> 8) & 0xff;
1489 if (r > 0) {
1490 if (r == 3) {
1491 dev_info(DEV, "before-resync-source handler returned %d, "
1492 "ignoring. Old userland tools?", r);
1493 } else {
1494 dev_info(DEV, "before-resync-source handler returned %d, "
1495 "dropping connection.\n", r);
1496 drbd_force_state(mdev, NS(conn, C_DISCONNECTING));
1497 return;
1498 }
1499 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001500 }
1501
1502 drbd_state_lock(mdev);
1503
1504 if (!get_ldev_if_state(mdev, D_NEGOTIATING)) {
1505 drbd_state_unlock(mdev);
1506 return;
1507 }
1508
Philipp Reisnerb411b362009-09-25 16:07:19 -07001509 write_lock_irq(&global_state_lock);
1510 ns = mdev->state;
1511
1512 ns.aftr_isp = !_drbd_may_sync_now(mdev);
1513
1514 ns.conn = side;
1515
1516 if (side == C_SYNC_TARGET)
1517 ns.disk = D_INCONSISTENT;
1518 else /* side == C_SYNC_SOURCE */
1519 ns.pdsk = D_INCONSISTENT;
1520
1521 r = __drbd_set_state(mdev, ns, CS_VERBOSE, NULL);
1522 ns = mdev->state;
1523
1524 if (ns.conn < C_CONNECTED)
1525 r = SS_UNKNOWN_ERROR;
1526
1527 if (r == SS_SUCCESS) {
Lars Ellenberg1d7734a2010-08-11 21:21:50 +02001528 unsigned long tw = drbd_bm_total_weight(mdev);
1529 unsigned long now = jiffies;
1530 int i;
1531
Philipp Reisnerb411b362009-09-25 16:07:19 -07001532 mdev->rs_failed = 0;
1533 mdev->rs_paused = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001534 mdev->rs_same_csum = 0;
Lars Ellenberg0f0601f2010-08-11 23:40:24 +02001535 mdev->rs_last_events = 0;
1536 mdev->rs_last_sect_ev = 0;
Lars Ellenberg1d7734a2010-08-11 21:21:50 +02001537 mdev->rs_total = tw;
1538 mdev->rs_start = now;
1539 for (i = 0; i < DRBD_SYNC_MARKS; i++) {
1540 mdev->rs_mark_left[i] = tw;
1541 mdev->rs_mark_time[i] = now;
1542 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001543 _drbd_pause_after(mdev);
1544 }
1545 write_unlock_irq(&global_state_lock);
Lars Ellenberg5a22db82010-12-17 21:14:23 +01001546
1547 if (side == C_SYNC_TARGET)
1548 mdev->bm_resync_fo = 0;
1549
1550 /* Since protocol 96, we must serialize drbd_gen_and_send_sync_uuid
1551 * with w_send_oos, or the sync target will get confused as to
1552 * how much bits to resync. We cannot do that always, because for an
1553 * empty resync and protocol < 95, we need to do it here, as we call
1554 * drbd_resync_finished from here in that case.
1555 * We drbd_gen_and_send_sync_uuid here for protocol < 96,
1556 * and from after_state_ch otherwise. */
1557 if (side == C_SYNC_SOURCE && mdev->agreed_pro_version < 96)
1558 drbd_gen_and_send_sync_uuid(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001559
1560 if (r == SS_SUCCESS) {
1561 dev_info(DEV, "Began resync as %s (will sync %lu KB [%lu bits set]).\n",
1562 drbd_conn_str(ns.conn),
1563 (unsigned long) mdev->rs_total << (BM_BLOCK_SHIFT-10),
1564 (unsigned long) mdev->rs_total);
1565
Lars Ellenbergaf85e8e2010-10-07 16:07:55 +02001566 if (mdev->agreed_pro_version < 95 && mdev->rs_total == 0) {
1567 /* This still has a race (about when exactly the peers
1568 * detect connection loss) that can lead to a full sync
1569 * on next handshake. In 8.3.9 we fixed this with explicit
1570 * resync-finished notifications, but the fix
1571 * introduces a protocol change. Sleeping for some
1572 * time longer than the ping interval + timeout on the
1573 * SyncSource, to give the SyncTarget the chance to
1574 * detect connection loss, then waiting for a ping
1575 * response (implicit in drbd_resync_finished) reduces
1576 * the race considerably, but does not solve it. */
1577 if (side == C_SYNC_SOURCE)
1578 schedule_timeout_interruptible(
1579 mdev->net_conf->ping_int * HZ +
1580 mdev->net_conf->ping_timeo*HZ/9);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001581 drbd_resync_finished(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001582 }
1583
Lars Ellenberg9bd28d32010-11-05 09:55:18 +01001584 drbd_rs_controller_reset(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001585 /* ns.conn may already be != mdev->state.conn,
1586 * we may have been paused in between, or become paused until
1587 * the timer triggers.
1588 * No matter, that is handled in resync_timer_fn() */
1589 if (ns.conn == C_SYNC_TARGET)
1590 mod_timer(&mdev->resync_timer, jiffies);
1591
1592 drbd_md_sync(mdev);
1593 }
Lars Ellenberg5a22db82010-12-17 21:14:23 +01001594 put_ldev(mdev);
Philipp Reisnerd0c3f602010-03-02 15:06:45 +01001595 drbd_state_unlock(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001596}
1597
1598int drbd_worker(struct drbd_thread *thi)
1599{
1600 struct drbd_conf *mdev = thi->mdev;
1601 struct drbd_work *w = NULL;
1602 LIST_HEAD(work_list);
1603 int intr = 0, i;
1604
1605 sprintf(current->comm, "drbd%d_worker", mdev_to_minor(mdev));
1606
1607 while (get_t_state(thi) == Running) {
1608 drbd_thread_current_set_cpu(mdev);
1609
1610 if (down_trylock(&mdev->data.work.s)) {
1611 mutex_lock(&mdev->data.mutex);
1612 if (mdev->data.socket && !mdev->net_conf->no_cork)
1613 drbd_tcp_uncork(mdev->data.socket);
1614 mutex_unlock(&mdev->data.mutex);
1615
1616 intr = down_interruptible(&mdev->data.work.s);
1617
1618 mutex_lock(&mdev->data.mutex);
1619 if (mdev->data.socket && !mdev->net_conf->no_cork)
1620 drbd_tcp_cork(mdev->data.socket);
1621 mutex_unlock(&mdev->data.mutex);
1622 }
1623
1624 if (intr) {
1625 D_ASSERT(intr == -EINTR);
1626 flush_signals(current);
1627 ERR_IF (get_t_state(thi) == Running)
1628 continue;
1629 break;
1630 }
1631
1632 if (get_t_state(thi) != Running)
1633 break;
1634 /* With this break, we have done a down() but not consumed
1635 the entry from the list. The cleanup code takes care of
1636 this... */
1637
1638 w = NULL;
1639 spin_lock_irq(&mdev->data.work.q_lock);
1640 ERR_IF(list_empty(&mdev->data.work.q)) {
1641 /* something terribly wrong in our logic.
1642 * we were able to down() the semaphore,
1643 * but the list is empty... doh.
1644 *
1645 * what is the best thing to do now?
1646 * try again from scratch, restarting the receiver,
1647 * asender, whatnot? could break even more ugly,
1648 * e.g. when we are primary, but no good local data.
1649 *
1650 * I'll try to get away just starting over this loop.
1651 */
1652 spin_unlock_irq(&mdev->data.work.q_lock);
1653 continue;
1654 }
1655 w = list_entry(mdev->data.work.q.next, struct drbd_work, list);
1656 list_del_init(&w->list);
1657 spin_unlock_irq(&mdev->data.work.q_lock);
1658
1659 if (!w->cb(mdev, w, mdev->state.conn < C_CONNECTED)) {
1660 /* dev_warn(DEV, "worker: a callback failed! \n"); */
1661 if (mdev->state.conn >= C_CONNECTED)
1662 drbd_force_state(mdev,
1663 NS(conn, C_NETWORK_FAILURE));
1664 }
1665 }
1666 D_ASSERT(test_bit(DEVICE_DYING, &mdev->flags));
1667 D_ASSERT(test_bit(CONFIG_PENDING, &mdev->flags));
1668
1669 spin_lock_irq(&mdev->data.work.q_lock);
1670 i = 0;
1671 while (!list_empty(&mdev->data.work.q)) {
1672 list_splice_init(&mdev->data.work.q, &work_list);
1673 spin_unlock_irq(&mdev->data.work.q_lock);
1674
1675 while (!list_empty(&work_list)) {
1676 w = list_entry(work_list.next, struct drbd_work, list);
1677 list_del_init(&w->list);
1678 w->cb(mdev, w, 1);
1679 i++; /* dead debugging code */
1680 }
1681
1682 spin_lock_irq(&mdev->data.work.q_lock);
1683 }
1684 sema_init(&mdev->data.work.s, 0);
1685 /* DANGEROUS race: if someone did queue his work within the spinlock,
1686 * but up() ed outside the spinlock, we could get an up() on the
1687 * semaphore without corresponding list entry.
1688 * So don't do that.
1689 */
1690 spin_unlock_irq(&mdev->data.work.q_lock);
1691
1692 D_ASSERT(mdev->state.disk == D_DISKLESS && mdev->state.conn == C_STANDALONE);
1693 /* _drbd_set_state only uses stop_nowait.
1694 * wait here for the Exiting receiver. */
1695 drbd_thread_stop(&mdev->receiver);
1696 drbd_mdev_cleanup(mdev);
1697
1698 dev_info(DEV, "worker terminated\n");
1699
1700 clear_bit(DEVICE_DYING, &mdev->flags);
1701 clear_bit(CONFIG_PENDING, &mdev->flags);
1702 wake_up(&mdev->state_wait);
1703
1704 return 0;
1705}