4 This file is part of DRBD by Philipp Reisner and Lars Ellenberg.
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>.
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)
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.
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.
26 #include <linux/module.h>
27 #include <linux/drbd.h>
28 #include <linux/sched.h>
29 #include <linux/wait.h>
31 #include <linux/memcontrol.h>
32 #include <linux/mm_inline.h>
33 #include <linux/slab.h>
34 #include <linux/random.h>
35 #include <linux/string.h>
36 #include <linux/scatterlist.h>
41 static int w_make_ov_request(struct drbd_work *w, int cancel);
45 * drbd_md_io_complete (defined here)
46 * drbd_request_endio (defined here)
47 * drbd_peer_request_endio (defined here)
48 * bm_async_io_complete (defined in drbd_bitmap.c)
50 * For all these callbacks, note the following:
51 * The callbacks will be called in irq context by the IDE drivers,
52 * and in Softirqs/Tasklets/BH context by the SCSI drivers.
53 * Try to get the locking right :)
58 /* About the global_state_lock
59 Each state transition on an device holds a read lock. In case we have
60 to evaluate the resync after dependencies, we grab a write lock, because
61 we need stable states on all devices for that. */
62 rwlock_t global_state_lock;
64 /* used for synchronous meta data and bitmap IO
65 * submitted by drbd_md_sync_page_io()
67 void drbd_md_io_complete(struct bio *bio, int error)
69 struct drbd_md_io *md_io;
70 struct drbd_conf *mdev;
72 md_io = (struct drbd_md_io *)bio->bi_private;
73 mdev = container_of(md_io, struct drbd_conf, md_io);
78 wake_up(&mdev->misc_wait);
80 drbd_md_put_buffer(mdev);
84 /* reads on behalf of the partner,
85 * "submitted" by the receiver
87 void drbd_endio_read_sec_final(struct drbd_peer_request *peer_req) __releases(local)
89 unsigned long flags = 0;
90 struct drbd_conf *mdev = peer_req->w.mdev;
92 spin_lock_irqsave(&mdev->tconn->req_lock, flags);
93 mdev->read_cnt += peer_req->i.size >> 9;
94 list_del(&peer_req->w.list);
95 if (list_empty(&mdev->read_ee))
96 wake_up(&mdev->ee_wait);
97 if (test_bit(__EE_WAS_ERROR, &peer_req->flags))
98 __drbd_chk_io_error(mdev, false);
99 spin_unlock_irqrestore(&mdev->tconn->req_lock, flags);
101 drbd_queue_work(&mdev->tconn->data.work, &peer_req->w);
105 /* writes on behalf of the partner, or resync writes,
106 * "submitted" by the receiver, final stage. */
107 static void drbd_endio_write_sec_final(struct drbd_peer_request *peer_req) __releases(local)
109 unsigned long flags = 0;
110 struct drbd_conf *mdev = peer_req->w.mdev;
111 struct drbd_interval i;
114 int do_al_complete_io;
116 /* after we moved peer_req 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) */
121 do_al_complete_io = peer_req->flags & EE_CALL_AL_COMPLETE_IO;
122 block_id = peer_req->block_id;
124 spin_lock_irqsave(&mdev->tconn->req_lock, flags);
125 mdev->writ_cnt += peer_req->i.size >> 9;
126 list_del(&peer_req->w.list); /* has been on active_ee or sync_ee */
127 list_add_tail(&peer_req->w.list, &mdev->done_ee);
130 * Do not remove from the write_requests tree here: we did not send the
131 * Ack yet and did not wake possibly waiting conflicting requests.
132 * Removed from the tree from "drbd_process_done_ee" within the
133 * appropriate w.cb (e_end_block/e_end_resync_block) or from
134 * _drbd_clear_done_ee.
137 do_wake = list_empty(block_id == ID_SYNCER ? &mdev->sync_ee : &mdev->active_ee);
139 if (test_bit(__EE_WAS_ERROR, &peer_req->flags))
140 __drbd_chk_io_error(mdev, false);
141 spin_unlock_irqrestore(&mdev->tconn->req_lock, flags);
143 if (block_id == ID_SYNCER)
144 drbd_rs_complete_io(mdev, i.sector);
147 wake_up(&mdev->ee_wait);
149 if (do_al_complete_io)
150 drbd_al_complete_io(mdev, &i);
152 wake_asender(mdev->tconn);
156 /* writes on behalf of the partner, or resync writes,
157 * "submitted" by the receiver.
159 void drbd_peer_request_endio(struct bio *bio, int error)
161 struct drbd_peer_request *peer_req = bio->bi_private;
162 struct drbd_conf *mdev = peer_req->w.mdev;
163 int uptodate = bio_flagged(bio, BIO_UPTODATE);
164 int is_write = bio_data_dir(bio) == WRITE;
166 if (error && __ratelimit(&drbd_ratelimit_state))
167 dev_warn(DEV, "%s: error=%d s=%llus\n",
168 is_write ? "write" : "read", error,
169 (unsigned long long)peer_req->i.sector);
170 if (!error && !uptodate) {
171 if (__ratelimit(&drbd_ratelimit_state))
172 dev_warn(DEV, "%s: setting error to -EIO s=%llus\n",
173 is_write ? "write" : "read",
174 (unsigned long long)peer_req->i.sector);
175 /* strange behavior of some lower level drivers...
176 * fail the request by clearing the uptodate flag,
177 * but do not return any error?! */
182 set_bit(__EE_WAS_ERROR, &peer_req->flags);
184 bio_put(bio); /* no need for the bio anymore */
185 if (atomic_dec_and_test(&peer_req->pending_bios)) {
187 drbd_endio_write_sec_final(peer_req);
189 drbd_endio_read_sec_final(peer_req);
193 /* read, readA or write requests on R_PRIMARY coming from drbd_make_request
195 void drbd_request_endio(struct bio *bio, int error)
198 struct drbd_request *req = bio->bi_private;
199 struct drbd_conf *mdev = req->w.mdev;
200 struct bio_and_error m;
201 enum drbd_req_event what;
202 int uptodate = bio_flagged(bio, BIO_UPTODATE);
204 if (!error && !uptodate) {
205 dev_warn(DEV, "p %s: setting error to -EIO\n",
206 bio_data_dir(bio) == WRITE ? "write" : "read");
207 /* strange behavior of some lower level drivers...
208 * fail the request by clearing the uptodate flag,
209 * but do not return any error?! */
213 /* to avoid recursion in __req_mod */
214 if (unlikely(error)) {
215 what = (bio_data_dir(bio) == WRITE)
216 ? WRITE_COMPLETED_WITH_ERROR
217 : (bio_rw(bio) == READ)
218 ? READ_COMPLETED_WITH_ERROR
219 : READ_AHEAD_COMPLETED_WITH_ERROR;
223 bio_put(req->private_bio);
224 req->private_bio = ERR_PTR(error);
226 /* not req_mod(), we need irqsave here! */
227 spin_lock_irqsave(&mdev->tconn->req_lock, flags);
228 __req_mod(req, what, &m);
229 spin_unlock_irqrestore(&mdev->tconn->req_lock, flags);
232 complete_master_bio(mdev, &m);
235 int w_read_retry_remote(struct drbd_work *w, int cancel)
237 struct drbd_request *req = container_of(w, struct drbd_request, w);
238 struct drbd_conf *mdev = w->mdev;
240 /* We should not detach for read io-error,
241 * but try to WRITE the P_DATA_REPLY to the failed location,
242 * to give the disk the chance to relocate that block */
244 spin_lock_irq(&mdev->tconn->req_lock);
245 if (cancel || mdev->state.pdsk != D_UP_TO_DATE) {
246 _req_mod(req, READ_RETRY_REMOTE_CANCELED);
247 spin_unlock_irq(&mdev->tconn->req_lock);
250 spin_unlock_irq(&mdev->tconn->req_lock);
252 return w_send_read_req(w, 0);
255 void drbd_csum_ee(struct drbd_conf *mdev, struct crypto_hash *tfm,
256 struct drbd_peer_request *peer_req, void *digest)
258 struct hash_desc desc;
259 struct scatterlist sg;
260 struct page *page = peer_req->pages;
267 sg_init_table(&sg, 1);
268 crypto_hash_init(&desc);
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);
276 /* and now the last, possibly only partially used page */
277 len = peer_req->i.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);
283 void drbd_csum_bio(struct drbd_conf *mdev, struct crypto_hash *tfm, struct bio *bio, void *digest)
285 struct hash_desc desc;
286 struct scatterlist sg;
287 struct bio_vec *bvec;
293 sg_init_table(&sg, 1);
294 crypto_hash_init(&desc);
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);
300 crypto_hash_final(&desc, digest);
303 /* MAYBE merge common code with w_e_end_ov_req */
304 static int w_e_send_csum(struct drbd_work *w, int cancel)
306 struct drbd_peer_request *peer_req = container_of(w, struct drbd_peer_request, w);
307 struct drbd_conf *mdev = w->mdev;
312 if (unlikely(cancel))
315 if (unlikely((peer_req->flags & EE_WAS_ERROR) != 0))
318 digest_size = crypto_hash_digestsize(mdev->tconn->csums_tfm);
319 digest = kmalloc(digest_size, GFP_NOIO);
321 sector_t sector = peer_req->i.sector;
322 unsigned int size = peer_req->i.size;
323 drbd_csum_ee(mdev, mdev->tconn->csums_tfm, peer_req, digest);
324 /* Free peer_req and pages before send.
325 * In case we block on congestion, we could otherwise run into
326 * some distributed deadlock, if the other side blocks on
327 * congestion as well, because our receiver blocks in
328 * drbd_alloc_pages due to pp_in_use > max_buffers. */
329 drbd_free_peer_req(mdev, peer_req);
331 inc_rs_pending(mdev);
332 err = drbd_send_drequest_csum(mdev, sector, size,
337 dev_err(DEV, "kmalloc() of digest failed.\n");
343 drbd_free_peer_req(mdev, peer_req);
346 dev_err(DEV, "drbd_send_drequest(..., csum) failed\n");
350 #define GFP_TRY (__GFP_HIGHMEM | __GFP_NOWARN)
352 static int read_for_csum(struct drbd_conf *mdev, sector_t sector, int size)
354 struct drbd_peer_request *peer_req;
359 if (drbd_rs_should_slow_down(mdev, sector))
362 /* GFP_TRY, because if there is no memory available right now, this may
363 * be rescheduled for later. It is "only" background resync, after all. */
364 peer_req = drbd_alloc_peer_req(mdev, ID_SYNCER /* unused */, sector,
369 peer_req->w.cb = w_e_send_csum;
370 spin_lock_irq(&mdev->tconn->req_lock);
371 list_add(&peer_req->w.list, &mdev->read_ee);
372 spin_unlock_irq(&mdev->tconn->req_lock);
374 atomic_add(size >> 9, &mdev->rs_sect_ev);
375 if (drbd_submit_peer_request(mdev, peer_req, READ, DRBD_FAULT_RS_RD) == 0)
378 /* If it failed because of ENOMEM, retry should help. If it failed
379 * because bio_add_page failed (probably broken lower level driver),
380 * retry may or may not help.
381 * If it does not, you may need to force disconnect. */
382 spin_lock_irq(&mdev->tconn->req_lock);
383 list_del(&peer_req->w.list);
384 spin_unlock_irq(&mdev->tconn->req_lock);
386 drbd_free_peer_req(mdev, peer_req);
392 int w_resync_timer(struct drbd_work *w, int cancel)
394 struct drbd_conf *mdev = w->mdev;
395 switch (mdev->state.conn) {
397 w_make_ov_request(w, cancel);
400 w_make_resync_request(w, cancel);
407 void resync_timer_fn(unsigned long data)
409 struct drbd_conf *mdev = (struct drbd_conf *) data;
411 if (list_empty(&mdev->resync_work.list))
412 drbd_queue_work(&mdev->tconn->data.work, &mdev->resync_work);
415 static void fifo_set(struct fifo_buffer *fb, int value)
419 for (i = 0; i < fb->size; i++)
420 fb->values[i] = value;
423 static int fifo_push(struct fifo_buffer *fb, int value)
427 ov = fb->values[fb->head_index];
428 fb->values[fb->head_index++] = value;
430 if (fb->head_index >= fb->size)
436 static void fifo_add_val(struct fifo_buffer *fb, int value)
440 for (i = 0; i < fb->size; i++)
441 fb->values[i] += value;
444 struct fifo_buffer *fifo_alloc(int fifo_size)
446 struct fifo_buffer *fb;
448 fb = kzalloc(sizeof(struct fifo_buffer) + sizeof(int) * fifo_size, GFP_KERNEL);
453 fb->size = fifo_size;
459 static int drbd_rs_controller(struct drbd_conf *mdev)
461 struct disk_conf *dc;
462 unsigned int sect_in; /* Number of sectors that came in since the last turn */
463 unsigned int want; /* The number of sectors we want in the proxy */
464 int req_sect; /* Number of sectors to request in this turn */
465 int correction; /* Number of sectors more we need in the proxy*/
466 int cps; /* correction per invocation of drbd_rs_controller() */
467 int steps; /* Number of time steps to plan ahead */
470 struct fifo_buffer *plan;
472 sect_in = atomic_xchg(&mdev->rs_sect_in, 0); /* Number of sectors that came in */
473 mdev->rs_in_flight -= sect_in;
475 dc = rcu_dereference(mdev->ldev->disk_conf);
476 plan = rcu_dereference(mdev->rs_plan_s);
478 steps = plan->size; /* (dc->c_plan_ahead * 10 * SLEEP_TIME) / HZ; */
480 if (mdev->rs_in_flight + sect_in == 0) { /* At start of resync */
481 want = ((dc->resync_rate * 2 * SLEEP_TIME) / HZ) * steps;
482 } else { /* normal path */
483 want = dc->c_fill_target ? dc->c_fill_target :
484 sect_in * dc->c_delay_target * HZ / (SLEEP_TIME * 10);
487 correction = want - mdev->rs_in_flight - plan->total;
490 cps = correction / steps;
491 fifo_add_val(plan, cps);
492 plan->total += cps * steps;
494 /* What we do in this step */
495 curr_corr = fifo_push(plan, 0);
496 plan->total -= curr_corr;
498 req_sect = sect_in + curr_corr;
502 max_sect = (dc->c_max_rate * 2 * SLEEP_TIME) / HZ;
503 if (req_sect > max_sect)
507 dev_warn(DEV, "si=%u if=%d wa=%u co=%d st=%d cps=%d pl=%d cc=%d rs=%d\n",
508 sect_in, mdev->rs_in_flight, want, correction,
509 steps, cps, mdev->rs_planed, curr_corr, req_sect);
515 static int drbd_rs_number_requests(struct drbd_conf *mdev)
520 if (rcu_dereference(mdev->rs_plan_s)->size) {
521 number = drbd_rs_controller(mdev) >> (BM_BLOCK_SHIFT - 9);
522 mdev->c_sync_rate = number * HZ * (BM_BLOCK_SIZE / 1024) / SLEEP_TIME;
524 mdev->c_sync_rate = rcu_dereference(mdev->ldev->disk_conf)->resync_rate;
525 number = SLEEP_TIME * mdev->c_sync_rate / ((BM_BLOCK_SIZE / 1024) * HZ);
529 /* ignore the amount of pending requests, the resync controller should
530 * throttle down to incoming reply rate soon enough anyways. */
534 int w_make_resync_request(struct drbd_work *w, int cancel)
536 struct drbd_conf *mdev = w->mdev;
539 const sector_t capacity = drbd_get_capacity(mdev->this_bdev);
541 int number, rollback_i, size;
542 int align, queued, sndbuf;
545 if (unlikely(cancel))
548 if (mdev->rs_total == 0) {
550 drbd_resync_finished(mdev);
554 if (!get_ldev(mdev)) {
555 /* Since we only need to access mdev->rsync a
556 get_ldev_if_state(mdev,D_FAILED) would be sufficient, but
557 to continue resync with a broken disk makes no sense at
559 dev_err(DEV, "Disk broke down during resync!\n");
563 max_bio_size = queue_max_hw_sectors(mdev->rq_queue) << 9;
564 number = drbd_rs_number_requests(mdev);
568 for (i = 0; i < number; i++) {
569 /* Stop generating RS requests, when half of the send buffer is filled */
570 mutex_lock(&mdev->tconn->data.mutex);
571 if (mdev->tconn->data.socket) {
572 queued = mdev->tconn->data.socket->sk->sk_wmem_queued;
573 sndbuf = mdev->tconn->data.socket->sk->sk_sndbuf;
578 mutex_unlock(&mdev->tconn->data.mutex);
579 if (queued > sndbuf / 2)
583 size = BM_BLOCK_SIZE;
584 bit = drbd_bm_find_next(mdev, mdev->bm_resync_fo);
586 if (bit == DRBD_END_OF_BITMAP) {
587 mdev->bm_resync_fo = drbd_bm_bits(mdev);
592 sector = BM_BIT_TO_SECT(bit);
594 if (drbd_rs_should_slow_down(mdev, sector) ||
595 drbd_try_rs_begin_io(mdev, sector)) {
596 mdev->bm_resync_fo = bit;
599 mdev->bm_resync_fo = bit + 1;
601 if (unlikely(drbd_bm_test_bit(mdev, bit) == 0)) {
602 drbd_rs_complete_io(mdev, sector);
606 #if DRBD_MAX_BIO_SIZE > BM_BLOCK_SIZE
607 /* try to find some adjacent bits.
608 * we stop if we have already the maximum req size.
610 * Additionally always align bigger requests, in order to
611 * be prepared for all stripe sizes of software RAIDs.
616 if (size + BM_BLOCK_SIZE > max_bio_size)
619 /* Be always aligned */
620 if (sector & ((1<<(align+3))-1))
623 /* do not cross extent boundaries */
624 if (((bit+1) & BM_BLOCKS_PER_BM_EXT_MASK) == 0)
626 /* now, is it actually dirty, after all?
627 * caution, drbd_bm_test_bit is tri-state for some
628 * obscure reason; ( b == 0 ) would get the out-of-band
629 * only accidentally right because of the "oddly sized"
630 * adjustment below */
631 if (drbd_bm_test_bit(mdev, bit+1) != 1)
634 size += BM_BLOCK_SIZE;
635 if ((BM_BLOCK_SIZE << align) <= size)
639 /* if we merged some,
640 * reset the offset to start the next drbd_bm_find_next from */
641 if (size > BM_BLOCK_SIZE)
642 mdev->bm_resync_fo = bit + 1;
645 /* adjust very last sectors, in case we are oddly sized */
646 if (sector + (size>>9) > capacity)
647 size = (capacity-sector)<<9;
648 if (mdev->tconn->agreed_pro_version >= 89 && mdev->tconn->csums_tfm) {
649 switch (read_for_csum(mdev, sector, size)) {
650 case -EIO: /* Disk failure */
653 case -EAGAIN: /* allocation failed, or ldev busy */
654 drbd_rs_complete_io(mdev, sector);
655 mdev->bm_resync_fo = BM_SECT_TO_BIT(sector);
667 inc_rs_pending(mdev);
668 err = drbd_send_drequest(mdev, P_RS_DATA_REQUEST,
669 sector, size, ID_SYNCER);
671 dev_err(DEV, "drbd_send_drequest() failed, aborting...\n");
672 dec_rs_pending(mdev);
679 if (mdev->bm_resync_fo >= drbd_bm_bits(mdev)) {
680 /* last syncer _request_ was sent,
681 * but the P_RS_DATA_REPLY not yet received. sync will end (and
682 * next sync group will resume), as soon as we receive the last
683 * resync data block, and the last bit is cleared.
684 * until then resync "work" is "inactive" ...
691 mdev->rs_in_flight += (i << (BM_BLOCK_SHIFT - 9));
692 mod_timer(&mdev->resync_timer, jiffies + SLEEP_TIME);
697 static int w_make_ov_request(struct drbd_work *w, int cancel)
699 struct drbd_conf *mdev = w->mdev;
702 const sector_t capacity = drbd_get_capacity(mdev->this_bdev);
704 if (unlikely(cancel))
707 number = drbd_rs_number_requests(mdev);
709 sector = mdev->ov_position;
710 for (i = 0; i < number; i++) {
711 if (sector >= capacity) {
715 size = BM_BLOCK_SIZE;
717 if (drbd_rs_should_slow_down(mdev, sector) ||
718 drbd_try_rs_begin_io(mdev, sector)) {
719 mdev->ov_position = sector;
723 if (sector + (size>>9) > capacity)
724 size = (capacity-sector)<<9;
726 inc_rs_pending(mdev);
727 if (drbd_send_ov_request(mdev, sector, size)) {
728 dec_rs_pending(mdev);
731 sector += BM_SECT_PER_BIT;
733 mdev->ov_position = sector;
736 mdev->rs_in_flight += (i << (BM_BLOCK_SHIFT - 9));
737 mod_timer(&mdev->resync_timer, jiffies + SLEEP_TIME);
741 int w_ov_finished(struct drbd_work *w, int cancel)
743 struct drbd_conf *mdev = w->mdev;
745 ov_out_of_sync_print(mdev);
746 drbd_resync_finished(mdev);
751 static int w_resync_finished(struct drbd_work *w, int cancel)
753 struct drbd_conf *mdev = w->mdev;
756 drbd_resync_finished(mdev);
761 static void ping_peer(struct drbd_conf *mdev)
763 struct drbd_tconn *tconn = mdev->tconn;
765 clear_bit(GOT_PING_ACK, &tconn->flags);
767 wait_event(tconn->ping_wait,
768 test_bit(GOT_PING_ACK, &tconn->flags) || mdev->state.conn < C_CONNECTED);
771 int drbd_resync_finished(struct drbd_conf *mdev)
773 unsigned long db, dt, dbdt;
775 union drbd_state os, ns;
777 char *khelper_cmd = NULL;
780 /* Remove all elements from the resync LRU. Since future actions
781 * might set bits in the (main) bitmap, then the entries in the
782 * resync LRU would be wrong. */
783 if (drbd_rs_del_all(mdev)) {
784 /* In case this is not possible now, most probably because
785 * there are P_RS_DATA_REPLY Packets lingering on the worker's
786 * queue (or even the read operations for those packets
787 * is not finished by now). Retry in 100ms. */
789 schedule_timeout_interruptible(HZ / 10);
790 w = kmalloc(sizeof(struct drbd_work), GFP_ATOMIC);
792 w->cb = w_resync_finished;
794 drbd_queue_work(&mdev->tconn->data.work, w);
797 dev_err(DEV, "Warn failed to drbd_rs_del_all() and to kmalloc(w).\n");
800 dt = (jiffies - mdev->rs_start - mdev->rs_paused) / HZ;
804 dbdt = Bit2KB(db/dt);
805 mdev->rs_paused /= HZ;
812 spin_lock_irq(&mdev->tconn->req_lock);
813 os = drbd_read_state(mdev);
815 verify_done = (os.conn == C_VERIFY_S || os.conn == C_VERIFY_T);
817 /* This protects us against multiple calls (that can happen in the presence
818 of application IO), and against connectivity loss just before we arrive here. */
819 if (os.conn <= C_CONNECTED)
823 ns.conn = C_CONNECTED;
825 dev_info(DEV, "%s done (total %lu sec; paused %lu sec; %lu K/sec)\n",
826 verify_done ? "Online verify " : "Resync",
827 dt + mdev->rs_paused, mdev->rs_paused, dbdt);
829 n_oos = drbd_bm_total_weight(mdev);
831 if (os.conn == C_VERIFY_S || os.conn == C_VERIFY_T) {
833 dev_alert(DEV, "Online verify found %lu %dk block out of sync!\n",
835 khelper_cmd = "out-of-sync";
838 D_ASSERT((n_oos - mdev->rs_failed) == 0);
840 if (os.conn == C_SYNC_TARGET || os.conn == C_PAUSED_SYNC_T)
841 khelper_cmd = "after-resync-target";
843 if (mdev->tconn->csums_tfm && mdev->rs_total) {
844 const unsigned long s = mdev->rs_same_csum;
845 const unsigned long t = mdev->rs_total;
848 (t < 100000) ? ((s*100)/t) : (s/(t/100));
849 dev_info(DEV, "%u %% had equal checksums, eliminated: %luK; "
850 "transferred %luK total %luK\n",
852 Bit2KB(mdev->rs_same_csum),
853 Bit2KB(mdev->rs_total - mdev->rs_same_csum),
854 Bit2KB(mdev->rs_total));
858 if (mdev->rs_failed) {
859 dev_info(DEV, " %lu failed blocks\n", mdev->rs_failed);
861 if (os.conn == C_SYNC_TARGET || os.conn == C_PAUSED_SYNC_T) {
862 ns.disk = D_INCONSISTENT;
863 ns.pdsk = D_UP_TO_DATE;
865 ns.disk = D_UP_TO_DATE;
866 ns.pdsk = D_INCONSISTENT;
869 ns.disk = D_UP_TO_DATE;
870 ns.pdsk = D_UP_TO_DATE;
872 if (os.conn == C_SYNC_TARGET || os.conn == C_PAUSED_SYNC_T) {
875 for (i = UI_BITMAP ; i <= UI_HISTORY_END ; i++)
876 _drbd_uuid_set(mdev, i, mdev->p_uuid[i]);
877 drbd_uuid_set(mdev, UI_BITMAP, mdev->ldev->md.uuid[UI_CURRENT]);
878 _drbd_uuid_set(mdev, UI_CURRENT, mdev->p_uuid[UI_CURRENT]);
880 dev_err(DEV, "mdev->p_uuid is NULL! BUG\n");
884 if (!(os.conn == C_VERIFY_S || os.conn == C_VERIFY_T)) {
885 /* for verify runs, we don't update uuids here,
886 * so there would be nothing to report. */
887 drbd_uuid_set_bm(mdev, 0UL);
888 drbd_print_uuids(mdev, "updated UUIDs");
890 /* Now the two UUID sets are equal, update what we
891 * know of the peer. */
893 for (i = UI_CURRENT ; i <= UI_HISTORY_END ; i++)
894 mdev->p_uuid[i] = mdev->ldev->md.uuid[i];
899 _drbd_set_state(mdev, ns, CS_VERBOSE, NULL);
901 spin_unlock_irq(&mdev->tconn->req_lock);
908 mdev->ov_start_sector = 0;
913 drbd_khelper(mdev, khelper_cmd);
919 static void move_to_net_ee_or_free(struct drbd_conf *mdev, struct drbd_peer_request *peer_req)
921 if (drbd_peer_req_has_active_page(peer_req)) {
922 /* This might happen if sendpage() has not finished */
923 int i = (peer_req->i.size + PAGE_SIZE -1) >> PAGE_SHIFT;
924 atomic_add(i, &mdev->pp_in_use_by_net);
925 atomic_sub(i, &mdev->pp_in_use);
926 spin_lock_irq(&mdev->tconn->req_lock);
927 list_add_tail(&peer_req->w.list, &mdev->net_ee);
928 spin_unlock_irq(&mdev->tconn->req_lock);
929 wake_up(&drbd_pp_wait);
931 drbd_free_peer_req(mdev, peer_req);
935 * w_e_end_data_req() - Worker callback, to send a P_DATA_REPLY packet in response to a P_DATA_REQUEST
936 * @mdev: DRBD device.
938 * @cancel: The connection will be closed anyways
940 int w_e_end_data_req(struct drbd_work *w, int cancel)
942 struct drbd_peer_request *peer_req = container_of(w, struct drbd_peer_request, w);
943 struct drbd_conf *mdev = w->mdev;
946 if (unlikely(cancel)) {
947 drbd_free_peer_req(mdev, peer_req);
952 if (likely((peer_req->flags & EE_WAS_ERROR) == 0)) {
953 err = drbd_send_block(mdev, P_DATA_REPLY, peer_req);
955 if (__ratelimit(&drbd_ratelimit_state))
956 dev_err(DEV, "Sending NegDReply. sector=%llus.\n",
957 (unsigned long long)peer_req->i.sector);
959 err = drbd_send_ack(mdev, P_NEG_DREPLY, peer_req);
964 move_to_net_ee_or_free(mdev, peer_req);
967 dev_err(DEV, "drbd_send_block() failed\n");
972 * w_e_end_rsdata_req() - Worker callback to send a P_RS_DATA_REPLY packet in response to a P_RS_DATA_REQUEST
973 * @mdev: DRBD device.
975 * @cancel: The connection will be closed anyways
977 int w_e_end_rsdata_req(struct drbd_work *w, int cancel)
979 struct drbd_peer_request *peer_req = container_of(w, struct drbd_peer_request, w);
980 struct drbd_conf *mdev = w->mdev;
983 if (unlikely(cancel)) {
984 drbd_free_peer_req(mdev, peer_req);
989 if (get_ldev_if_state(mdev, D_FAILED)) {
990 drbd_rs_complete_io(mdev, peer_req->i.sector);
994 if (mdev->state.conn == C_AHEAD) {
995 err = drbd_send_ack(mdev, P_RS_CANCEL, peer_req);
996 } else if (likely((peer_req->flags & EE_WAS_ERROR) == 0)) {
997 if (likely(mdev->state.pdsk >= D_INCONSISTENT)) {
998 inc_rs_pending(mdev);
999 err = drbd_send_block(mdev, P_RS_DATA_REPLY, peer_req);
1001 if (__ratelimit(&drbd_ratelimit_state))
1002 dev_err(DEV, "Not sending RSDataReply, "
1003 "partner DISKLESS!\n");
1007 if (__ratelimit(&drbd_ratelimit_state))
1008 dev_err(DEV, "Sending NegRSDReply. sector %llus.\n",
1009 (unsigned long long)peer_req->i.sector);
1011 err = drbd_send_ack(mdev, P_NEG_RS_DREPLY, peer_req);
1013 /* update resync data with failure */
1014 drbd_rs_failed_io(mdev, peer_req->i.sector, peer_req->i.size);
1019 move_to_net_ee_or_free(mdev, peer_req);
1022 dev_err(DEV, "drbd_send_block() failed\n");
1026 int w_e_end_csum_rs_req(struct drbd_work *w, int cancel)
1028 struct drbd_peer_request *peer_req = container_of(w, struct drbd_peer_request, w);
1029 struct drbd_conf *mdev = w->mdev;
1030 struct digest_info *di;
1032 void *digest = NULL;
1035 if (unlikely(cancel)) {
1036 drbd_free_peer_req(mdev, peer_req);
1041 if (get_ldev(mdev)) {
1042 drbd_rs_complete_io(mdev, peer_req->i.sector);
1046 di = peer_req->digest;
1048 if (likely((peer_req->flags & EE_WAS_ERROR) == 0)) {
1049 /* quick hack to try to avoid a race against reconfiguration.
1050 * a real fix would be much more involved,
1051 * introducing more locking mechanisms */
1052 if (mdev->tconn->csums_tfm) {
1053 digest_size = crypto_hash_digestsize(mdev->tconn->csums_tfm);
1054 D_ASSERT(digest_size == di->digest_size);
1055 digest = kmalloc(digest_size, GFP_NOIO);
1058 drbd_csum_ee(mdev, mdev->tconn->csums_tfm, peer_req, digest);
1059 eq = !memcmp(digest, di->digest, digest_size);
1064 drbd_set_in_sync(mdev, peer_req->i.sector, peer_req->i.size);
1065 /* rs_same_csums unit is BM_BLOCK_SIZE */
1066 mdev->rs_same_csum += peer_req->i.size >> BM_BLOCK_SHIFT;
1067 err = drbd_send_ack(mdev, P_RS_IS_IN_SYNC, peer_req);
1069 inc_rs_pending(mdev);
1070 peer_req->block_id = ID_SYNCER; /* By setting block_id, digest pointer becomes invalid! */
1071 peer_req->flags &= ~EE_HAS_DIGEST; /* This peer request no longer has a digest pointer */
1073 err = drbd_send_block(mdev, P_RS_DATA_REPLY, peer_req);
1076 err = drbd_send_ack(mdev, P_NEG_RS_DREPLY, peer_req);
1077 if (__ratelimit(&drbd_ratelimit_state))
1078 dev_err(DEV, "Sending NegDReply. I guess it gets messy.\n");
1082 move_to_net_ee_or_free(mdev, peer_req);
1085 dev_err(DEV, "drbd_send_block/ack() failed\n");
1089 int w_e_end_ov_req(struct drbd_work *w, int cancel)
1091 struct drbd_peer_request *peer_req = container_of(w, struct drbd_peer_request, w);
1092 struct drbd_conf *mdev = w->mdev;
1093 sector_t sector = peer_req->i.sector;
1094 unsigned int size = peer_req->i.size;
1099 if (unlikely(cancel))
1102 digest_size = crypto_hash_digestsize(mdev->tconn->verify_tfm);
1103 digest = kmalloc(digest_size, GFP_NOIO);
1105 err = 1; /* terminate the connection in case the allocation failed */
1109 if (likely(!(peer_req->flags & EE_WAS_ERROR)))
1110 drbd_csum_ee(mdev, mdev->tconn->verify_tfm, peer_req, digest);
1112 memset(digest, 0, digest_size);
1114 /* Free e and pages before send.
1115 * In case we block on congestion, we could otherwise run into
1116 * some distributed deadlock, if the other side blocks on
1117 * congestion as well, because our receiver blocks in
1118 * drbd_alloc_pages due to pp_in_use > max_buffers. */
1119 drbd_free_peer_req(mdev, peer_req);
1121 inc_rs_pending(mdev);
1122 err = drbd_send_drequest_csum(mdev, sector, size, digest, digest_size, P_OV_REPLY);
1124 dec_rs_pending(mdev);
1129 drbd_free_peer_req(mdev, peer_req);
1134 void drbd_ov_out_of_sync_found(struct drbd_conf *mdev, sector_t sector, int size)
1136 if (mdev->ov_last_oos_start + mdev->ov_last_oos_size == sector) {
1137 mdev->ov_last_oos_size += size>>9;
1139 mdev->ov_last_oos_start = sector;
1140 mdev->ov_last_oos_size = size>>9;
1142 drbd_set_out_of_sync(mdev, sector, size);
1145 int w_e_end_ov_reply(struct drbd_work *w, int cancel)
1147 struct drbd_peer_request *peer_req = container_of(w, struct drbd_peer_request, w);
1148 struct drbd_conf *mdev = w->mdev;
1149 struct digest_info *di;
1151 sector_t sector = peer_req->i.sector;
1152 unsigned int size = peer_req->i.size;
1156 if (unlikely(cancel)) {
1157 drbd_free_peer_req(mdev, peer_req);
1162 /* after "cancel", because after drbd_disconnect/drbd_rs_cancel_all
1163 * the resync lru has been cleaned up already */
1164 if (get_ldev(mdev)) {
1165 drbd_rs_complete_io(mdev, peer_req->i.sector);
1169 di = peer_req->digest;
1171 if (likely((peer_req->flags & EE_WAS_ERROR) == 0)) {
1172 digest_size = crypto_hash_digestsize(mdev->tconn->verify_tfm);
1173 digest = kmalloc(digest_size, GFP_NOIO);
1175 drbd_csum_ee(mdev, mdev->tconn->verify_tfm, peer_req, digest);
1177 D_ASSERT(digest_size == di->digest_size);
1178 eq = !memcmp(digest, di->digest, digest_size);
1183 /* Free peer_req and pages before send.
1184 * In case we block on congestion, we could otherwise run into
1185 * some distributed deadlock, if the other side blocks on
1186 * congestion as well, because our receiver blocks in
1187 * drbd_alloc_pages due to pp_in_use > max_buffers. */
1188 drbd_free_peer_req(mdev, peer_req);
1190 drbd_ov_out_of_sync_found(mdev, sector, size);
1192 ov_out_of_sync_print(mdev);
1194 err = drbd_send_ack_ex(mdev, P_OV_RESULT, sector, size,
1195 eq ? ID_IN_SYNC : ID_OUT_OF_SYNC);
1201 /* let's advance progress step marks only for every other megabyte */
1202 if ((mdev->ov_left & 0x200) == 0x200)
1203 drbd_advance_rs_marks(mdev, mdev->ov_left);
1205 if (mdev->ov_left == 0) {
1206 ov_out_of_sync_print(mdev);
1207 drbd_resync_finished(mdev);
1213 int w_prev_work_done(struct drbd_work *w, int cancel)
1215 struct drbd_wq_barrier *b = container_of(w, struct drbd_wq_barrier, w);
1221 int w_send_barrier(struct drbd_work *w, int cancel)
1223 struct drbd_socket *sock;
1224 struct drbd_tl_epoch *b = container_of(w, struct drbd_tl_epoch, w);
1225 struct drbd_conf *mdev = w->mdev;
1226 struct p_barrier *p;
1228 /* really avoid racing with tl_clear. w.cb may have been referenced
1229 * just before it was reassigned and re-queued, so double check that.
1230 * actually, this race was harmless, since we only try to send the
1231 * barrier packet here, and otherwise do nothing with the object.
1232 * but compare with the head of w_clear_epoch */
1233 spin_lock_irq(&mdev->tconn->req_lock);
1234 if (w->cb != w_send_barrier || mdev->state.conn < C_CONNECTED)
1236 spin_unlock_irq(&mdev->tconn->req_lock);
1240 sock = &mdev->tconn->data;
1241 p = drbd_prepare_command(mdev, sock);
1244 p->barrier = b->br_number;
1245 /* inc_ap_pending was done where this was queued.
1246 * dec_ap_pending will be done in got_BarrierAck
1247 * or (on connection loss) in w_clear_epoch. */
1248 return drbd_send_command(mdev, sock, P_BARRIER, sizeof(*p), NULL, 0);
1251 int w_send_write_hint(struct drbd_work *w, int cancel)
1253 struct drbd_conf *mdev = w->mdev;
1254 struct drbd_socket *sock;
1258 sock = &mdev->tconn->data;
1259 if (!drbd_prepare_command(mdev, sock))
1261 return drbd_send_command(mdev, sock, P_UNPLUG_REMOTE, 0, NULL, 0);
1264 int w_send_out_of_sync(struct drbd_work *w, int cancel)
1266 struct drbd_request *req = container_of(w, struct drbd_request, w);
1267 struct drbd_conf *mdev = w->mdev;
1270 if (unlikely(cancel)) {
1271 req_mod(req, SEND_CANCELED);
1275 err = drbd_send_out_of_sync(mdev, req);
1276 req_mod(req, OOS_HANDED_TO_NETWORK);
1282 * w_send_dblock() - Worker callback to send a P_DATA packet in order to mirror a write request
1283 * @mdev: DRBD device.
1285 * @cancel: The connection will be closed anyways
1287 int w_send_dblock(struct drbd_work *w, int cancel)
1289 struct drbd_request *req = container_of(w, struct drbd_request, w);
1290 struct drbd_conf *mdev = w->mdev;
1293 if (unlikely(cancel)) {
1294 req_mod(req, SEND_CANCELED);
1298 err = drbd_send_dblock(mdev, req);
1299 req_mod(req, err ? SEND_FAILED : HANDED_OVER_TO_NETWORK);
1305 * w_send_read_req() - Worker callback to send a read request (P_DATA_REQUEST) packet
1306 * @mdev: DRBD device.
1308 * @cancel: The connection will be closed anyways
1310 int w_send_read_req(struct drbd_work *w, int cancel)
1312 struct drbd_request *req = container_of(w, struct drbd_request, w);
1313 struct drbd_conf *mdev = w->mdev;
1316 if (unlikely(cancel)) {
1317 req_mod(req, SEND_CANCELED);
1321 err = drbd_send_drequest(mdev, P_DATA_REQUEST, req->i.sector, req->i.size,
1322 (unsigned long)req);
1324 req_mod(req, err ? SEND_FAILED : HANDED_OVER_TO_NETWORK);
1329 int w_restart_disk_io(struct drbd_work *w, int cancel)
1331 struct drbd_request *req = container_of(w, struct drbd_request, w);
1332 struct drbd_conf *mdev = w->mdev;
1334 if (bio_data_dir(req->master_bio) == WRITE && req->rq_state & RQ_IN_ACT_LOG)
1335 drbd_al_begin_io(mdev, &req->i);
1337 drbd_req_make_private_bio(req, req->master_bio);
1338 req->private_bio->bi_bdev = mdev->ldev->backing_bdev;
1339 generic_make_request(req->private_bio);
1344 static int _drbd_may_sync_now(struct drbd_conf *mdev)
1346 struct drbd_conf *odev = mdev;
1353 resync_after = rcu_dereference(odev->ldev->disk_conf)->resync_after;
1355 if (resync_after == -1)
1357 odev = minor_to_mdev(resync_after);
1360 if ((odev->state.conn >= C_SYNC_SOURCE &&
1361 odev->state.conn <= C_PAUSED_SYNC_T) ||
1362 odev->state.aftr_isp || odev->state.peer_isp ||
1363 odev->state.user_isp)
1369 * _drbd_pause_after() - Pause resync on all devices that may not resync now
1370 * @mdev: DRBD device.
1372 * Called from process context only (admin command and after_state_ch).
1374 static int _drbd_pause_after(struct drbd_conf *mdev)
1376 struct drbd_conf *odev;
1380 idr_for_each_entry(&minors, odev, i) {
1381 if (odev->state.conn == C_STANDALONE && odev->state.disk == D_DISKLESS)
1383 if (!_drbd_may_sync_now(odev))
1384 rv |= (__drbd_set_state(_NS(odev, aftr_isp, 1), CS_HARD, NULL)
1385 != SS_NOTHING_TO_DO);
1393 * _drbd_resume_next() - Resume resync on all devices that may resync now
1394 * @mdev: DRBD device.
1396 * Called from process context only (admin command and worker).
1398 static int _drbd_resume_next(struct drbd_conf *mdev)
1400 struct drbd_conf *odev;
1404 idr_for_each_entry(&minors, odev, i) {
1405 if (odev->state.conn == C_STANDALONE && odev->state.disk == D_DISKLESS)
1407 if (odev->state.aftr_isp) {
1408 if (_drbd_may_sync_now(odev))
1409 rv |= (__drbd_set_state(_NS(odev, aftr_isp, 0),
1411 != SS_NOTHING_TO_DO) ;
1418 void resume_next_sg(struct drbd_conf *mdev)
1420 write_lock_irq(&global_state_lock);
1421 _drbd_resume_next(mdev);
1422 write_unlock_irq(&global_state_lock);
1425 void suspend_other_sg(struct drbd_conf *mdev)
1427 write_lock_irq(&global_state_lock);
1428 _drbd_pause_after(mdev);
1429 write_unlock_irq(&global_state_lock);
1432 /* caller must hold global_state_lock */
1433 enum drbd_ret_code drbd_resync_after_valid(struct drbd_conf *mdev, int o_minor)
1435 struct drbd_conf *odev;
1440 if (o_minor < -1 || minor_to_mdev(o_minor) == NULL)
1441 return ERR_RESYNC_AFTER;
1443 /* check for loops */
1444 odev = minor_to_mdev(o_minor);
1447 return ERR_RESYNC_AFTER_CYCLE;
1450 resync_after = rcu_dereference(odev->ldev->disk_conf)->resync_after;
1452 /* dependency chain ends here, no cycles. */
1453 if (resync_after == -1)
1456 /* follow the dependency chain */
1457 odev = minor_to_mdev(resync_after);
1461 /* caller must hold global_state_lock */
1462 void drbd_resync_after_changed(struct drbd_conf *mdev)
1467 changes = _drbd_pause_after(mdev);
1468 changes |= _drbd_resume_next(mdev);
1472 void drbd_rs_controller_reset(struct drbd_conf *mdev)
1474 struct fifo_buffer *plan;
1476 atomic_set(&mdev->rs_sect_in, 0);
1477 atomic_set(&mdev->rs_sect_ev, 0);
1478 mdev->rs_in_flight = 0;
1480 /* Updating the RCU protected object in place is necessary since
1481 this function gets called from atomic context.
1482 It is valid since all other updates also lead to an completely
1485 plan = rcu_dereference(mdev->rs_plan_s);
1491 void start_resync_timer_fn(unsigned long data)
1493 struct drbd_conf *mdev = (struct drbd_conf *) data;
1495 drbd_queue_work(&mdev->tconn->data.work, &mdev->start_resync_work);
1498 int w_start_resync(struct drbd_work *w, int cancel)
1500 struct drbd_conf *mdev = w->mdev;
1502 if (atomic_read(&mdev->unacked_cnt) || atomic_read(&mdev->rs_pending_cnt)) {
1503 dev_warn(DEV, "w_start_resync later...\n");
1504 mdev->start_resync_timer.expires = jiffies + HZ/10;
1505 add_timer(&mdev->start_resync_timer);
1509 drbd_start_resync(mdev, C_SYNC_SOURCE);
1510 clear_bit(AHEAD_TO_SYNC_SOURCE, &mdev->current_epoch->flags);
1515 * drbd_start_resync() - Start the resync process
1516 * @mdev: DRBD device.
1517 * @side: Either C_SYNC_SOURCE or C_SYNC_TARGET
1519 * This function might bring you directly into one of the
1520 * C_PAUSED_SYNC_* states.
1522 void drbd_start_resync(struct drbd_conf *mdev, enum drbd_conns side)
1524 union drbd_state ns;
1527 if (mdev->state.conn >= C_SYNC_SOURCE && mdev->state.conn < C_AHEAD) {
1528 dev_err(DEV, "Resync already running!\n");
1532 if (mdev->state.conn < C_AHEAD) {
1533 /* In case a previous resync run was aborted by an IO error/detach on the peer. */
1534 drbd_rs_cancel_all(mdev);
1535 /* This should be done when we abort the resync. We definitely do not
1536 want to have this for connections going back and forth between
1537 Ahead/Behind and SyncSource/SyncTarget */
1540 if (!test_bit(B_RS_H_DONE, &mdev->flags)) {
1541 if (side == C_SYNC_TARGET) {
1542 /* Since application IO was locked out during C_WF_BITMAP_T and
1543 C_WF_SYNC_UUID we are still unmodified. Before going to C_SYNC_TARGET
1544 we check that we might make the data inconsistent. */
1545 r = drbd_khelper(mdev, "before-resync-target");
1546 r = (r >> 8) & 0xff;
1548 dev_info(DEV, "before-resync-target handler returned %d, "
1549 "dropping connection.\n", r);
1550 conn_request_state(mdev->tconn, NS(conn, C_DISCONNECTING), CS_HARD);
1553 } else /* C_SYNC_SOURCE */ {
1554 r = drbd_khelper(mdev, "before-resync-source");
1555 r = (r >> 8) & 0xff;
1558 dev_info(DEV, "before-resync-source handler returned %d, "
1559 "ignoring. Old userland tools?", r);
1561 dev_info(DEV, "before-resync-source handler returned %d, "
1562 "dropping connection.\n", r);
1563 conn_request_state(mdev->tconn, NS(conn, C_DISCONNECTING), CS_HARD);
1570 if (current == mdev->tconn->worker.task) {
1571 /* The worker should not sleep waiting for state_mutex,
1572 that can take long */
1573 if (!mutex_trylock(mdev->state_mutex)) {
1574 set_bit(B_RS_H_DONE, &mdev->flags);
1575 mdev->start_resync_timer.expires = jiffies + HZ/5;
1576 add_timer(&mdev->start_resync_timer);
1580 mutex_lock(mdev->state_mutex);
1582 clear_bit(B_RS_H_DONE, &mdev->flags);
1584 if (!get_ldev_if_state(mdev, D_NEGOTIATING)) {
1585 mutex_unlock(mdev->state_mutex);
1589 write_lock_irq(&global_state_lock);
1590 ns = drbd_read_state(mdev);
1592 ns.aftr_isp = !_drbd_may_sync_now(mdev);
1596 if (side == C_SYNC_TARGET)
1597 ns.disk = D_INCONSISTENT;
1598 else /* side == C_SYNC_SOURCE */
1599 ns.pdsk = D_INCONSISTENT;
1601 r = __drbd_set_state(mdev, ns, CS_VERBOSE, NULL);
1602 ns = drbd_read_state(mdev);
1604 if (ns.conn < C_CONNECTED)
1605 r = SS_UNKNOWN_ERROR;
1607 if (r == SS_SUCCESS) {
1608 unsigned long tw = drbd_bm_total_weight(mdev);
1609 unsigned long now = jiffies;
1612 mdev->rs_failed = 0;
1613 mdev->rs_paused = 0;
1614 mdev->rs_same_csum = 0;
1615 mdev->rs_last_events = 0;
1616 mdev->rs_last_sect_ev = 0;
1617 mdev->rs_total = tw;
1618 mdev->rs_start = now;
1619 for (i = 0; i < DRBD_SYNC_MARKS; i++) {
1620 mdev->rs_mark_left[i] = tw;
1621 mdev->rs_mark_time[i] = now;
1623 _drbd_pause_after(mdev);
1625 write_unlock_irq(&global_state_lock);
1627 if (r == SS_SUCCESS) {
1628 dev_info(DEV, "Began resync as %s (will sync %lu KB [%lu bits set]).\n",
1629 drbd_conn_str(ns.conn),
1630 (unsigned long) mdev->rs_total << (BM_BLOCK_SHIFT-10),
1631 (unsigned long) mdev->rs_total);
1632 if (side == C_SYNC_TARGET)
1633 mdev->bm_resync_fo = 0;
1635 /* Since protocol 96, we must serialize drbd_gen_and_send_sync_uuid
1636 * with w_send_oos, or the sync target will get confused as to
1637 * how much bits to resync. We cannot do that always, because for an
1638 * empty resync and protocol < 95, we need to do it here, as we call
1639 * drbd_resync_finished from here in that case.
1640 * We drbd_gen_and_send_sync_uuid here for protocol < 96,
1641 * and from after_state_ch otherwise. */
1642 if (side == C_SYNC_SOURCE && mdev->tconn->agreed_pro_version < 96)
1643 drbd_gen_and_send_sync_uuid(mdev);
1645 if (mdev->tconn->agreed_pro_version < 95 && mdev->rs_total == 0) {
1646 /* This still has a race (about when exactly the peers
1647 * detect connection loss) that can lead to a full sync
1648 * on next handshake. In 8.3.9 we fixed this with explicit
1649 * resync-finished notifications, but the fix
1650 * introduces a protocol change. Sleeping for some
1651 * time longer than the ping interval + timeout on the
1652 * SyncSource, to give the SyncTarget the chance to
1653 * detect connection loss, then waiting for a ping
1654 * response (implicit in drbd_resync_finished) reduces
1655 * the race considerably, but does not solve it. */
1656 if (side == C_SYNC_SOURCE) {
1657 struct net_conf *nc;
1661 nc = rcu_dereference(mdev->tconn->net_conf);
1662 timeo = nc->ping_int * HZ + nc->ping_timeo * HZ / 9;
1664 schedule_timeout_interruptible(timeo);
1666 drbd_resync_finished(mdev);
1669 drbd_rs_controller_reset(mdev);
1670 /* ns.conn may already be != mdev->state.conn,
1671 * we may have been paused in between, or become paused until
1672 * the timer triggers.
1673 * No matter, that is handled in resync_timer_fn() */
1674 if (ns.conn == C_SYNC_TARGET)
1675 mod_timer(&mdev->resync_timer, jiffies);
1680 mutex_unlock(mdev->state_mutex);
1683 int drbd_worker(struct drbd_thread *thi)
1685 struct drbd_tconn *tconn = thi->tconn;
1686 struct drbd_work *w = NULL;
1687 struct drbd_conf *mdev;
1688 struct net_conf *nc;
1689 LIST_HEAD(work_list);
1693 while (get_t_state(thi) == RUNNING) {
1694 drbd_thread_current_set_cpu(thi);
1696 if (down_trylock(&tconn->data.work.s)) {
1697 mutex_lock(&tconn->data.mutex);
1700 nc = rcu_dereference(tconn->net_conf);
1701 cork = nc ? nc->tcp_cork : 0;
1704 if (tconn->data.socket && cork)
1705 drbd_tcp_uncork(tconn->data.socket);
1706 mutex_unlock(&tconn->data.mutex);
1708 intr = down_interruptible(&tconn->data.work.s);
1710 mutex_lock(&tconn->data.mutex);
1711 if (tconn->data.socket && cork)
1712 drbd_tcp_cork(tconn->data.socket);
1713 mutex_unlock(&tconn->data.mutex);
1717 flush_signals(current);
1718 if (get_t_state(thi) == RUNNING) {
1719 conn_warn(tconn, "Worker got an unexpected signal\n");
1725 if (get_t_state(thi) != RUNNING)
1727 /* With this break, we have done a down() but not consumed
1728 the entry from the list. The cleanup code takes care of
1732 spin_lock_irq(&tconn->data.work.q_lock);
1733 if (list_empty(&tconn->data.work.q)) {
1734 /* something terribly wrong in our logic.
1735 * we were able to down() the semaphore,
1736 * but the list is empty... doh.
1738 * what is the best thing to do now?
1739 * try again from scratch, restarting the receiver,
1740 * asender, whatnot? could break even more ugly,
1741 * e.g. when we are primary, but no good local data.
1743 * I'll try to get away just starting over this loop.
1745 conn_warn(tconn, "Work list unexpectedly empty\n");
1746 spin_unlock_irq(&tconn->data.work.q_lock);
1749 w = list_entry(tconn->data.work.q.next, struct drbd_work, list);
1750 list_del_init(&w->list);
1751 spin_unlock_irq(&tconn->data.work.q_lock);
1753 if (w->cb(w, tconn->cstate < C_WF_REPORT_PARAMS)) {
1754 /* dev_warn(DEV, "worker: a callback failed! \n"); */
1755 if (tconn->cstate >= C_WF_REPORT_PARAMS)
1756 conn_request_state(tconn, NS(conn, C_NETWORK_FAILURE), CS_HARD);
1760 spin_lock_irq(&tconn->data.work.q_lock);
1761 while (!list_empty(&tconn->data.work.q)) {
1762 list_splice_init(&tconn->data.work.q, &work_list);
1763 spin_unlock_irq(&tconn->data.work.q_lock);
1765 while (!list_empty(&work_list)) {
1766 w = list_entry(work_list.next, struct drbd_work, list);
1767 list_del_init(&w->list);
1771 spin_lock_irq(&tconn->data.work.q_lock);
1773 sema_init(&tconn->data.work.s, 0);
1774 /* DANGEROUS race: if someone did queue his work within the spinlock,
1775 * but up() ed outside the spinlock, we could get an up() on the
1776 * semaphore without corresponding list entry.
1779 spin_unlock_irq(&tconn->data.work.q_lock);
1782 idr_for_each_entry(&tconn->volumes, mdev, vnr) {
1783 D_ASSERT(mdev->state.disk == D_DISKLESS && mdev->state.conn == C_STANDALONE);
1784 kref_get(&mdev->kref);
1786 drbd_mdev_cleanup(mdev);
1787 kref_put(&mdev->kref, &drbd_minor_destroy);