blob: 3b3d980ac8c7bf79f84f5a51870011673fdd4fc7 [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
Andreas Gruenbacher84b8c062011-07-28 15:27:51 +020024*/
Philipp Reisnerb411b362009-09-25 16:07:19 -070025
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"
Andreas Gruenbachera3603a62011-05-30 11:47:37 +020039#include "drbd_protocol.h"
Philipp Reisnerb411b362009-09-25 16:07:19 -070040#include "drbd_req.h"
Philipp Reisnerb411b362009-09-25 16:07:19 -070041
Andreas Gruenbacherd448a2e2011-08-25 16:59:58 +020042static int make_ov_request(struct drbd_device *, int);
43static int make_resync_request(struct drbd_device *, int);
Philipp Reisnerb411b362009-09-25 16:07:19 -070044
Andreas Gruenbacherc5a91612011-01-25 17:33:38 +010045/* endio handlers:
Andreas Gruenbachered15b792014-09-11 14:29:06 +020046 * drbd_md_endio (defined here)
Andreas Gruenbacherfcefa622011-02-17 16:46:59 +010047 * drbd_request_endio (defined here)
48 * drbd_peer_request_endio (defined here)
Andreas Gruenbachered15b792014-09-11 14:29:06 +020049 * drbd_bm_endio (defined in drbd_bitmap.c)
Andreas Gruenbacherc5a91612011-01-25 17:33:38 +010050 *
Philipp Reisnerb411b362009-09-25 16:07:19 -070051 * For all these callbacks, note the following:
52 * The callbacks will be called in irq context by the IDE drivers,
53 * and in Softirqs/Tasklets/BH context by the SCSI drivers.
54 * Try to get the locking right :)
55 *
56 */
57
Philipp Reisnerb411b362009-09-25 16:07:19 -070058/* used for synchronous meta data and bitmap IO
59 * submitted by drbd_md_sync_page_io()
60 */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +020061void drbd_md_endio(struct bio *bio)
Philipp Reisnerb411b362009-09-25 16:07:19 -070062{
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +020063 struct drbd_device *device;
Philipp Reisnerb411b362009-09-25 16:07:19 -070064
Lars Ellenberge37d2432014-04-01 23:53:30 +020065 device = bio->bi_private;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +020066 device->md_io.error = bio->bi_error;
Philipp Reisnerb411b362009-09-25 16:07:19 -070067
Philipp Reisner0cfac5d2011-11-10 12:12:52 +010068 /* We grabbed an extra reference in _drbd_md_sync_page_io() to be able
69 * to timeout on the lower level device, and eventually detach from it.
70 * If this io completion runs after that timeout expired, this
71 * drbd_md_put_buffer() may allow us to finally try and re-attach.
72 * During normal operation, this only puts that extra reference
73 * down to 1 again.
74 * Make sure we first drop the reference, and only then signal
75 * completion, or we may (in drbd_al_read_log()) cycle so fast into the
76 * next drbd_md_sync_page_io(), that we trigger the
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +020077 * ASSERT(atomic_read(&device->md_io_in_use) == 1) there.
Philipp Reisner0cfac5d2011-11-10 12:12:52 +010078 */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +020079 drbd_md_put_buffer(device);
Lars Ellenberge37d2432014-04-01 23:53:30 +020080 device->md_io.done = 1;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +020081 wake_up(&device->misc_wait);
Philipp Reisnercdfda632011-07-05 15:38:59 +020082 bio_put(bio);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +020083 if (device->ldev) /* special case: drbd_md_read() during drbd_adm_attach() */
84 put_ldev(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -070085}
86
87/* reads on behalf of the partner,
88 * "submitted" by the receiver
89 */
Rashika Kheriaa186e472013-12-19 15:06:10 +053090static void drbd_endio_read_sec_final(struct drbd_peer_request *peer_req) __releases(local)
Philipp Reisnerb411b362009-09-25 16:07:19 -070091{
92 unsigned long flags = 0;
Andreas Gruenbacher67801392011-09-13 10:39:41 +020093 struct drbd_peer_device *peer_device = peer_req->peer_device;
94 struct drbd_device *device = peer_device->device;
Philipp Reisnerb411b362009-09-25 16:07:19 -070095
Andreas Gruenbacher05008132011-07-07 14:19:42 +020096 spin_lock_irqsave(&device->resource->req_lock, flags);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +020097 device->read_cnt += peer_req->i.size >> 9;
Andreas Gruenbachera8cd15b2011-08-25 15:49:40 +020098 list_del(&peer_req->w.list);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +020099 if (list_empty(&device->read_ee))
100 wake_up(&device->ee_wait);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100101 if (test_bit(__EE_WAS_ERROR, &peer_req->flags))
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200102 __drbd_chk_io_error(device, DRBD_READ_ERROR);
Andreas Gruenbacher05008132011-07-07 14:19:42 +0200103 spin_unlock_irqrestore(&device->resource->req_lock, flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700104
Andreas Gruenbacher67801392011-09-13 10:39:41 +0200105 drbd_queue_work(&peer_device->connection->sender_work, &peer_req->w);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200106 put_ldev(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700107}
108
109/* writes on behalf of the partner, or resync writes,
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200110 * "submitted" by the receiver, final stage. */
Lars Ellenberga0fb3c42014-04-28 18:43:23 +0200111void drbd_endio_write_sec_final(struct drbd_peer_request *peer_req) __releases(local)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700112{
113 unsigned long flags = 0;
Andreas Gruenbacher67801392011-09-13 10:39:41 +0200114 struct drbd_peer_device *peer_device = peer_req->peer_device;
115 struct drbd_device *device = peer_device->device;
Lars Ellenberg181286a2011-03-31 15:18:56 +0200116 struct drbd_interval i;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700117 int do_wake;
Andreas Gruenbacher579b57e2011-01-13 18:40:57 +0100118 u64 block_id;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700119 int do_al_complete_io;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700120
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100121 /* after we moved peer_req to done_ee,
Philipp Reisnerb411b362009-09-25 16:07:19 -0700122 * we may no longer access it,
123 * it may be freed/reused already!
124 * (as soon as we release the req_lock) */
Lars Ellenberg181286a2011-03-31 15:18:56 +0200125 i = peer_req->i;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100126 do_al_complete_io = peer_req->flags & EE_CALL_AL_COMPLETE_IO;
127 block_id = peer_req->block_id;
Lars Ellenberg21ae5d72014-05-05 23:42:24 +0200128 peer_req->flags &= ~EE_CALL_AL_COMPLETE_IO;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700129
Andreas Gruenbacher05008132011-07-07 14:19:42 +0200130 spin_lock_irqsave(&device->resource->req_lock, flags);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200131 device->writ_cnt += peer_req->i.size >> 9;
Andreas Gruenbachera8cd15b2011-08-25 15:49:40 +0200132 list_move_tail(&peer_req->w.list, &device->done_ee);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700133
Andreas Gruenbacherbb3bfe92011-01-21 15:59:23 +0100134 /*
Andreas Gruenbacher5e472262011-01-27 14:42:51 +0100135 * Do not remove from the write_requests tree here: we did not send the
Andreas Gruenbacherbb3bfe92011-01-21 15:59:23 +0100136 * Ack yet and did not wake possibly waiting conflicting requests.
137 * Removed from the tree from "drbd_process_done_ee" within the
Andreas Gruenbacher84b8c062011-07-28 15:27:51 +0200138 * appropriate dw.cb (e_end_block/e_end_resync_block) or from
Andreas Gruenbacherbb3bfe92011-01-21 15:59:23 +0100139 * _drbd_clear_done_ee.
140 */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700141
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200142 do_wake = list_empty(block_id == ID_SYNCER ? &device->sync_ee : &device->active_ee);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700143
Lars Ellenberga0fb3c42014-04-28 18:43:23 +0200144 /* FIXME do we want to detach for failed REQ_DISCARD?
145 * ((peer_req->flags & (EE_WAS_ERROR|EE_IS_TRIM)) == EE_WAS_ERROR) */
146 if (peer_req->flags & EE_WAS_ERROR)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200147 __drbd_chk_io_error(device, DRBD_WRITE_ERROR);
Andreas Gruenbacher05008132011-07-07 14:19:42 +0200148 spin_unlock_irqrestore(&device->resource->req_lock, flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700149
Andreas Gruenbacher579b57e2011-01-13 18:40:57 +0100150 if (block_id == ID_SYNCER)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200151 drbd_rs_complete_io(device, i.sector);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700152
153 if (do_wake)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200154 wake_up(&device->ee_wait);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700155
156 if (do_al_complete_io)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200157 drbd_al_complete_io(device, &i);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700158
Andreas Gruenbacher67801392011-09-13 10:39:41 +0200159 wake_asender(peer_device->connection);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200160 put_ldev(device);
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200161}
Philipp Reisnerb411b362009-09-25 16:07:19 -0700162
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200163/* writes on behalf of the partner, or resync writes,
164 * "submitted" by the receiver.
165 */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200166void drbd_peer_request_endio(struct bio *bio)
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200167{
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100168 struct drbd_peer_request *peer_req = bio->bi_private;
Andreas Gruenbachera8cd15b2011-08-25 15:49:40 +0200169 struct drbd_device *device = peer_req->peer_device->device;
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200170 int is_write = bio_data_dir(bio) == WRITE;
Lars Ellenberga0fb3c42014-04-28 18:43:23 +0200171 int is_discard = !!(bio->bi_rw & REQ_DISCARD);
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200172
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200173 if (bio->bi_error && __ratelimit(&drbd_ratelimit_state))
Andreas Gruenbacherd0180172011-07-03 17:53:52 +0200174 drbd_warn(device, "%s: error=%d s=%llus\n",
Lars Ellenberga0fb3c42014-04-28 18:43:23 +0200175 is_write ? (is_discard ? "discard" : "write")
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200176 : "read", bio->bi_error,
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100177 (unsigned long long)peer_req->i.sector);
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200178
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200179 if (bio->bi_error)
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100180 set_bit(__EE_WAS_ERROR, &peer_req->flags);
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200181
182 bio_put(bio); /* no need for the bio anymore */
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100183 if (atomic_dec_and_test(&peer_req->pending_bios)) {
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200184 if (is_write)
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100185 drbd_endio_write_sec_final(peer_req);
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200186 else
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100187 drbd_endio_read_sec_final(peer_req);
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200188 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700189}
190
191/* read, readA or write requests on R_PRIMARY coming from drbd_make_request
192 */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200193void drbd_request_endio(struct bio *bio)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700194{
Lars Ellenberga1154132010-11-13 20:42:29 +0100195 unsigned long flags;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700196 struct drbd_request *req = bio->bi_private;
Andreas Gruenbacher84b8c062011-07-28 15:27:51 +0200197 struct drbd_device *device = req->device;
Lars Ellenberga1154132010-11-13 20:42:29 +0100198 struct bio_and_error m;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700199 enum drbd_req_event what;
Philipp Reisner1b6dd252012-09-04 15:16:20 +0200200
201 /* If this request was aborted locally before,
202 * but now was completed "successfully",
203 * chances are that this caused arbitrary data corruption.
204 *
205 * "aborting" requests, or force-detaching the disk, is intended for
206 * completely blocked/hung local backing devices which do no longer
207 * complete requests at all, not even do error completions. In this
208 * situation, usually a hard-reset and failover is the only way out.
209 *
210 * By "aborting", basically faking a local error-completion,
211 * we allow for a more graceful swichover by cleanly migrating services.
212 * Still the affected node has to be rebooted "soon".
213 *
214 * By completing these requests, we allow the upper layers to re-use
215 * the associated data pages.
216 *
217 * If later the local backing device "recovers", and now DMAs some data
218 * from disk into the original request pages, in the best case it will
219 * just put random data into unused pages; but typically it will corrupt
220 * meanwhile completely unrelated data, causing all sorts of damage.
221 *
222 * Which means delayed successful completion,
223 * especially for READ requests,
224 * is a reason to panic().
225 *
226 * We assume that a delayed *error* completion is OK,
227 * though we still will complain noisily about it.
228 */
229 if (unlikely(req->rq_state & RQ_LOCAL_ABORTED)) {
230 if (__ratelimit(&drbd_ratelimit_state))
Andreas Gruenbacherd0180172011-07-03 17:53:52 +0200231 drbd_emerg(device, "delayed completion of aborted local request; disk-timeout may be too aggressive\n");
Philipp Reisner1b6dd252012-09-04 15:16:20 +0200232
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200233 if (!bio->bi_error)
Philipp Reisner1b6dd252012-09-04 15:16:20 +0200234 panic("possible random memory corruption caused by delayed completion of aborted local request\n");
235 }
236
Philipp Reisnerb411b362009-09-25 16:07:19 -0700237 /* to avoid recursion in __req_mod */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200238 if (unlikely(bio->bi_error)) {
Lars Ellenberg2f632ae2014-04-28 18:43:24 +0200239 if (bio->bi_rw & REQ_DISCARD)
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200240 what = (bio->bi_error == -EOPNOTSUPP)
Lars Ellenberg2f632ae2014-04-28 18:43:24 +0200241 ? DISCARD_COMPLETED_NOTSUPP
242 : DISCARD_COMPLETED_WITH_ERROR;
243 else
244 what = (bio_data_dir(bio) == WRITE)
Andreas Gruenbacher8554df12011-01-25 15:37:43 +0100245 ? WRITE_COMPLETED_WITH_ERROR
Lars Ellenberg5c3c7e62010-04-10 02:10:09 +0200246 : (bio_rw(bio) == READ)
Andreas Gruenbacher8554df12011-01-25 15:37:43 +0100247 ? READ_COMPLETED_WITH_ERROR
248 : READ_AHEAD_COMPLETED_WITH_ERROR;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700249 } else
Andreas Gruenbacher8554df12011-01-25 15:37:43 +0100250 what = COMPLETED_OK;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700251
252 bio_put(req->private_bio);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200253 req->private_bio = ERR_PTR(bio->bi_error);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700254
Lars Ellenberga1154132010-11-13 20:42:29 +0100255 /* not req_mod(), we need irqsave here! */
Andreas Gruenbacher05008132011-07-07 14:19:42 +0200256 spin_lock_irqsave(&device->resource->req_lock, flags);
Lars Ellenberga1154132010-11-13 20:42:29 +0100257 __req_mod(req, what, &m);
Andreas Gruenbacher05008132011-07-07 14:19:42 +0200258 spin_unlock_irqrestore(&device->resource->req_lock, flags);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200259 put_ldev(device);
Lars Ellenberga1154132010-11-13 20:42:29 +0100260
261 if (m.bio)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200262 complete_master_bio(device, &m);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700263}
264
Andreas Gruenbacher79a3c8d2011-08-09 02:49:01 +0200265void drbd_csum_ee(struct crypto_hash *tfm, struct drbd_peer_request *peer_req, void *digest)
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200266{
267 struct hash_desc desc;
268 struct scatterlist sg;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100269 struct page *page = peer_req->pages;
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200270 struct page *tmp;
271 unsigned len;
272
273 desc.tfm = tfm;
274 desc.flags = 0;
275
276 sg_init_table(&sg, 1);
277 crypto_hash_init(&desc);
278
279 while ((tmp = page_chain_next(page))) {
280 /* all but the last page will be fully used */
281 sg_set_page(&sg, page, PAGE_SIZE, 0);
282 crypto_hash_update(&desc, &sg, sg.length);
283 page = tmp;
284 }
285 /* and now the last, possibly only partially used page */
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100286 len = peer_req->i.size & (PAGE_SIZE - 1);
Lars Ellenberg45bb9122010-05-14 17:10:48 +0200287 sg_set_page(&sg, page, len ?: PAGE_SIZE, 0);
288 crypto_hash_update(&desc, &sg, sg.length);
289 crypto_hash_final(&desc, digest);
290}
291
Andreas Gruenbacher79a3c8d2011-08-09 02:49:01 +0200292void drbd_csum_bio(struct crypto_hash *tfm, struct bio *bio, void *digest)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700293{
294 struct hash_desc desc;
295 struct scatterlist sg;
Kent Overstreet79886132013-11-23 17:19:00 -0800296 struct bio_vec bvec;
297 struct bvec_iter iter;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700298
299 desc.tfm = tfm;
300 desc.flags = 0;
301
302 sg_init_table(&sg, 1);
303 crypto_hash_init(&desc);
304
Kent Overstreet79886132013-11-23 17:19:00 -0800305 bio_for_each_segment(bvec, bio, iter) {
306 sg_set_page(&sg, bvec.bv_page, bvec.bv_len, bvec.bv_offset);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700307 crypto_hash_update(&desc, &sg, sg.length);
308 }
309 crypto_hash_final(&desc, digest);
310}
311
Lars Ellenberg9676c762011-02-22 14:02:31 +0100312/* MAYBE merge common code with w_e_end_ov_req */
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +0100313static int w_e_send_csum(struct drbd_work *w, int cancel)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700314{
Andreas Gruenbachera8cd15b2011-08-25 15:49:40 +0200315 struct drbd_peer_request *peer_req = container_of(w, struct drbd_peer_request, w);
Andreas Gruenbacher67801392011-09-13 10:39:41 +0200316 struct drbd_peer_device *peer_device = peer_req->peer_device;
317 struct drbd_device *device = peer_device->device;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700318 int digest_size;
319 void *digest;
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +0100320 int err = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700321
Lars Ellenberg53ea4332011-03-08 17:11:40 +0100322 if (unlikely(cancel))
323 goto out;
324
Lars Ellenberg9676c762011-02-22 14:02:31 +0100325 if (unlikely((peer_req->flags & EE_WAS_ERROR) != 0))
Lars Ellenberg53ea4332011-03-08 17:11:40 +0100326 goto out;
327
Andreas Gruenbacher67801392011-09-13 10:39:41 +0200328 digest_size = crypto_hash_digestsize(peer_device->connection->csums_tfm);
Lars Ellenberg53ea4332011-03-08 17:11:40 +0100329 digest = kmalloc(digest_size, GFP_NOIO);
330 if (digest) {
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100331 sector_t sector = peer_req->i.sector;
332 unsigned int size = peer_req->i.size;
Andreas Gruenbacher67801392011-09-13 10:39:41 +0200333 drbd_csum_ee(peer_device->connection->csums_tfm, peer_req, digest);
Lars Ellenberg9676c762011-02-22 14:02:31 +0100334 /* Free peer_req and pages before send.
Lars Ellenberg53ea4332011-03-08 17:11:40 +0100335 * In case we block on congestion, we could otherwise run into
336 * some distributed deadlock, if the other side blocks on
337 * congestion as well, because our receiver blocks in
Andreas Gruenbacherc37c8ec2011-04-07 21:02:09 +0200338 * drbd_alloc_pages due to pp_in_use > max_buffers. */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200339 drbd_free_peer_req(device, peer_req);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100340 peer_req = NULL;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200341 inc_rs_pending(device);
Andreas Gruenbacher67801392011-09-13 10:39:41 +0200342 err = drbd_send_drequest_csum(peer_device, sector, size,
Andreas Gruenbacherdb1b0b72011-03-16 01:37:21 +0100343 digest, digest_size,
344 P_CSUM_RS_REQUEST);
Lars Ellenberg53ea4332011-03-08 17:11:40 +0100345 kfree(digest);
346 } else {
Andreas Gruenbacherd0180172011-07-03 17:53:52 +0200347 drbd_err(device, "kmalloc() of digest failed.\n");
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +0100348 err = -ENOMEM;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700349 }
350
Lars Ellenberg53ea4332011-03-08 17:11:40 +0100351out:
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100352 if (peer_req)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200353 drbd_free_peer_req(device, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700354
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +0100355 if (unlikely(err))
Andreas Gruenbacherd0180172011-07-03 17:53:52 +0200356 drbd_err(device, "drbd_send_drequest(..., csum) failed\n");
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +0100357 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700358}
359
360#define GFP_TRY (__GFP_HIGHMEM | __GFP_NOWARN)
361
Andreas Gruenbacher69a22772011-08-09 00:47:13 +0200362static int read_for_csum(struct drbd_peer_device *peer_device, sector_t sector, int size)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700363{
Andreas Gruenbacher69a22772011-08-09 00:47:13 +0200364 struct drbd_device *device = peer_device->device;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100365 struct drbd_peer_request *peer_req;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700366
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200367 if (!get_ldev(device))
Lars Ellenberg80a40e42010-08-11 23:28:00 +0200368 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700369
370 /* GFP_TRY, because if there is no memory available right now, this may
371 * be rescheduled for later. It is "only" background resync, after all. */
Andreas Gruenbacher69a22772011-08-09 00:47:13 +0200372 peer_req = drbd_alloc_peer_req(peer_device, ID_SYNCER /* unused */, sector,
Lars Ellenberga0fb3c42014-04-28 18:43:23 +0200373 size, true /* has real payload */, GFP_TRY);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100374 if (!peer_req)
Lars Ellenberg80a40e42010-08-11 23:28:00 +0200375 goto defer;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700376
Andreas Gruenbachera8cd15b2011-08-25 15:49:40 +0200377 peer_req->w.cb = w_e_send_csum;
Andreas Gruenbacher05008132011-07-07 14:19:42 +0200378 spin_lock_irq(&device->resource->req_lock);
Lars Ellenbergb9ed7082014-04-23 12:15:35 +0200379 list_add_tail(&peer_req->w.list, &device->read_ee);
Andreas Gruenbacher05008132011-07-07 14:19:42 +0200380 spin_unlock_irq(&device->resource->req_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700381
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200382 atomic_add(size >> 9, &device->rs_sect_ev);
383 if (drbd_submit_peer_request(device, peer_req, READ, DRBD_FAULT_RS_RD) == 0)
Lars Ellenberg80a40e42010-08-11 23:28:00 +0200384 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700385
Lars Ellenberg10f6d9922011-01-24 14:47:09 +0100386 /* If it failed because of ENOMEM, retry should help. If it failed
387 * because bio_add_page failed (probably broken lower level driver),
388 * retry may or may not help.
389 * If it does not, you may need to force disconnect. */
Andreas Gruenbacher05008132011-07-07 14:19:42 +0200390 spin_lock_irq(&device->resource->req_lock);
Andreas Gruenbachera8cd15b2011-08-25 15:49:40 +0200391 list_del(&peer_req->w.list);
Andreas Gruenbacher05008132011-07-07 14:19:42 +0200392 spin_unlock_irq(&device->resource->req_lock);
Lars Ellenberg22cc37a2010-09-14 20:40:41 +0200393
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200394 drbd_free_peer_req(device, peer_req);
Lars Ellenberg80a40e42010-08-11 23:28:00 +0200395defer:
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200396 put_ldev(device);
Lars Ellenberg80a40e42010-08-11 23:28:00 +0200397 return -EAGAIN;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700398}
399
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +0100400int w_resync_timer(struct drbd_work *w, int cancel)
Philipp Reisner794abb72010-12-27 11:51:23 +0100401{
Andreas Gruenbacher84b8c062011-07-28 15:27:51 +0200402 struct drbd_device *device =
403 container_of(w, struct drbd_device, resync_work);
404
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200405 switch (device->state.conn) {
Philipp Reisner794abb72010-12-27 11:51:23 +0100406 case C_VERIFY_S:
Andreas Gruenbacherd448a2e2011-08-25 16:59:58 +0200407 make_ov_request(device, cancel);
Philipp Reisner794abb72010-12-27 11:51:23 +0100408 break;
409 case C_SYNC_TARGET:
Andreas Gruenbacherd448a2e2011-08-25 16:59:58 +0200410 make_resync_request(device, cancel);
Philipp Reisner794abb72010-12-27 11:51:23 +0100411 break;
412 }
413
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +0100414 return 0;
Philipp Reisner794abb72010-12-27 11:51:23 +0100415}
416
Philipp Reisnerb411b362009-09-25 16:07:19 -0700417void resync_timer_fn(unsigned long data)
418{
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200419 struct drbd_device *device = (struct drbd_device *) data;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700420
Lars Ellenberg15e26f62014-04-28 11:43:21 +0200421 drbd_queue_work_if_unqueued(
422 &first_peer_device(device)->connection->sender_work,
423 &device->resync_work);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700424}
425
Philipp Reisner778f2712010-07-06 11:14:00 +0200426static void fifo_set(struct fifo_buffer *fb, int value)
427{
428 int i;
429
430 for (i = 0; i < fb->size; i++)
Philipp Reisnerf10f2622010-10-05 16:50:17 +0200431 fb->values[i] = value;
Philipp Reisner778f2712010-07-06 11:14:00 +0200432}
433
434static int fifo_push(struct fifo_buffer *fb, int value)
435{
436 int ov;
437
438 ov = fb->values[fb->head_index];
439 fb->values[fb->head_index++] = value;
440
441 if (fb->head_index >= fb->size)
442 fb->head_index = 0;
443
444 return ov;
445}
446
447static void fifo_add_val(struct fifo_buffer *fb, int value)
448{
449 int i;
450
451 for (i = 0; i < fb->size; i++)
452 fb->values[i] += value;
453}
454
Philipp Reisner9958c852011-05-03 16:19:31 +0200455struct fifo_buffer *fifo_alloc(int fifo_size)
456{
457 struct fifo_buffer *fb;
458
Lars Ellenberg8747d302012-09-26 14:22:40 +0200459 fb = kzalloc(sizeof(struct fifo_buffer) + sizeof(int) * fifo_size, GFP_NOIO);
Philipp Reisner9958c852011-05-03 16:19:31 +0200460 if (!fb)
461 return NULL;
462
463 fb->head_index = 0;
464 fb->size = fifo_size;
465 fb->total = 0;
466
467 return fb;
468}
469
Lars Ellenberg0e49d7b2014-04-28 18:43:18 +0200470static int drbd_rs_controller(struct drbd_device *device, unsigned int sect_in)
Philipp Reisner778f2712010-07-06 11:14:00 +0200471{
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +0200472 struct disk_conf *dc;
Lars Ellenberg7f34f612014-04-22 16:37:16 +0200473 unsigned int want; /* The number of sectors we want in-flight */
Philipp Reisner778f2712010-07-06 11:14:00 +0200474 int req_sect; /* Number of sectors to request in this turn */
Lars Ellenberg7f34f612014-04-22 16:37:16 +0200475 int correction; /* Number of sectors more we need in-flight */
Philipp Reisner778f2712010-07-06 11:14:00 +0200476 int cps; /* correction per invocation of drbd_rs_controller() */
477 int steps; /* Number of time steps to plan ahead */
478 int curr_corr;
479 int max_sect;
Philipp Reisner813472c2011-05-03 16:47:02 +0200480 struct fifo_buffer *plan;
Philipp Reisner778f2712010-07-06 11:14:00 +0200481
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200482 dc = rcu_dereference(device->ldev->disk_conf);
483 plan = rcu_dereference(device->rs_plan_s);
Philipp Reisner778f2712010-07-06 11:14:00 +0200484
Philipp Reisner813472c2011-05-03 16:47:02 +0200485 steps = plan->size; /* (dc->c_plan_ahead * 10 * SLEEP_TIME) / HZ; */
Philipp Reisner778f2712010-07-06 11:14:00 +0200486
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200487 if (device->rs_in_flight + sect_in == 0) { /* At start of resync */
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +0200488 want = ((dc->resync_rate * 2 * SLEEP_TIME) / HZ) * steps;
Philipp Reisner778f2712010-07-06 11:14:00 +0200489 } else { /* normal path */
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +0200490 want = dc->c_fill_target ? dc->c_fill_target :
491 sect_in * dc->c_delay_target * HZ / (SLEEP_TIME * 10);
Philipp Reisner778f2712010-07-06 11:14:00 +0200492 }
493
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200494 correction = want - device->rs_in_flight - plan->total;
Philipp Reisner778f2712010-07-06 11:14:00 +0200495
496 /* Plan ahead */
497 cps = correction / steps;
Philipp Reisner813472c2011-05-03 16:47:02 +0200498 fifo_add_val(plan, cps);
499 plan->total += cps * steps;
Philipp Reisner778f2712010-07-06 11:14:00 +0200500
501 /* What we do in this step */
Philipp Reisner813472c2011-05-03 16:47:02 +0200502 curr_corr = fifo_push(plan, 0);
503 plan->total -= curr_corr;
Philipp Reisner778f2712010-07-06 11:14:00 +0200504
505 req_sect = sect_in + curr_corr;
506 if (req_sect < 0)
507 req_sect = 0;
508
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +0200509 max_sect = (dc->c_max_rate * 2 * SLEEP_TIME) / HZ;
Philipp Reisner778f2712010-07-06 11:14:00 +0200510 if (req_sect > max_sect)
511 req_sect = max_sect;
512
513 /*
Andreas Gruenbacherd0180172011-07-03 17:53:52 +0200514 drbd_warn(device, "si=%u if=%d wa=%u co=%d st=%d cps=%d pl=%d cc=%d rs=%d\n",
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200515 sect_in, device->rs_in_flight, want, correction,
516 steps, cps, device->rs_planed, curr_corr, req_sect);
Philipp Reisner778f2712010-07-06 11:14:00 +0200517 */
518
519 return req_sect;
520}
521
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200522static int drbd_rs_number_requests(struct drbd_device *device)
Lars Ellenberge65f4402010-11-05 10:04:07 +0100523{
Lars Ellenberg0e49d7b2014-04-28 18:43:18 +0200524 unsigned int sect_in; /* Number of sectors that came in since the last turn */
525 int number, mxb;
526
527 sect_in = atomic_xchg(&device->rs_sect_in, 0);
528 device->rs_in_flight -= sect_in;
Philipp Reisner813472c2011-05-03 16:47:02 +0200529
530 rcu_read_lock();
Lars Ellenberg0e49d7b2014-04-28 18:43:18 +0200531 mxb = drbd_get_max_buffers(device) / 2;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200532 if (rcu_dereference(device->rs_plan_s)->size) {
Lars Ellenberg0e49d7b2014-04-28 18:43:18 +0200533 number = drbd_rs_controller(device, sect_in) >> (BM_BLOCK_SHIFT - 9);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200534 device->c_sync_rate = number * HZ * (BM_BLOCK_SIZE / 1024) / SLEEP_TIME;
Lars Ellenberge65f4402010-11-05 10:04:07 +0100535 } else {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200536 device->c_sync_rate = rcu_dereference(device->ldev->disk_conf)->resync_rate;
537 number = SLEEP_TIME * device->c_sync_rate / ((BM_BLOCK_SIZE / 1024) * HZ);
Lars Ellenberge65f4402010-11-05 10:04:07 +0100538 }
Philipp Reisner813472c2011-05-03 16:47:02 +0200539 rcu_read_unlock();
Lars Ellenberge65f4402010-11-05 10:04:07 +0100540
Lars Ellenberg0e49d7b2014-04-28 18:43:18 +0200541 /* Don't have more than "max-buffers"/2 in-flight.
542 * Otherwise we may cause the remote site to stall on drbd_alloc_pages(),
543 * potentially causing a distributed deadlock on congestion during
544 * online-verify or (checksum-based) resync, if max-buffers,
545 * socket buffer sizes and resync rate settings are mis-configured. */
Lars Ellenberg7f34f612014-04-22 16:37:16 +0200546
547 /* note that "number" is in units of "BM_BLOCK_SIZE" (which is 4k),
548 * mxb (as used here, and in drbd_alloc_pages on the peer) is
549 * "number of pages" (typically also 4k),
550 * but "rs_in_flight" is in "sectors" (512 Byte). */
551 if (mxb - device->rs_in_flight/8 < number)
552 number = mxb - device->rs_in_flight/8;
Lars Ellenberg0e49d7b2014-04-28 18:43:18 +0200553
Lars Ellenberge65f4402010-11-05 10:04:07 +0100554 return number;
555}
556
Lars Ellenberg44a4d552013-11-22 12:40:58 +0100557static int make_resync_request(struct drbd_device *const device, int cancel)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700558{
Lars Ellenberg44a4d552013-11-22 12:40:58 +0100559 struct drbd_peer_device *const peer_device = first_peer_device(device);
560 struct drbd_connection *const connection = peer_device ? peer_device->connection : NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700561 unsigned long bit;
562 sector_t sector;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200563 const sector_t capacity = drbd_get_capacity(device->this_bdev);
Lars Ellenberg1816a2b2010-11-11 15:19:07 +0100564 int max_bio_size;
Lars Ellenberge65f4402010-11-05 10:04:07 +0100565 int number, rollback_i, size;
Lars Ellenberg506afb62014-01-31 14:55:12 +0100566 int align, requeue = 0;
Lars Ellenberg0f0601f2010-08-11 23:40:24 +0200567 int i = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700568
569 if (unlikely(cancel))
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +0100570 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700571
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200572 if (device->rs_total == 0) {
Lars Ellenbergaf85e8e2010-10-07 16:07:55 +0200573 /* empty resync? */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200574 drbd_resync_finished(device);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +0100575 return 0;
Lars Ellenbergaf85e8e2010-10-07 16:07:55 +0200576 }
577
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200578 if (!get_ldev(device)) {
579 /* Since we only need to access device->rsync a
580 get_ldev_if_state(device,D_FAILED) would be sufficient, but
Philipp Reisnerb411b362009-09-25 16:07:19 -0700581 to continue resync with a broken disk makes no sense at
582 all */
Andreas Gruenbacherd0180172011-07-03 17:53:52 +0200583 drbd_err(device, "Disk broke down during resync!\n");
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +0100584 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700585 }
586
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200587 max_bio_size = queue_max_hw_sectors(device->rq_queue) << 9;
588 number = drbd_rs_number_requests(device);
Lars Ellenberg0e49d7b2014-04-28 18:43:18 +0200589 if (number <= 0)
Lars Ellenberg0f0601f2010-08-11 23:40:24 +0200590 goto requeue;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700591
Philipp Reisnerb411b362009-09-25 16:07:19 -0700592 for (i = 0; i < number; i++) {
Lars Ellenberg506afb62014-01-31 14:55:12 +0100593 /* Stop generating RS requests when half of the send buffer is filled,
594 * but notify TCP that we'd like to have more space. */
Lars Ellenberg44a4d552013-11-22 12:40:58 +0100595 mutex_lock(&connection->data.mutex);
596 if (connection->data.socket) {
Lars Ellenberg506afb62014-01-31 14:55:12 +0100597 struct sock *sk = connection->data.socket->sk;
598 int queued = sk->sk_wmem_queued;
599 int sndbuf = sk->sk_sndbuf;
600 if (queued > sndbuf / 2) {
601 requeue = 1;
602 if (sk->sk_socket)
603 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
604 }
605 } else
606 requeue = 1;
Lars Ellenberg44a4d552013-11-22 12:40:58 +0100607 mutex_unlock(&connection->data.mutex);
Lars Ellenberg506afb62014-01-31 14:55:12 +0100608 if (requeue)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700609 goto requeue;
610
611next_sector:
612 size = BM_BLOCK_SIZE;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200613 bit = drbd_bm_find_next(device, device->bm_resync_fo);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700614
Lars Ellenberg4b0715f2010-12-14 15:13:04 +0100615 if (bit == DRBD_END_OF_BITMAP) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200616 device->bm_resync_fo = drbd_bm_bits(device);
617 put_ldev(device);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +0100618 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700619 }
620
621 sector = BM_BIT_TO_SECT(bit);
622
Lars Ellenbergad3fee72013-12-20 11:22:13 +0100623 if (drbd_try_rs_begin_io(device, sector)) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200624 device->bm_resync_fo = bit;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700625 goto requeue;
626 }
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200627 device->bm_resync_fo = bit + 1;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700628
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200629 if (unlikely(drbd_bm_test_bit(device, bit) == 0)) {
630 drbd_rs_complete_io(device, sector);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700631 goto next_sector;
632 }
633
Lars Ellenberg1816a2b2010-11-11 15:19:07 +0100634#if DRBD_MAX_BIO_SIZE > BM_BLOCK_SIZE
Philipp Reisnerb411b362009-09-25 16:07:19 -0700635 /* try to find some adjacent bits.
636 * we stop if we have already the maximum req size.
637 *
638 * Additionally always align bigger requests, in order to
639 * be prepared for all stripe sizes of software RAIDs.
Philipp Reisnerb411b362009-09-25 16:07:19 -0700640 */
641 align = 1;
Philipp Reisnerd2074502010-07-22 15:27:27 +0200642 rollback_i = i;
Lars Ellenberg6377b922014-04-28 18:43:17 +0200643 while (i < number) {
Lars Ellenberg1816a2b2010-11-11 15:19:07 +0100644 if (size + BM_BLOCK_SIZE > max_bio_size)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700645 break;
646
647 /* Be always aligned */
648 if (sector & ((1<<(align+3))-1))
649 break;
650
651 /* do not cross extent boundaries */
652 if (((bit+1) & BM_BLOCKS_PER_BM_EXT_MASK) == 0)
653 break;
654 /* now, is it actually dirty, after all?
655 * caution, drbd_bm_test_bit is tri-state for some
656 * obscure reason; ( b == 0 ) would get the out-of-band
657 * only accidentally right because of the "oddly sized"
658 * adjustment below */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200659 if (drbd_bm_test_bit(device, bit+1) != 1)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700660 break;
661 bit++;
662 size += BM_BLOCK_SIZE;
663 if ((BM_BLOCK_SIZE << align) <= size)
664 align++;
665 i++;
666 }
667 /* if we merged some,
668 * reset the offset to start the next drbd_bm_find_next from */
669 if (size > BM_BLOCK_SIZE)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200670 device->bm_resync_fo = bit + 1;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700671#endif
672
673 /* adjust very last sectors, in case we are oddly sized */
674 if (sector + (size>>9) > capacity)
675 size = (capacity-sector)<<9;
Lars Ellenbergaaaba342014-03-18 12:30:09 +0100676
677 if (device->use_csums) {
Lars Ellenberg44a4d552013-11-22 12:40:58 +0100678 switch (read_for_csum(peer_device, sector, size)) {
Lars Ellenberg80a40e42010-08-11 23:28:00 +0200679 case -EIO: /* Disk failure */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200680 put_ldev(device);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +0100681 return -EIO;
Lars Ellenberg80a40e42010-08-11 23:28:00 +0200682 case -EAGAIN: /* allocation failed, or ldev busy */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200683 drbd_rs_complete_io(device, sector);
684 device->bm_resync_fo = BM_SECT_TO_BIT(sector);
Philipp Reisnerd2074502010-07-22 15:27:27 +0200685 i = rollback_i;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700686 goto requeue;
Lars Ellenberg80a40e42010-08-11 23:28:00 +0200687 case 0:
688 /* everything ok */
689 break;
690 default:
691 BUG();
Philipp Reisnerb411b362009-09-25 16:07:19 -0700692 }
693 } else {
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +0100694 int err;
695
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200696 inc_rs_pending(device);
Lars Ellenberg44a4d552013-11-22 12:40:58 +0100697 err = drbd_send_drequest(peer_device, P_RS_DATA_REQUEST,
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +0100698 sector, size, ID_SYNCER);
699 if (err) {
Andreas Gruenbacherd0180172011-07-03 17:53:52 +0200700 drbd_err(device, "drbd_send_drequest() failed, aborting...\n");
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200701 dec_rs_pending(device);
702 put_ldev(device);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +0100703 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700704 }
705 }
706 }
707
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200708 if (device->bm_resync_fo >= drbd_bm_bits(device)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700709 /* last syncer _request_ was sent,
710 * but the P_RS_DATA_REPLY not yet received. sync will end (and
711 * next sync group will resume), as soon as we receive the last
712 * resync data block, and the last bit is cleared.
713 * until then resync "work" is "inactive" ...
714 */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200715 put_ldev(device);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +0100716 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700717 }
718
719 requeue:
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200720 device->rs_in_flight += (i << (BM_BLOCK_SHIFT - 9));
721 mod_timer(&device->resync_timer, jiffies + SLEEP_TIME);
722 put_ldev(device);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +0100723 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700724}
725
Andreas Gruenbacherd448a2e2011-08-25 16:59:58 +0200726static int make_ov_request(struct drbd_device *device, int cancel)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700727{
728 int number, i, size;
729 sector_t sector;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200730 const sector_t capacity = drbd_get_capacity(device->this_bdev);
Lars Ellenberg58ffa582012-07-26 14:09:49 +0200731 bool stop_sector_reached = false;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700732
733 if (unlikely(cancel))
734 return 1;
735
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200736 number = drbd_rs_number_requests(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700737
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200738 sector = device->ov_position;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700739 for (i = 0; i < number; i++) {
Lars Ellenberg58ffa582012-07-26 14:09:49 +0200740 if (sector >= capacity)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700741 return 1;
Lars Ellenberg58ffa582012-07-26 14:09:49 +0200742
743 /* We check for "finished" only in the reply path:
744 * w_e_end_ov_reply().
745 * We need to send at least one request out. */
746 stop_sector_reached = i > 0
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200747 && verify_can_do_stop_sector(device)
748 && sector >= device->ov_stop_sector;
Lars Ellenberg58ffa582012-07-26 14:09:49 +0200749 if (stop_sector_reached)
750 break;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700751
752 size = BM_BLOCK_SIZE;
753
Lars Ellenbergad3fee72013-12-20 11:22:13 +0100754 if (drbd_try_rs_begin_io(device, sector)) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200755 device->ov_position = sector;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700756 goto requeue;
757 }
758
759 if (sector + (size>>9) > capacity)
760 size = (capacity-sector)<<9;
761
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200762 inc_rs_pending(device);
Andreas Gruenbacher69a22772011-08-09 00:47:13 +0200763 if (drbd_send_ov_request(first_peer_device(device), sector, size)) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200764 dec_rs_pending(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700765 return 0;
766 }
767 sector += BM_SECT_PER_BIT;
768 }
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200769 device->ov_position = sector;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700770
771 requeue:
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200772 device->rs_in_flight += (i << (BM_BLOCK_SHIFT - 9));
Lars Ellenberg58ffa582012-07-26 14:09:49 +0200773 if (i == 0 || !stop_sector_reached)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200774 mod_timer(&device->resync_timer, jiffies + SLEEP_TIME);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700775 return 1;
776}
777
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +0100778int w_ov_finished(struct drbd_work *w, int cancel)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700779{
Andreas Gruenbacher84b8c062011-07-28 15:27:51 +0200780 struct drbd_device_work *dw =
781 container_of(w, struct drbd_device_work, w);
782 struct drbd_device *device = dw->device;
783 kfree(dw);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200784 ov_out_of_sync_print(device);
785 drbd_resync_finished(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700786
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +0100787 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700788}
789
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +0100790static int w_resync_finished(struct drbd_work *w, int cancel)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700791{
Andreas Gruenbacher84b8c062011-07-28 15:27:51 +0200792 struct drbd_device_work *dw =
793 container_of(w, struct drbd_device_work, w);
794 struct drbd_device *device = dw->device;
795 kfree(dw);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700796
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200797 drbd_resync_finished(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700798
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +0100799 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700800}
801
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200802static void ping_peer(struct drbd_device *device)
Lars Ellenbergaf85e8e2010-10-07 16:07:55 +0200803{
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +0200804 struct drbd_connection *connection = first_peer_device(device)->connection;
Philipp Reisner2a67d8b2011-02-09 14:10:32 +0100805
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +0200806 clear_bit(GOT_PING_ACK, &connection->flags);
807 request_ping(connection);
808 wait_event(connection->ping_wait,
809 test_bit(GOT_PING_ACK, &connection->flags) || device->state.conn < C_CONNECTED);
Lars Ellenbergaf85e8e2010-10-07 16:07:55 +0200810}
811
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200812int drbd_resync_finished(struct drbd_device *device)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700813{
814 unsigned long db, dt, dbdt;
815 unsigned long n_oos;
816 union drbd_state os, ns;
Andreas Gruenbacher84b8c062011-07-28 15:27:51 +0200817 struct drbd_device_work *dw;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700818 char *khelper_cmd = NULL;
Lars Ellenberg26525612010-11-05 09:56:33 +0100819 int verify_done = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700820
821 /* Remove all elements from the resync LRU. Since future actions
822 * might set bits in the (main) bitmap, then the entries in the
823 * resync LRU would be wrong. */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200824 if (drbd_rs_del_all(device)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700825 /* In case this is not possible now, most probably because
826 * there are P_RS_DATA_REPLY Packets lingering on the worker's
827 * queue (or even the read operations for those packets
828 * is not finished by now). Retry in 100ms. */
829
Philipp Reisner20ee6392011-01-18 15:28:59 +0100830 schedule_timeout_interruptible(HZ / 10);
Andreas Gruenbacher84b8c062011-07-28 15:27:51 +0200831 dw = kmalloc(sizeof(struct drbd_device_work), GFP_ATOMIC);
832 if (dw) {
833 dw->w.cb = w_resync_finished;
834 dw->device = device;
835 drbd_queue_work(&first_peer_device(device)->connection->sender_work,
836 &dw->w);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700837 return 1;
838 }
Andreas Gruenbacher84b8c062011-07-28 15:27:51 +0200839 drbd_err(device, "Warn failed to drbd_rs_del_all() and to kmalloc(dw).\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700840 }
841
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200842 dt = (jiffies - device->rs_start - device->rs_paused) / HZ;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700843 if (dt <= 0)
844 dt = 1;
Andreas Gruenbacher84b8c062011-07-28 15:27:51 +0200845
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200846 db = device->rs_total;
Lars Ellenberg58ffa582012-07-26 14:09:49 +0200847 /* adjust for verify start and stop sectors, respective reached position */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200848 if (device->state.conn == C_VERIFY_S || device->state.conn == C_VERIFY_T)
849 db -= device->ov_left;
Lars Ellenberg58ffa582012-07-26 14:09:49 +0200850
Philipp Reisnerb411b362009-09-25 16:07:19 -0700851 dbdt = Bit2KB(db/dt);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200852 device->rs_paused /= HZ;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700853
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200854 if (!get_ldev(device))
Philipp Reisnerb411b362009-09-25 16:07:19 -0700855 goto out;
856
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200857 ping_peer(device);
Lars Ellenbergaf85e8e2010-10-07 16:07:55 +0200858
Andreas Gruenbacher05008132011-07-07 14:19:42 +0200859 spin_lock_irq(&device->resource->req_lock);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200860 os = drbd_read_state(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700861
Lars Ellenberg26525612010-11-05 09:56:33 +0100862 verify_done = (os.conn == C_VERIFY_S || os.conn == C_VERIFY_T);
863
Philipp Reisnerb411b362009-09-25 16:07:19 -0700864 /* This protects us against multiple calls (that can happen in the presence
865 of application IO), and against connectivity loss just before we arrive here. */
866 if (os.conn <= C_CONNECTED)
867 goto out_unlock;
868
869 ns = os;
870 ns.conn = C_CONNECTED;
871
Andreas Gruenbacherd0180172011-07-03 17:53:52 +0200872 drbd_info(device, "%s done (total %lu sec; paused %lu sec; %lu K/sec)\n",
Lars Ellenberg58ffa582012-07-26 14:09:49 +0200873 verify_done ? "Online verify" : "Resync",
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200874 dt + device->rs_paused, device->rs_paused, dbdt);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700875
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200876 n_oos = drbd_bm_total_weight(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700877
878 if (os.conn == C_VERIFY_S || os.conn == C_VERIFY_T) {
879 if (n_oos) {
Andreas Gruenbacherd0180172011-07-03 17:53:52 +0200880 drbd_alert(device, "Online verify found %lu %dk block out of sync!\n",
Philipp Reisnerb411b362009-09-25 16:07:19 -0700881 n_oos, Bit2KB(1));
882 khelper_cmd = "out-of-sync";
883 }
884 } else {
Andreas Gruenbacher0b0ba1e2011-06-27 16:23:33 +0200885 D_ASSERT(device, (n_oos - device->rs_failed) == 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700886
887 if (os.conn == C_SYNC_TARGET || os.conn == C_PAUSED_SYNC_T)
888 khelper_cmd = "after-resync-target";
889
Lars Ellenbergaaaba342014-03-18 12:30:09 +0100890 if (device->use_csums && device->rs_total) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200891 const unsigned long s = device->rs_same_csum;
892 const unsigned long t = device->rs_total;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700893 const int ratio =
894 (t == 0) ? 0 :
895 (t < 100000) ? ((s*100)/t) : (s/(t/100));
Andreas Gruenbacherd0180172011-07-03 17:53:52 +0200896 drbd_info(device, "%u %% had equal checksums, eliminated: %luK; "
Philipp Reisnerb411b362009-09-25 16:07:19 -0700897 "transferred %luK total %luK\n",
898 ratio,
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200899 Bit2KB(device->rs_same_csum),
900 Bit2KB(device->rs_total - device->rs_same_csum),
901 Bit2KB(device->rs_total));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700902 }
903 }
904
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200905 if (device->rs_failed) {
Andreas Gruenbacherd0180172011-07-03 17:53:52 +0200906 drbd_info(device, " %lu failed blocks\n", device->rs_failed);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700907
908 if (os.conn == C_SYNC_TARGET || os.conn == C_PAUSED_SYNC_T) {
909 ns.disk = D_INCONSISTENT;
910 ns.pdsk = D_UP_TO_DATE;
911 } else {
912 ns.disk = D_UP_TO_DATE;
913 ns.pdsk = D_INCONSISTENT;
914 }
915 } else {
916 ns.disk = D_UP_TO_DATE;
917 ns.pdsk = D_UP_TO_DATE;
918
919 if (os.conn == C_SYNC_TARGET || os.conn == C_PAUSED_SYNC_T) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200920 if (device->p_uuid) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700921 int i;
922 for (i = UI_BITMAP ; i <= UI_HISTORY_END ; i++)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200923 _drbd_uuid_set(device, i, device->p_uuid[i]);
924 drbd_uuid_set(device, UI_BITMAP, device->ldev->md.uuid[UI_CURRENT]);
925 _drbd_uuid_set(device, UI_CURRENT, device->p_uuid[UI_CURRENT]);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700926 } else {
Andreas Gruenbacherd0180172011-07-03 17:53:52 +0200927 drbd_err(device, "device->p_uuid is NULL! BUG\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700928 }
929 }
930
Lars Ellenberg62b0da32011-01-20 13:25:21 +0100931 if (!(os.conn == C_VERIFY_S || os.conn == C_VERIFY_T)) {
932 /* for verify runs, we don't update uuids here,
933 * so there would be nothing to report. */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200934 drbd_uuid_set_bm(device, 0UL);
935 drbd_print_uuids(device, "updated UUIDs");
936 if (device->p_uuid) {
Lars Ellenberg62b0da32011-01-20 13:25:21 +0100937 /* Now the two UUID sets are equal, update what we
938 * know of the peer. */
939 int i;
940 for (i = UI_CURRENT ; i <= UI_HISTORY_END ; i++)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200941 device->p_uuid[i] = device->ldev->md.uuid[i];
Lars Ellenberg62b0da32011-01-20 13:25:21 +0100942 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700943 }
944 }
945
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200946 _drbd_set_state(device, ns, CS_VERBOSE, NULL);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700947out_unlock:
Andreas Gruenbacher05008132011-07-07 14:19:42 +0200948 spin_unlock_irq(&device->resource->req_lock);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200949 put_ldev(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700950out:
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200951 device->rs_total = 0;
952 device->rs_failed = 0;
953 device->rs_paused = 0;
Lars Ellenberg58ffa582012-07-26 14:09:49 +0200954
955 /* reset start sector, if we reached end of device */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200956 if (verify_done && device->ov_left == 0)
957 device->ov_start_sector = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700958
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200959 drbd_md_sync(device);
Lars Ellenberg13d42682010-10-13 17:37:54 +0200960
Philipp Reisnerb411b362009-09-25 16:07:19 -0700961 if (khelper_cmd)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200962 drbd_khelper(device, khelper_cmd);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700963
964 return 1;
965}
966
967/* helper */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200968static void move_to_net_ee_or_free(struct drbd_device *device, struct drbd_peer_request *peer_req)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700969{
Andreas Gruenbacher045417f2011-04-07 21:34:24 +0200970 if (drbd_peer_req_has_active_page(peer_req)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700971 /* This might happen if sendpage() has not finished */
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +0100972 int i = (peer_req->i.size + PAGE_SIZE -1) >> PAGE_SHIFT;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200973 atomic_add(i, &device->pp_in_use_by_net);
974 atomic_sub(i, &device->pp_in_use);
Andreas Gruenbacher05008132011-07-07 14:19:42 +0200975 spin_lock_irq(&device->resource->req_lock);
Andreas Gruenbachera8cd15b2011-08-25 15:49:40 +0200976 list_add_tail(&peer_req->w.list, &device->net_ee);
Andreas Gruenbacher05008132011-07-07 14:19:42 +0200977 spin_unlock_irq(&device->resource->req_lock);
Lars Ellenberg435f0742010-09-06 12:30:25 +0200978 wake_up(&drbd_pp_wait);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700979 } else
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200980 drbd_free_peer_req(device, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700981}
982
983/**
984 * w_e_end_data_req() - Worker callback, to send a P_DATA_REPLY packet in response to a P_DATA_REQUEST
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200985 * @device: DRBD device.
Philipp Reisnerb411b362009-09-25 16:07:19 -0700986 * @w: work object.
987 * @cancel: The connection will be closed anyways
988 */
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +0100989int w_e_end_data_req(struct drbd_work *w, int cancel)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700990{
Andreas Gruenbachera8cd15b2011-08-25 15:49:40 +0200991 struct drbd_peer_request *peer_req = container_of(w, struct drbd_peer_request, w);
Andreas Gruenbacher67801392011-09-13 10:39:41 +0200992 struct drbd_peer_device *peer_device = peer_req->peer_device;
993 struct drbd_device *device = peer_device->device;
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +0100994 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700995
996 if (unlikely(cancel)) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200997 drbd_free_peer_req(device, peer_req);
998 dec_unacked(device);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +0100999 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001000 }
1001
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001002 if (likely((peer_req->flags & EE_WAS_ERROR) == 0)) {
Andreas Gruenbacher67801392011-09-13 10:39:41 +02001003 err = drbd_send_block(peer_device, P_DATA_REPLY, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001004 } else {
1005 if (__ratelimit(&drbd_ratelimit_state))
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02001006 drbd_err(device, "Sending NegDReply. sector=%llus.\n",
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001007 (unsigned long long)peer_req->i.sector);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001008
Andreas Gruenbacher67801392011-09-13 10:39:41 +02001009 err = drbd_send_ack(peer_device, P_NEG_DREPLY, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001010 }
1011
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001012 dec_unacked(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001013
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001014 move_to_net_ee_or_free(device, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001015
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001016 if (unlikely(err))
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02001017 drbd_err(device, "drbd_send_block() failed\n");
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001018 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001019}
1020
1021/**
Andreas Gruenbachera209b4a2011-08-17 12:43:25 +02001022 * w_e_end_rsdata_req() - Worker callback to send a P_RS_DATA_REPLY packet in response to a P_RS_DATA_REQUEST
Philipp Reisnerb411b362009-09-25 16:07:19 -07001023 * @w: work object.
1024 * @cancel: The connection will be closed anyways
1025 */
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001026int w_e_end_rsdata_req(struct drbd_work *w, int cancel)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001027{
Andreas Gruenbachera8cd15b2011-08-25 15:49:40 +02001028 struct drbd_peer_request *peer_req = container_of(w, struct drbd_peer_request, w);
Andreas Gruenbacher67801392011-09-13 10:39:41 +02001029 struct drbd_peer_device *peer_device = peer_req->peer_device;
1030 struct drbd_device *device = peer_device->device;
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001031 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001032
1033 if (unlikely(cancel)) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001034 drbd_free_peer_req(device, peer_req);
1035 dec_unacked(device);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001036 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001037 }
1038
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001039 if (get_ldev_if_state(device, D_FAILED)) {
1040 drbd_rs_complete_io(device, peer_req->i.sector);
1041 put_ldev(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001042 }
1043
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001044 if (device->state.conn == C_AHEAD) {
Andreas Gruenbacher67801392011-09-13 10:39:41 +02001045 err = drbd_send_ack(peer_device, P_RS_CANCEL, peer_req);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001046 } else if (likely((peer_req->flags & EE_WAS_ERROR) == 0)) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001047 if (likely(device->state.pdsk >= D_INCONSISTENT)) {
1048 inc_rs_pending(device);
Andreas Gruenbacher67801392011-09-13 10:39:41 +02001049 err = drbd_send_block(peer_device, P_RS_DATA_REPLY, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001050 } else {
1051 if (__ratelimit(&drbd_ratelimit_state))
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02001052 drbd_err(device, "Not sending RSDataReply, "
Philipp Reisnerb411b362009-09-25 16:07:19 -07001053 "partner DISKLESS!\n");
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001054 err = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001055 }
1056 } else {
1057 if (__ratelimit(&drbd_ratelimit_state))
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02001058 drbd_err(device, "Sending NegRSDReply. sector %llus.\n",
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001059 (unsigned long long)peer_req->i.sector);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001060
Andreas Gruenbacher67801392011-09-13 10:39:41 +02001061 err = drbd_send_ack(peer_device, P_NEG_RS_DREPLY, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001062
1063 /* update resync data with failure */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001064 drbd_rs_failed_io(device, peer_req->i.sector, peer_req->i.size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001065 }
1066
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001067 dec_unacked(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001068
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001069 move_to_net_ee_or_free(device, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001070
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001071 if (unlikely(err))
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02001072 drbd_err(device, "drbd_send_block() failed\n");
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001073 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001074}
1075
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001076int w_e_end_csum_rs_req(struct drbd_work *w, int cancel)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001077{
Andreas Gruenbachera8cd15b2011-08-25 15:49:40 +02001078 struct drbd_peer_request *peer_req = container_of(w, struct drbd_peer_request, w);
Andreas Gruenbacher67801392011-09-13 10:39:41 +02001079 struct drbd_peer_device *peer_device = peer_req->peer_device;
1080 struct drbd_device *device = peer_device->device;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001081 struct digest_info *di;
1082 int digest_size;
1083 void *digest = NULL;
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001084 int err, eq = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001085
1086 if (unlikely(cancel)) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001087 drbd_free_peer_req(device, peer_req);
1088 dec_unacked(device);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001089 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001090 }
1091
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001092 if (get_ldev(device)) {
1093 drbd_rs_complete_io(device, peer_req->i.sector);
1094 put_ldev(device);
Lars Ellenberg1d53f092010-09-05 01:13:24 +02001095 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001096
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001097 di = peer_req->digest;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001098
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001099 if (likely((peer_req->flags & EE_WAS_ERROR) == 0)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001100 /* quick hack to try to avoid a race against reconfiguration.
1101 * a real fix would be much more involved,
1102 * introducing more locking mechanisms */
Andreas Gruenbacher67801392011-09-13 10:39:41 +02001103 if (peer_device->connection->csums_tfm) {
1104 digest_size = crypto_hash_digestsize(peer_device->connection->csums_tfm);
Andreas Gruenbacher0b0ba1e2011-06-27 16:23:33 +02001105 D_ASSERT(device, digest_size == di->digest_size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001106 digest = kmalloc(digest_size, GFP_NOIO);
1107 }
1108 if (digest) {
Andreas Gruenbacher67801392011-09-13 10:39:41 +02001109 drbd_csum_ee(peer_device->connection->csums_tfm, peer_req, digest);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001110 eq = !memcmp(digest, di->digest, digest_size);
1111 kfree(digest);
1112 }
1113
1114 if (eq) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001115 drbd_set_in_sync(device, peer_req->i.sector, peer_req->i.size);
Lars Ellenberg676396d2010-03-03 02:08:22 +01001116 /* rs_same_csums unit is BM_BLOCK_SIZE */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001117 device->rs_same_csum += peer_req->i.size >> BM_BLOCK_SHIFT;
Andreas Gruenbacher67801392011-09-13 10:39:41 +02001118 err = drbd_send_ack(peer_device, P_RS_IS_IN_SYNC, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001119 } else {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001120 inc_rs_pending(device);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001121 peer_req->block_id = ID_SYNCER; /* By setting block_id, digest pointer becomes invalid! */
1122 peer_req->flags &= ~EE_HAS_DIGEST; /* This peer request no longer has a digest pointer */
Philipp Reisner204bba92010-08-23 16:17:13 +02001123 kfree(di);
Andreas Gruenbacher67801392011-09-13 10:39:41 +02001124 err = drbd_send_block(peer_device, P_RS_DATA_REPLY, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001125 }
1126 } else {
Andreas Gruenbacher67801392011-09-13 10:39:41 +02001127 err = drbd_send_ack(peer_device, P_NEG_RS_DREPLY, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001128 if (__ratelimit(&drbd_ratelimit_state))
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02001129 drbd_err(device, "Sending NegDReply. I guess it gets messy.\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07001130 }
1131
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001132 dec_unacked(device);
1133 move_to_net_ee_or_free(device, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001134
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001135 if (unlikely(err))
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02001136 drbd_err(device, "drbd_send_block/ack() failed\n");
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001137 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001138}
1139
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001140int w_e_end_ov_req(struct drbd_work *w, int cancel)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001141{
Andreas Gruenbachera8cd15b2011-08-25 15:49:40 +02001142 struct drbd_peer_request *peer_req = container_of(w, struct drbd_peer_request, w);
Andreas Gruenbacher67801392011-09-13 10:39:41 +02001143 struct drbd_peer_device *peer_device = peer_req->peer_device;
1144 struct drbd_device *device = peer_device->device;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001145 sector_t sector = peer_req->i.sector;
1146 unsigned int size = peer_req->i.size;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001147 int digest_size;
1148 void *digest;
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001149 int err = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001150
1151 if (unlikely(cancel))
1152 goto out;
1153
Andreas Gruenbacher67801392011-09-13 10:39:41 +02001154 digest_size = crypto_hash_digestsize(peer_device->connection->verify_tfm);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001155 digest = kmalloc(digest_size, GFP_NOIO);
Philipp Reisner8f214202011-03-01 15:52:35 +01001156 if (!digest) {
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001157 err = 1; /* terminate the connection in case the allocation failed */
Philipp Reisner8f214202011-03-01 15:52:35 +01001158 goto out;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001159 }
1160
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001161 if (likely(!(peer_req->flags & EE_WAS_ERROR)))
Andreas Gruenbacher67801392011-09-13 10:39:41 +02001162 drbd_csum_ee(peer_device->connection->verify_tfm, peer_req, digest);
Philipp Reisner8f214202011-03-01 15:52:35 +01001163 else
1164 memset(digest, 0, digest_size);
1165
Lars Ellenberg53ea4332011-03-08 17:11:40 +01001166 /* Free e and pages before send.
1167 * In case we block on congestion, we could otherwise run into
1168 * some distributed deadlock, if the other side blocks on
1169 * congestion as well, because our receiver blocks in
Andreas Gruenbacherc37c8ec2011-04-07 21:02:09 +02001170 * drbd_alloc_pages due to pp_in_use > max_buffers. */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001171 drbd_free_peer_req(device, peer_req);
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001172 peer_req = NULL;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001173 inc_rs_pending(device);
Andreas Gruenbacher67801392011-09-13 10:39:41 +02001174 err = drbd_send_drequest_csum(peer_device, sector, size, digest, digest_size, P_OV_REPLY);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001175 if (err)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001176 dec_rs_pending(device);
Philipp Reisner8f214202011-03-01 15:52:35 +01001177 kfree(digest);
1178
Philipp Reisnerb411b362009-09-25 16:07:19 -07001179out:
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001180 if (peer_req)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001181 drbd_free_peer_req(device, peer_req);
1182 dec_unacked(device);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001183 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001184}
1185
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001186void drbd_ov_out_of_sync_found(struct drbd_device *device, sector_t sector, int size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001187{
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001188 if (device->ov_last_oos_start + device->ov_last_oos_size == sector) {
1189 device->ov_last_oos_size += size>>9;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001190 } else {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001191 device->ov_last_oos_start = sector;
1192 device->ov_last_oos_size = size>>9;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001193 }
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001194 drbd_set_out_of_sync(device, sector, size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001195}
1196
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001197int w_e_end_ov_reply(struct drbd_work *w, int cancel)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001198{
Andreas Gruenbachera8cd15b2011-08-25 15:49:40 +02001199 struct drbd_peer_request *peer_req = container_of(w, struct drbd_peer_request, w);
Andreas Gruenbacher67801392011-09-13 10:39:41 +02001200 struct drbd_peer_device *peer_device = peer_req->peer_device;
1201 struct drbd_device *device = peer_device->device;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001202 struct digest_info *di;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001203 void *digest;
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001204 sector_t sector = peer_req->i.sector;
1205 unsigned int size = peer_req->i.size;
Lars Ellenberg53ea4332011-03-08 17:11:40 +01001206 int digest_size;
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001207 int err, eq = 0;
Lars Ellenberg58ffa582012-07-26 14:09:49 +02001208 bool stop_sector_reached = false;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001209
1210 if (unlikely(cancel)) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001211 drbd_free_peer_req(device, peer_req);
1212 dec_unacked(device);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001213 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001214 }
1215
1216 /* after "cancel", because after drbd_disconnect/drbd_rs_cancel_all
1217 * the resync lru has been cleaned up already */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001218 if (get_ldev(device)) {
1219 drbd_rs_complete_io(device, peer_req->i.sector);
1220 put_ldev(device);
Lars Ellenberg1d53f092010-09-05 01:13:24 +02001221 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001222
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001223 di = peer_req->digest;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001224
Andreas Gruenbacherdb830c42011-02-04 15:57:48 +01001225 if (likely((peer_req->flags & EE_WAS_ERROR) == 0)) {
Andreas Gruenbacher67801392011-09-13 10:39:41 +02001226 digest_size = crypto_hash_digestsize(peer_device->connection->verify_tfm);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001227 digest = kmalloc(digest_size, GFP_NOIO);
1228 if (digest) {
Andreas Gruenbacher67801392011-09-13 10:39:41 +02001229 drbd_csum_ee(peer_device->connection->verify_tfm, peer_req, digest);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001230
Andreas Gruenbacher0b0ba1e2011-06-27 16:23:33 +02001231 D_ASSERT(device, digest_size == di->digest_size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001232 eq = !memcmp(digest, di->digest, digest_size);
1233 kfree(digest);
1234 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001235 }
1236
Lars Ellenberg9676c762011-02-22 14:02:31 +01001237 /* Free peer_req and pages before send.
1238 * In case we block on congestion, we could otherwise run into
1239 * some distributed deadlock, if the other side blocks on
1240 * congestion as well, because our receiver blocks in
Andreas Gruenbacherc37c8ec2011-04-07 21:02:09 +02001241 * drbd_alloc_pages due to pp_in_use > max_buffers. */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001242 drbd_free_peer_req(device, peer_req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001243 if (!eq)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001244 drbd_ov_out_of_sync_found(device, sector, size);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001245 else
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001246 ov_out_of_sync_print(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001247
Andreas Gruenbacher67801392011-09-13 10:39:41 +02001248 err = drbd_send_ack_ex(peer_device, P_OV_RESULT, sector, size,
Andreas Gruenbacherfa79abd2011-03-16 01:31:39 +01001249 eq ? ID_IN_SYNC : ID_OUT_OF_SYNC);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001250
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001251 dec_unacked(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001252
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001253 --device->ov_left;
Lars Ellenbergea5442a2010-11-05 09:48:01 +01001254
1255 /* let's advance progress step marks only for every other megabyte */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001256 if ((device->ov_left & 0x200) == 0x200)
1257 drbd_advance_rs_marks(device, device->ov_left);
Lars Ellenbergea5442a2010-11-05 09:48:01 +01001258
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001259 stop_sector_reached = verify_can_do_stop_sector(device) &&
1260 (sector + (size>>9)) >= device->ov_stop_sector;
Lars Ellenberg58ffa582012-07-26 14:09:49 +02001261
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001262 if (device->ov_left == 0 || stop_sector_reached) {
1263 ov_out_of_sync_print(device);
1264 drbd_resync_finished(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001265 }
1266
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001267 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001268}
1269
Lars Ellenbergb6dd1a82011-11-28 15:04:49 +01001270/* FIXME
1271 * We need to track the number of pending barrier acks,
1272 * and to be able to wait for them.
1273 * See also comment in drbd_adm_attach before drbd_suspend_io.
1274 */
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001275static int drbd_send_barrier(struct drbd_connection *connection)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001276{
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02001277 struct p_barrier *p;
Lars Ellenbergb6dd1a82011-11-28 15:04:49 +01001278 struct drbd_socket *sock;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001279
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001280 sock = &connection->data;
1281 p = conn_prepare_command(connection, sock);
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02001282 if (!p)
1283 return -EIO;
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001284 p->barrier = connection->send.current_epoch_nr;
Lars Ellenbergb6dd1a82011-11-28 15:04:49 +01001285 p->pad = 0;
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001286 connection->send.current_epoch_writes = 0;
Lars Ellenbergb6dd1a82011-11-28 15:04:49 +01001287
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001288 return conn_send_command(connection, sock, P_BARRIER, sizeof(*p), NULL, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001289}
1290
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001291int w_send_write_hint(struct drbd_work *w, int cancel)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001292{
Andreas Gruenbacher84b8c062011-07-28 15:27:51 +02001293 struct drbd_device *device =
1294 container_of(w, struct drbd_device, unplug_work);
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02001295 struct drbd_socket *sock;
1296
Philipp Reisnerb411b362009-09-25 16:07:19 -07001297 if (cancel)
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001298 return 0;
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02001299 sock = &first_peer_device(device)->connection->data;
Andreas Gruenbacher69a22772011-08-09 00:47:13 +02001300 if (!drbd_prepare_command(first_peer_device(device), sock))
Andreas Gruenbacher9f5bdc32011-03-28 14:23:08 +02001301 return -EIO;
Andreas Gruenbacher69a22772011-08-09 00:47:13 +02001302 return drbd_send_command(first_peer_device(device), sock, P_UNPLUG_REMOTE, 0, NULL, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001303}
1304
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001305static void re_init_if_first_write(struct drbd_connection *connection, unsigned int epoch)
Lars Ellenberg4eb9b3c2012-08-20 11:05:23 +02001306{
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001307 if (!connection->send.seen_any_write_yet) {
1308 connection->send.seen_any_write_yet = true;
1309 connection->send.current_epoch_nr = epoch;
1310 connection->send.current_epoch_writes = 0;
Lars Ellenberg4eb9b3c2012-08-20 11:05:23 +02001311 }
1312}
1313
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001314static void maybe_send_barrier(struct drbd_connection *connection, unsigned int epoch)
Lars Ellenberg4eb9b3c2012-08-20 11:05:23 +02001315{
1316 /* re-init if first write on this connection */
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001317 if (!connection->send.seen_any_write_yet)
Lars Ellenberg4eb9b3c2012-08-20 11:05:23 +02001318 return;
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001319 if (connection->send.current_epoch_nr != epoch) {
1320 if (connection->send.current_epoch_writes)
1321 drbd_send_barrier(connection);
1322 connection->send.current_epoch_nr = epoch;
Lars Ellenberg4eb9b3c2012-08-20 11:05:23 +02001323 }
1324}
1325
Andreas Gruenbacher8f7bed72010-12-19 23:53:14 +01001326int w_send_out_of_sync(struct drbd_work *w, int cancel)
Philipp Reisner73a01a12010-10-27 14:33:00 +02001327{
1328 struct drbd_request *req = container_of(w, struct drbd_request, w);
Andreas Gruenbacher84b8c062011-07-28 15:27:51 +02001329 struct drbd_device *device = req->device;
Lars Ellenberg44a4d552013-11-22 12:40:58 +01001330 struct drbd_peer_device *const peer_device = first_peer_device(device);
1331 struct drbd_connection *const connection = peer_device->connection;
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001332 int err;
Philipp Reisner73a01a12010-10-27 14:33:00 +02001333
1334 if (unlikely(cancel)) {
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01001335 req_mod(req, SEND_CANCELED);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001336 return 0;
Philipp Reisner73a01a12010-10-27 14:33:00 +02001337 }
Lars Ellenberge5f891b2013-11-22 12:32:01 +01001338 req->pre_send_jif = jiffies;
Philipp Reisner73a01a12010-10-27 14:33:00 +02001339
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001340 /* this time, no connection->send.current_epoch_writes++;
Lars Ellenbergb6dd1a82011-11-28 15:04:49 +01001341 * If it was sent, it was the closing barrier for the last
1342 * replicated epoch, before we went into AHEAD mode.
1343 * No more barriers will be sent, until we leave AHEAD mode again. */
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001344 maybe_send_barrier(connection, req->epoch);
Lars Ellenbergb6dd1a82011-11-28 15:04:49 +01001345
Lars Ellenberg44a4d552013-11-22 12:40:58 +01001346 err = drbd_send_out_of_sync(peer_device, req);
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01001347 req_mod(req, OOS_HANDED_TO_NETWORK);
Philipp Reisner73a01a12010-10-27 14:33:00 +02001348
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001349 return err;
Philipp Reisner73a01a12010-10-27 14:33:00 +02001350}
1351
Philipp Reisnerb411b362009-09-25 16:07:19 -07001352/**
1353 * w_send_dblock() - Worker callback to send a P_DATA packet in order to mirror a write request
Philipp Reisnerb411b362009-09-25 16:07:19 -07001354 * @w: work object.
1355 * @cancel: The connection will be closed anyways
1356 */
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001357int w_send_dblock(struct drbd_work *w, int cancel)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001358{
1359 struct drbd_request *req = container_of(w, struct drbd_request, w);
Andreas Gruenbacher84b8c062011-07-28 15:27:51 +02001360 struct drbd_device *device = req->device;
Lars Ellenberg44a4d552013-11-22 12:40:58 +01001361 struct drbd_peer_device *const peer_device = first_peer_device(device);
1362 struct drbd_connection *connection = peer_device->connection;
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001363 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001364
1365 if (unlikely(cancel)) {
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01001366 req_mod(req, SEND_CANCELED);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001367 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001368 }
Lars Ellenberge5f891b2013-11-22 12:32:01 +01001369 req->pre_send_jif = jiffies;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001370
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001371 re_init_if_first_write(connection, req->epoch);
1372 maybe_send_barrier(connection, req->epoch);
1373 connection->send.current_epoch_writes++;
Lars Ellenbergb6dd1a82011-11-28 15:04:49 +01001374
Lars Ellenberg44a4d552013-11-22 12:40:58 +01001375 err = drbd_send_dblock(peer_device, req);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001376 req_mod(req, err ? SEND_FAILED : HANDED_OVER_TO_NETWORK);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001377
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001378 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001379}
1380
1381/**
1382 * w_send_read_req() - Worker callback to send a read request (P_DATA_REQUEST) packet
Philipp Reisnerb411b362009-09-25 16:07:19 -07001383 * @w: work object.
1384 * @cancel: The connection will be closed anyways
1385 */
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001386int w_send_read_req(struct drbd_work *w, int cancel)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001387{
1388 struct drbd_request *req = container_of(w, struct drbd_request, w);
Andreas Gruenbacher84b8c062011-07-28 15:27:51 +02001389 struct drbd_device *device = req->device;
Lars Ellenberg44a4d552013-11-22 12:40:58 +01001390 struct drbd_peer_device *const peer_device = first_peer_device(device);
1391 struct drbd_connection *connection = peer_device->connection;
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001392 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001393
1394 if (unlikely(cancel)) {
Andreas Gruenbacher8554df12011-01-25 15:37:43 +01001395 req_mod(req, SEND_CANCELED);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001396 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001397 }
Lars Ellenberge5f891b2013-11-22 12:32:01 +01001398 req->pre_send_jif = jiffies;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001399
Lars Ellenbergb6dd1a82011-11-28 15:04:49 +01001400 /* Even read requests may close a write epoch,
1401 * if there was any yet. */
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001402 maybe_send_barrier(connection, req->epoch);
Lars Ellenbergb6dd1a82011-11-28 15:04:49 +01001403
Lars Ellenberg44a4d552013-11-22 12:40:58 +01001404 err = drbd_send_drequest(peer_device, P_DATA_REQUEST, req->i.sector, req->i.size,
Andreas Gruenbacher6c1005e2011-03-16 01:34:24 +01001405 (unsigned long)req);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001406
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001407 req_mod(req, err ? SEND_FAILED : HANDED_OVER_TO_NETWORK);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001408
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001409 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001410}
1411
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001412int w_restart_disk_io(struct drbd_work *w, int cancel)
Philipp Reisner265be2d2010-05-31 10:14:17 +02001413{
1414 struct drbd_request *req = container_of(w, struct drbd_request, w);
Andreas Gruenbacher84b8c062011-07-28 15:27:51 +02001415 struct drbd_device *device = req->device;
Philipp Reisner265be2d2010-05-31 10:14:17 +02001416
Philipp Reisner07782862010-08-31 12:00:50 +02001417 if (bio_data_dir(req->master_bio) == WRITE && req->rq_state & RQ_IN_ACT_LOG)
Lars Ellenberg4dd726f2014-02-11 11:15:36 +01001418 drbd_al_begin_io(device, &req->i);
Philipp Reisner265be2d2010-05-31 10:14:17 +02001419
1420 drbd_req_make_private_bio(req, req->master_bio);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001421 req->private_bio->bi_bdev = device->ldev->backing_bdev;
Philipp Reisner265be2d2010-05-31 10:14:17 +02001422 generic_make_request(req->private_bio);
1423
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +01001424 return 0;
Philipp Reisner265be2d2010-05-31 10:14:17 +02001425}
1426
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001427static int _drbd_may_sync_now(struct drbd_device *device)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001428{
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001429 struct drbd_device *odev = device;
Andreas Gruenbacher95f8efd2011-05-12 11:15:34 +02001430 int resync_after;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001431
1432 while (1) {
Lars Ellenberga3f8f7d2013-03-27 14:08:43 +01001433 if (!odev->ldev || odev->state.disk == D_DISKLESS)
Philipp Reisner438c8372011-03-28 14:48:01 +02001434 return 1;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001435 rcu_read_lock();
Andreas Gruenbacher95f8efd2011-05-12 11:15:34 +02001436 resync_after = rcu_dereference(odev->ldev->disk_conf)->resync_after;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001437 rcu_read_unlock();
Andreas Gruenbacher95f8efd2011-05-12 11:15:34 +02001438 if (resync_after == -1)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001439 return 1;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001440 odev = minor_to_device(resync_after);
Lars Ellenberga3f8f7d2013-03-27 14:08:43 +01001441 if (!odev)
Andreas Gruenbacher841ce242010-12-15 19:31:20 +01001442 return 1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001443 if ((odev->state.conn >= C_SYNC_SOURCE &&
1444 odev->state.conn <= C_PAUSED_SYNC_T) ||
1445 odev->state.aftr_isp || odev->state.peer_isp ||
1446 odev->state.user_isp)
1447 return 0;
1448 }
1449}
1450
1451/**
Andreas Gruenbacher28bc3b82014-08-14 18:33:30 +02001452 * drbd_pause_after() - Pause resync on all devices that may not resync now
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001453 * @device: DRBD device.
Philipp Reisnerb411b362009-09-25 16:07:19 -07001454 *
1455 * Called from process context only (admin command and after_state_ch).
1456 */
Andreas Gruenbacher28bc3b82014-08-14 18:33:30 +02001457static bool drbd_pause_after(struct drbd_device *device)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001458{
Andreas Gruenbacher28bc3b82014-08-14 18:33:30 +02001459 bool changed = false;
Andreas Gruenbacher54761692011-05-30 16:15:21 +02001460 struct drbd_device *odev;
Andreas Gruenbacher28bc3b82014-08-14 18:33:30 +02001461 int i;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001462
Philipp Reisner695d08f2011-04-11 22:53:32 -07001463 rcu_read_lock();
Andreas Gruenbacher05a10ec2011-06-07 22:54:17 +02001464 idr_for_each_entry(&drbd_devices, odev, i) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001465 if (odev->state.conn == C_STANDALONE && odev->state.disk == D_DISKLESS)
1466 continue;
Andreas Gruenbacher28bc3b82014-08-14 18:33:30 +02001467 if (!_drbd_may_sync_now(odev) &&
1468 _drbd_set_state(_NS(odev, aftr_isp, 1),
1469 CS_HARD, NULL) != SS_NOTHING_TO_DO)
1470 changed = true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001471 }
Philipp Reisner695d08f2011-04-11 22:53:32 -07001472 rcu_read_unlock();
Philipp Reisnerb411b362009-09-25 16:07:19 -07001473
Andreas Gruenbacher28bc3b82014-08-14 18:33:30 +02001474 return changed;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001475}
1476
1477/**
Andreas Gruenbacher28bc3b82014-08-14 18:33:30 +02001478 * drbd_resume_next() - Resume resync on all devices that may resync now
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001479 * @device: DRBD device.
Philipp Reisnerb411b362009-09-25 16:07:19 -07001480 *
1481 * Called from process context only (admin command and worker).
1482 */
Andreas Gruenbacher28bc3b82014-08-14 18:33:30 +02001483static bool drbd_resume_next(struct drbd_device *device)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001484{
Andreas Gruenbacher28bc3b82014-08-14 18:33:30 +02001485 bool changed = false;
Andreas Gruenbacher54761692011-05-30 16:15:21 +02001486 struct drbd_device *odev;
Andreas Gruenbacher28bc3b82014-08-14 18:33:30 +02001487 int i;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001488
Philipp Reisner695d08f2011-04-11 22:53:32 -07001489 rcu_read_lock();
Andreas Gruenbacher05a10ec2011-06-07 22:54:17 +02001490 idr_for_each_entry(&drbd_devices, odev, i) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001491 if (odev->state.conn == C_STANDALONE && odev->state.disk == D_DISKLESS)
1492 continue;
1493 if (odev->state.aftr_isp) {
Andreas Gruenbacher28bc3b82014-08-14 18:33:30 +02001494 if (_drbd_may_sync_now(odev) &&
1495 _drbd_set_state(_NS(odev, aftr_isp, 0),
1496 CS_HARD, NULL) != SS_NOTHING_TO_DO)
1497 changed = true;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001498 }
1499 }
Philipp Reisner695d08f2011-04-11 22:53:32 -07001500 rcu_read_unlock();
Andreas Gruenbacher28bc3b82014-08-14 18:33:30 +02001501 return changed;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001502}
1503
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001504void resume_next_sg(struct drbd_device *device)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001505{
Andreas Gruenbacher28bc3b82014-08-14 18:33:30 +02001506 lock_all_resources();
1507 drbd_resume_next(device);
1508 unlock_all_resources();
Philipp Reisnerb411b362009-09-25 16:07:19 -07001509}
1510
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001511void suspend_other_sg(struct drbd_device *device)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001512{
Andreas Gruenbacher28bc3b82014-08-14 18:33:30 +02001513 lock_all_resources();
1514 drbd_pause_after(device);
1515 unlock_all_resources();
Philipp Reisnerb411b362009-09-25 16:07:19 -07001516}
1517
Andreas Gruenbacher28bc3b82014-08-14 18:33:30 +02001518/* caller must lock_all_resources() */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001519enum drbd_ret_code drbd_resync_after_valid(struct drbd_device *device, int o_minor)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001520{
Andreas Gruenbacher54761692011-05-30 16:15:21 +02001521 struct drbd_device *odev;
Andreas Gruenbacher95f8efd2011-05-12 11:15:34 +02001522 int resync_after;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001523
1524 if (o_minor == -1)
1525 return NO_ERROR;
Lars Ellenberga3f8f7d2013-03-27 14:08:43 +01001526 if (o_minor < -1 || o_minor > MINORMASK)
Andreas Gruenbacher95f8efd2011-05-12 11:15:34 +02001527 return ERR_RESYNC_AFTER;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001528
1529 /* check for loops */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001530 odev = minor_to_device(o_minor);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001531 while (1) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001532 if (odev == device)
Andreas Gruenbacher95f8efd2011-05-12 11:15:34 +02001533 return ERR_RESYNC_AFTER_CYCLE;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001534
Lars Ellenberga3f8f7d2013-03-27 14:08:43 +01001535 /* You are free to depend on diskless, non-existing,
1536 * or not yet/no longer existing minors.
1537 * We only reject dependency loops.
1538 * We cannot follow the dependency chain beyond a detached or
1539 * missing minor.
1540 */
1541 if (!odev || !odev->ldev || odev->state.disk == D_DISKLESS)
1542 return NO_ERROR;
1543
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001544 rcu_read_lock();
Andreas Gruenbacher95f8efd2011-05-12 11:15:34 +02001545 resync_after = rcu_dereference(odev->ldev->disk_conf)->resync_after;
Philipp Reisnerdaeda1c2011-05-03 15:00:55 +02001546 rcu_read_unlock();
Philipp Reisnerb411b362009-09-25 16:07:19 -07001547 /* dependency chain ends here, no cycles. */
Andreas Gruenbacher95f8efd2011-05-12 11:15:34 +02001548 if (resync_after == -1)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001549 return NO_ERROR;
1550
1551 /* follow the dependency chain */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001552 odev = minor_to_device(resync_after);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001553 }
1554}
1555
Andreas Gruenbacher28bc3b82014-08-14 18:33:30 +02001556/* caller must lock_all_resources() */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001557void drbd_resync_after_changed(struct drbd_device *device)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001558{
Andreas Gruenbacher28bc3b82014-08-14 18:33:30 +02001559 int changed;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001560
Philipp Reisnerdc97b702011-05-03 14:27:15 +02001561 do {
Andreas Gruenbacher28bc3b82014-08-14 18:33:30 +02001562 changed = drbd_pause_after(device);
1563 changed |= drbd_resume_next(device);
1564 } while (changed);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001565}
1566
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001567void drbd_rs_controller_reset(struct drbd_device *device)
Lars Ellenberg9bd28d32010-11-05 09:55:18 +01001568{
Lars Ellenbergff8bd882014-11-10 17:21:12 +01001569 struct gendisk *disk = device->ldev->backing_bdev->bd_contains->bd_disk;
Philipp Reisner813472c2011-05-03 16:47:02 +02001570 struct fifo_buffer *plan;
1571
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001572 atomic_set(&device->rs_sect_in, 0);
1573 atomic_set(&device->rs_sect_ev, 0);
1574 device->rs_in_flight = 0;
Lars Ellenbergff8bd882014-11-10 17:21:12 +01001575 device->rs_last_events =
1576 (int)part_stat_read(&disk->part0, sectors[0]) +
1577 (int)part_stat_read(&disk->part0, sectors[1]);
Philipp Reisner813472c2011-05-03 16:47:02 +02001578
1579 /* Updating the RCU protected object in place is necessary since
1580 this function gets called from atomic context.
1581 It is valid since all other updates also lead to an completely
1582 empty fifo */
1583 rcu_read_lock();
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001584 plan = rcu_dereference(device->rs_plan_s);
Philipp Reisner813472c2011-05-03 16:47:02 +02001585 plan->total = 0;
1586 fifo_set(plan, 0);
1587 rcu_read_unlock();
Lars Ellenberg9bd28d32010-11-05 09:55:18 +01001588}
1589
Philipp Reisner1f04af32011-02-07 11:33:59 +01001590void start_resync_timer_fn(unsigned long data)
1591{
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001592 struct drbd_device *device = (struct drbd_device *) data;
Lars Ellenbergac0acb92014-02-11 09:47:58 +01001593 drbd_device_post_work(device, RS_START);
Philipp Reisner1f04af32011-02-07 11:33:59 +01001594}
1595
Lars Ellenbergac0acb92014-02-11 09:47:58 +01001596static void do_start_resync(struct drbd_device *device)
Philipp Reisner1f04af32011-02-07 11:33:59 +01001597{
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001598 if (atomic_read(&device->unacked_cnt) || atomic_read(&device->rs_pending_cnt)) {
Lars Ellenbergac0acb92014-02-11 09:47:58 +01001599 drbd_warn(device, "postponing start_resync ...\n");
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001600 device->start_resync_timer.expires = jiffies + HZ/10;
1601 add_timer(&device->start_resync_timer);
Lars Ellenbergac0acb92014-02-11 09:47:58 +01001602 return;
Philipp Reisner1f04af32011-02-07 11:33:59 +01001603 }
1604
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001605 drbd_start_resync(device, C_SYNC_SOURCE);
1606 clear_bit(AHEAD_TO_SYNC_SOURCE, &device->flags);
Philipp Reisner1f04af32011-02-07 11:33:59 +01001607}
1608
Lars Ellenbergaaaba342014-03-18 12:30:09 +01001609static bool use_checksum_based_resync(struct drbd_connection *connection, struct drbd_device *device)
1610{
1611 bool csums_after_crash_only;
1612 rcu_read_lock();
1613 csums_after_crash_only = rcu_dereference(connection->net_conf)->csums_after_crash_only;
1614 rcu_read_unlock();
1615 return connection->agreed_pro_version >= 89 && /* supported? */
1616 connection->csums_tfm && /* configured? */
1617 (csums_after_crash_only == 0 /* use for each resync? */
1618 || test_bit(CRASHED_PRIMARY, &device->flags)); /* or only after Primary crash? */
1619}
1620
Philipp Reisnerb411b362009-09-25 16:07:19 -07001621/**
1622 * drbd_start_resync() - Start the resync process
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001623 * @device: DRBD device.
Philipp Reisnerb411b362009-09-25 16:07:19 -07001624 * @side: Either C_SYNC_SOURCE or C_SYNC_TARGET
1625 *
1626 * This function might bring you directly into one of the
1627 * C_PAUSED_SYNC_* states.
1628 */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001629void drbd_start_resync(struct drbd_device *device, enum drbd_conns side)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001630{
Lars Ellenberg44a4d552013-11-22 12:40:58 +01001631 struct drbd_peer_device *peer_device = first_peer_device(device);
1632 struct drbd_connection *connection = peer_device ? peer_device->connection : NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001633 union drbd_state ns;
1634 int r;
1635
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001636 if (device->state.conn >= C_SYNC_SOURCE && device->state.conn < C_AHEAD) {
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02001637 drbd_err(device, "Resync already running!\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07001638 return;
1639 }
1640
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001641 if (!test_bit(B_RS_H_DONE, &device->flags)) {
Philipp Reisnere64a3292011-02-05 17:34:11 +01001642 if (side == C_SYNC_TARGET) {
1643 /* Since application IO was locked out during C_WF_BITMAP_T and
1644 C_WF_SYNC_UUID we are still unmodified. Before going to C_SYNC_TARGET
1645 we check that we might make the data inconsistent. */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001646 r = drbd_khelper(device, "before-resync-target");
Philipp Reisnere64a3292011-02-05 17:34:11 +01001647 r = (r >> 8) & 0xff;
1648 if (r > 0) {
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02001649 drbd_info(device, "before-resync-target handler returned %d, "
Philipp Reisner09b9e792010-12-03 16:04:24 +01001650 "dropping connection.\n", r);
Lars Ellenberg44a4d552013-11-22 12:40:58 +01001651 conn_request_state(connection, NS(conn, C_DISCONNECTING), CS_HARD);
Philipp Reisner09b9e792010-12-03 16:04:24 +01001652 return;
1653 }
Philipp Reisnere64a3292011-02-05 17:34:11 +01001654 } else /* C_SYNC_SOURCE */ {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001655 r = drbd_khelper(device, "before-resync-source");
Philipp Reisnere64a3292011-02-05 17:34:11 +01001656 r = (r >> 8) & 0xff;
1657 if (r > 0) {
1658 if (r == 3) {
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02001659 drbd_info(device, "before-resync-source handler returned %d, "
Philipp Reisnere64a3292011-02-05 17:34:11 +01001660 "ignoring. Old userland tools?", r);
1661 } else {
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02001662 drbd_info(device, "before-resync-source handler returned %d, "
Philipp Reisnere64a3292011-02-05 17:34:11 +01001663 "dropping connection.\n", r);
Lars Ellenberg44a4d552013-11-22 12:40:58 +01001664 conn_request_state(connection,
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +02001665 NS(conn, C_DISCONNECTING), CS_HARD);
Philipp Reisnere64a3292011-02-05 17:34:11 +01001666 return;
1667 }
1668 }
Philipp Reisner09b9e792010-12-03 16:04:24 +01001669 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001670 }
1671
Lars Ellenberg44a4d552013-11-22 12:40:58 +01001672 if (current == connection->worker.task) {
Philipp Reisnerdad20552011-02-11 19:43:55 +01001673 /* The worker should not sleep waiting for state_mutex,
Philipp Reisnere64a3292011-02-05 17:34:11 +01001674 that can take long */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001675 if (!mutex_trylock(device->state_mutex)) {
1676 set_bit(B_RS_H_DONE, &device->flags);
1677 device->start_resync_timer.expires = jiffies + HZ/5;
1678 add_timer(&device->start_resync_timer);
Philipp Reisnere64a3292011-02-05 17:34:11 +01001679 return;
1680 }
1681 } else {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001682 mutex_lock(device->state_mutex);
Philipp Reisnere64a3292011-02-05 17:34:11 +01001683 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001684
Andreas Gruenbacher28bc3b82014-08-14 18:33:30 +02001685 lock_all_resources();
1686 clear_bit(B_RS_H_DONE, &device->flags);
Philipp Reisnera7004712013-03-27 14:08:35 +01001687 /* Did some connection breakage or IO error race with us? */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001688 if (device->state.conn < C_CONNECTED
1689 || !get_ldev_if_state(device, D_NEGOTIATING)) {
Andreas Gruenbacher28bc3b82014-08-14 18:33:30 +02001690 unlock_all_resources();
1691 goto out;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001692 }
1693
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001694 ns = drbd_read_state(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001695
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001696 ns.aftr_isp = !_drbd_may_sync_now(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001697
1698 ns.conn = side;
1699
1700 if (side == C_SYNC_TARGET)
1701 ns.disk = D_INCONSISTENT;
1702 else /* side == C_SYNC_SOURCE */
1703 ns.pdsk = D_INCONSISTENT;
1704
Andreas Gruenbacher28bc3b82014-08-14 18:33:30 +02001705 r = _drbd_set_state(device, ns, CS_VERBOSE, NULL);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001706 ns = drbd_read_state(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001707
1708 if (ns.conn < C_CONNECTED)
1709 r = SS_UNKNOWN_ERROR;
1710
1711 if (r == SS_SUCCESS) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001712 unsigned long tw = drbd_bm_total_weight(device);
Lars Ellenberg1d7734a2010-08-11 21:21:50 +02001713 unsigned long now = jiffies;
1714 int i;
1715
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001716 device->rs_failed = 0;
1717 device->rs_paused = 0;
1718 device->rs_same_csum = 0;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001719 device->rs_last_sect_ev = 0;
1720 device->rs_total = tw;
1721 device->rs_start = now;
Lars Ellenberg1d7734a2010-08-11 21:21:50 +02001722 for (i = 0; i < DRBD_SYNC_MARKS; i++) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001723 device->rs_mark_left[i] = tw;
1724 device->rs_mark_time[i] = now;
Lars Ellenberg1d7734a2010-08-11 21:21:50 +02001725 }
Andreas Gruenbacher28bc3b82014-08-14 18:33:30 +02001726 drbd_pause_after(device);
Lars Ellenberg5ab7d2c2014-01-27 15:58:22 +01001727 /* Forget potentially stale cached per resync extent bit-counts.
1728 * Open coded drbd_rs_cancel_all(device), we already have IRQs
1729 * disabled, and know the disk state is ok. */
1730 spin_lock(&device->al_lock);
1731 lc_reset(device->resync);
1732 device->resync_locked = 0;
1733 device->resync_wenr = LC_FREE;
1734 spin_unlock(&device->al_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001735 }
Andreas Gruenbacher28bc3b82014-08-14 18:33:30 +02001736 unlock_all_resources();
Lars Ellenberg5a22db82010-12-17 21:14:23 +01001737
Philipp Reisnerb411b362009-09-25 16:07:19 -07001738 if (r == SS_SUCCESS) {
Lars Ellenberg5ab7d2c2014-01-27 15:58:22 +01001739 wake_up(&device->al_wait); /* for lc_reset() above */
Philipp Reisner328e0f12012-10-19 14:37:47 +02001740 /* reset rs_last_bcast when a resync or verify is started,
1741 * to deal with potential jiffies wrap. */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001742 device->rs_last_bcast = jiffies - HZ;
Philipp Reisner328e0f12012-10-19 14:37:47 +02001743
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02001744 drbd_info(device, "Began resync as %s (will sync %lu KB [%lu bits set]).\n",
Philipp Reisnerb411b362009-09-25 16:07:19 -07001745 drbd_conn_str(ns.conn),
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001746 (unsigned long) device->rs_total << (BM_BLOCK_SHIFT-10),
1747 (unsigned long) device->rs_total);
Lars Ellenbergaaaba342014-03-18 12:30:09 +01001748 if (side == C_SYNC_TARGET) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001749 device->bm_resync_fo = 0;
Lars Ellenbergaaaba342014-03-18 12:30:09 +01001750 device->use_csums = use_checksum_based_resync(connection, device);
1751 } else {
1752 device->use_csums = 0;
1753 }
Lars Ellenberg6c922ed2011-01-12 11:51:13 +01001754
1755 /* Since protocol 96, we must serialize drbd_gen_and_send_sync_uuid
1756 * with w_send_oos, or the sync target will get confused as to
1757 * how much bits to resync. We cannot do that always, because for an
1758 * empty resync and protocol < 95, we need to do it here, as we call
1759 * drbd_resync_finished from here in that case.
1760 * We drbd_gen_and_send_sync_uuid here for protocol < 96,
1761 * and from after_state_ch otherwise. */
Lars Ellenberg44a4d552013-11-22 12:40:58 +01001762 if (side == C_SYNC_SOURCE && connection->agreed_pro_version < 96)
1763 drbd_gen_and_send_sync_uuid(peer_device);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001764
Lars Ellenberg44a4d552013-11-22 12:40:58 +01001765 if (connection->agreed_pro_version < 95 && device->rs_total == 0) {
Lars Ellenbergaf85e8e2010-10-07 16:07:55 +02001766 /* This still has a race (about when exactly the peers
1767 * detect connection loss) that can lead to a full sync
1768 * on next handshake. In 8.3.9 we fixed this with explicit
1769 * resync-finished notifications, but the fix
1770 * introduces a protocol change. Sleeping for some
1771 * time longer than the ping interval + timeout on the
1772 * SyncSource, to give the SyncTarget the chance to
1773 * detect connection loss, then waiting for a ping
1774 * response (implicit in drbd_resync_finished) reduces
1775 * the race considerably, but does not solve it. */
Philipp Reisner44ed1672011-04-19 17:10:19 +02001776 if (side == C_SYNC_SOURCE) {
1777 struct net_conf *nc;
1778 int timeo;
1779
1780 rcu_read_lock();
Lars Ellenberg44a4d552013-11-22 12:40:58 +01001781 nc = rcu_dereference(connection->net_conf);
Philipp Reisner44ed1672011-04-19 17:10:19 +02001782 timeo = nc->ping_int * HZ + nc->ping_timeo * HZ / 9;
1783 rcu_read_unlock();
1784 schedule_timeout_interruptible(timeo);
1785 }
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001786 drbd_resync_finished(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001787 }
1788
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001789 drbd_rs_controller_reset(device);
1790 /* ns.conn may already be != device->state.conn,
Philipp Reisnerb411b362009-09-25 16:07:19 -07001791 * we may have been paused in between, or become paused until
1792 * the timer triggers.
1793 * No matter, that is handled in resync_timer_fn() */
1794 if (ns.conn == C_SYNC_TARGET)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001795 mod_timer(&device->resync_timer, jiffies);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001796
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001797 drbd_md_sync(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001798 }
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001799 put_ldev(device);
Andreas Gruenbacher28bc3b82014-08-14 18:33:30 +02001800out:
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001801 mutex_unlock(device->state_mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001802}
1803
Lars Ellenberge334f552014-02-11 09:30:49 +01001804static void update_on_disk_bitmap(struct drbd_device *device, bool resync_done)
Lars Ellenbergc7a58db2013-12-20 11:39:48 +01001805{
1806 struct sib_info sib = { .sib_reason = SIB_SYNC_PROGRESS, };
1807 device->rs_last_bcast = jiffies;
1808
1809 if (!get_ldev(device))
1810 return;
1811
1812 drbd_bm_write_lazy(device, 0);
Lars Ellenberg5ab7d2c2014-01-27 15:58:22 +01001813 if (resync_done && is_sync_state(device->state.conn))
Lars Ellenbergc7a58db2013-12-20 11:39:48 +01001814 drbd_resync_finished(device);
Lars Ellenberg5ab7d2c2014-01-27 15:58:22 +01001815
Lars Ellenbergc7a58db2013-12-20 11:39:48 +01001816 drbd_bcast_event(device, &sib);
1817 /* update timestamp, in case it took a while to write out stuff */
1818 device->rs_last_bcast = jiffies;
1819 put_ldev(device);
1820}
1821
Lars Ellenberge334f552014-02-11 09:30:49 +01001822static void drbd_ldev_destroy(struct drbd_device *device)
1823{
1824 lc_destroy(device->resync);
1825 device->resync = NULL;
1826 lc_destroy(device->act_log);
1827 device->act_log = NULL;
Andreas Gruenbacherd1b80852014-09-11 14:29:09 +02001828
1829 __acquire(local);
1830 drbd_free_ldev(device->ldev);
1831 device->ldev = NULL;
1832 __release(local);
1833
Lars Ellenberge334f552014-02-11 09:30:49 +01001834 clear_bit(GOING_DISKLESS, &device->flags);
1835 wake_up(&device->misc_wait);
1836}
1837
1838static void go_diskless(struct drbd_device *device)
1839{
1840 D_ASSERT(device, device->state.disk == D_FAILED);
1841 /* we cannot assert local_cnt == 0 here, as get_ldev_if_state will
1842 * inc/dec it frequently. Once we are D_DISKLESS, no one will touch
1843 * the protected members anymore, though, so once put_ldev reaches zero
1844 * again, it will be safe to free them. */
1845
1846 /* Try to write changed bitmap pages, read errors may have just
1847 * set some bits outside the area covered by the activity log.
1848 *
1849 * If we have an IO error during the bitmap writeout,
1850 * we will want a full sync next time, just in case.
1851 * (Do we want a specific meta data flag for this?)
1852 *
1853 * If that does not make it to stable storage either,
1854 * we cannot do anything about that anymore.
1855 *
1856 * We still need to check if both bitmap and ldev are present, we may
1857 * end up here after a failed attach, before ldev was even assigned.
1858 */
1859 if (device->bitmap && device->ldev) {
1860 /* An interrupted resync or similar is allowed to recounts bits
1861 * while we detach.
1862 * Any modifications would not be expected anymore, though.
1863 */
1864 if (drbd_bitmap_io_from_worker(device, drbd_bm_write,
1865 "detach", BM_LOCKED_TEST_ALLOWED)) {
1866 if (test_bit(WAS_READ_ERROR, &device->flags)) {
1867 drbd_md_set_flag(device, MDF_FULL_SYNC);
1868 drbd_md_sync(device);
1869 }
1870 }
1871 }
1872
1873 drbd_force_state(device, NS(disk, D_DISKLESS));
1874}
1875
Lars Ellenbergac0acb92014-02-11 09:47:58 +01001876static int do_md_sync(struct drbd_device *device)
1877{
1878 drbd_warn(device, "md_sync_timer expired! Worker calls drbd_md_sync().\n");
1879 drbd_md_sync(device);
1880 return 0;
1881}
1882
Lars Ellenberg944410e2014-05-06 15:02:05 +02001883/* only called from drbd_worker thread, no locking */
1884void __update_timing_details(
1885 struct drbd_thread_timing_details *tdp,
1886 unsigned int *cb_nr,
1887 void *cb,
1888 const char *fn, const unsigned int line)
1889{
1890 unsigned int i = *cb_nr % DRBD_THREAD_DETAILS_HIST;
1891 struct drbd_thread_timing_details *td = tdp + i;
1892
1893 td->start_jif = jiffies;
1894 td->cb_addr = cb;
1895 td->caller_fn = fn;
1896 td->line = line;
1897 td->cb_nr = *cb_nr;
1898
1899 i = (i+1) % DRBD_THREAD_DETAILS_HIST;
1900 td = tdp + i;
1901 memset(td, 0, sizeof(*td));
1902
1903 ++(*cb_nr);
1904}
1905
Lars Ellenberge334f552014-02-11 09:30:49 +01001906static void do_device_work(struct drbd_device *device, const unsigned long todo)
1907{
Andreas Gruenbacherb47a06d2014-09-11 14:29:10 +02001908 if (test_bit(MD_SYNC, &todo))
Lars Ellenbergac0acb92014-02-11 09:47:58 +01001909 do_md_sync(device);
Andreas Gruenbacherb47a06d2014-09-11 14:29:10 +02001910 if (test_bit(RS_DONE, &todo) ||
1911 test_bit(RS_PROGRESS, &todo))
1912 update_on_disk_bitmap(device, test_bit(RS_DONE, &todo));
1913 if (test_bit(GO_DISKLESS, &todo))
Lars Ellenberge334f552014-02-11 09:30:49 +01001914 go_diskless(device);
Andreas Gruenbacherb47a06d2014-09-11 14:29:10 +02001915 if (test_bit(DESTROY_DISK, &todo))
Lars Ellenberge334f552014-02-11 09:30:49 +01001916 drbd_ldev_destroy(device);
Andreas Gruenbacherb47a06d2014-09-11 14:29:10 +02001917 if (test_bit(RS_START, &todo))
Lars Ellenbergac0acb92014-02-11 09:47:58 +01001918 do_start_resync(device);
Lars Ellenberge334f552014-02-11 09:30:49 +01001919}
1920
1921#define DRBD_DEVICE_WORK_MASK \
1922 ((1UL << GO_DISKLESS) \
1923 |(1UL << DESTROY_DISK) \
Lars Ellenbergac0acb92014-02-11 09:47:58 +01001924 |(1UL << MD_SYNC) \
1925 |(1UL << RS_START) \
Lars Ellenberge334f552014-02-11 09:30:49 +01001926 |(1UL << RS_PROGRESS) \
1927 |(1UL << RS_DONE) \
1928 )
1929
1930static unsigned long get_work_bits(unsigned long *flags)
1931{
1932 unsigned long old, new;
1933 do {
1934 old = *flags;
1935 new = old & ~DRBD_DEVICE_WORK_MASK;
1936 } while (cmpxchg(flags, old, new) != old);
1937 return old & DRBD_DEVICE_WORK_MASK;
1938}
1939
1940static void do_unqueued_work(struct drbd_connection *connection)
Lars Ellenbergc7a58db2013-12-20 11:39:48 +01001941{
1942 struct drbd_peer_device *peer_device;
1943 int vnr;
1944
1945 rcu_read_lock();
1946 idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
1947 struct drbd_device *device = peer_device->device;
Lars Ellenberge334f552014-02-11 09:30:49 +01001948 unsigned long todo = get_work_bits(&device->flags);
1949 if (!todo)
Lars Ellenbergc7a58db2013-12-20 11:39:48 +01001950 continue;
Lars Ellenberg5ab7d2c2014-01-27 15:58:22 +01001951
Lars Ellenbergc7a58db2013-12-20 11:39:48 +01001952 kref_get(&device->kref);
1953 rcu_read_unlock();
Lars Ellenberge334f552014-02-11 09:30:49 +01001954 do_device_work(device, todo);
Lars Ellenbergc7a58db2013-12-20 11:39:48 +01001955 kref_put(&device->kref, drbd_destroy_device);
1956 rcu_read_lock();
1957 }
1958 rcu_read_unlock();
1959}
1960
Rashika Kheriaa186e472013-12-19 15:06:10 +05301961static bool dequeue_work_batch(struct drbd_work_queue *queue, struct list_head *work_list)
Lars Ellenberg8c0785a2011-10-19 11:50:57 +02001962{
1963 spin_lock_irq(&queue->q_lock);
Lars Ellenberg15e26f62014-04-28 11:43:21 +02001964 list_splice_tail_init(&queue->q, work_list);
Lars Ellenberg8c0785a2011-10-19 11:50:57 +02001965 spin_unlock_irq(&queue->q_lock);
1966 return !list_empty(work_list);
1967}
1968
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02001969static void wait_for_work(struct drbd_connection *connection, struct list_head *work_list)
Lars Ellenbergb6dd1a82011-11-28 15:04:49 +01001970{
1971 DEFINE_WAIT(wait);
1972 struct net_conf *nc;
1973 int uncork, cork;
1974
Lars Ellenbergabde9cc2014-09-11 14:29:11 +02001975 dequeue_work_batch(&connection->sender_work, work_list);
Lars Ellenbergb6dd1a82011-11-28 15:04:49 +01001976 if (!list_empty(work_list))
1977 return;
1978
1979 /* Still nothing to do?
1980 * Maybe we still need to close the current epoch,
1981 * even if no new requests are queued yet.
1982 *
1983 * Also, poke TCP, just in case.
1984 * Then wait for new work (or signal). */
1985 rcu_read_lock();
1986 nc = rcu_dereference(connection->net_conf);
1987 uncork = nc ? nc->tcp_cork : 0;
1988 rcu_read_unlock();
1989 if (uncork) {
1990 mutex_lock(&connection->data.mutex);
1991 if (connection->data.socket)
1992 drbd_tcp_uncork(connection->data.socket);
1993 mutex_unlock(&connection->data.mutex);
1994 }
1995
1996 for (;;) {
1997 int send_barrier;
1998 prepare_to_wait(&connection->sender_work.q_wait, &wait, TASK_INTERRUPTIBLE);
Andreas Gruenbacher05008132011-07-07 14:19:42 +02001999 spin_lock_irq(&connection->resource->req_lock);
Lars Ellenbergb6dd1a82011-11-28 15:04:49 +01002000 spin_lock(&connection->sender_work.q_lock); /* FIXME get rid of this one? */
Lars Ellenbergbc317a92012-08-22 11:47:14 +02002001 if (!list_empty(&connection->sender_work.q))
Lars Ellenberg4dd726f2014-02-11 11:15:36 +01002002 list_splice_tail_init(&connection->sender_work.q, work_list);
Lars Ellenbergb6dd1a82011-11-28 15:04:49 +01002003 spin_unlock(&connection->sender_work.q_lock); /* FIXME get rid of this one? */
2004 if (!list_empty(work_list) || signal_pending(current)) {
Andreas Gruenbacher05008132011-07-07 14:19:42 +02002005 spin_unlock_irq(&connection->resource->req_lock);
Lars Ellenbergb6dd1a82011-11-28 15:04:49 +01002006 break;
2007 }
Lars Ellenbergf9c78122014-04-28 18:43:29 +02002008
2009 /* We found nothing new to do, no to-be-communicated request,
2010 * no other work item. We may still need to close the last
2011 * epoch. Next incoming request epoch will be connection ->
2012 * current transfer log epoch number. If that is different
2013 * from the epoch of the last request we communicated, it is
2014 * safe to send the epoch separating barrier now.
2015 */
2016 send_barrier =
2017 atomic_read(&connection->current_tle_nr) !=
2018 connection->send.current_epoch_nr;
Andreas Gruenbacher05008132011-07-07 14:19:42 +02002019 spin_unlock_irq(&connection->resource->req_lock);
Lars Ellenbergf9c78122014-04-28 18:43:29 +02002020
2021 if (send_barrier)
2022 maybe_send_barrier(connection,
2023 connection->send.current_epoch_nr + 1);
Lars Ellenberg5ab7d2c2014-01-27 15:58:22 +01002024
Lars Ellenberge334f552014-02-11 09:30:49 +01002025 if (test_bit(DEVICE_WORK_PENDING, &connection->flags))
Lars Ellenberg5ab7d2c2014-01-27 15:58:22 +01002026 break;
2027
Lars Ellenberga80ca1a2013-12-27 17:17:25 +01002028 /* drbd_send() may have called flush_signals() */
2029 if (get_t_state(&connection->worker) != RUNNING)
2030 break;
Lars Ellenberg5ab7d2c2014-01-27 15:58:22 +01002031
Lars Ellenbergb6dd1a82011-11-28 15:04:49 +01002032 schedule();
2033 /* may be woken up for other things but new work, too,
2034 * e.g. if the current epoch got closed.
2035 * In which case we send the barrier above. */
2036 }
2037 finish_wait(&connection->sender_work.q_wait, &wait);
2038
2039 /* someone may have changed the config while we have been waiting above. */
2040 rcu_read_lock();
2041 nc = rcu_dereference(connection->net_conf);
2042 cork = nc ? nc->tcp_cork : 0;
2043 rcu_read_unlock();
2044 mutex_lock(&connection->data.mutex);
2045 if (connection->data.socket) {
2046 if (cork)
2047 drbd_tcp_cork(connection->data.socket);
2048 else if (!uncork)
2049 drbd_tcp_uncork(connection->data.socket);
2050 }
2051 mutex_unlock(&connection->data.mutex);
2052}
2053
Philipp Reisnerb411b362009-09-25 16:07:19 -07002054int drbd_worker(struct drbd_thread *thi)
2055{
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002056 struct drbd_connection *connection = thi->connection;
Andreas Gruenbacher6db7e502011-08-26 23:50:08 +02002057 struct drbd_work *w = NULL;
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +02002058 struct drbd_peer_device *peer_device;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002059 LIST_HEAD(work_list);
Lars Ellenberg8c0785a2011-10-19 11:50:57 +02002060 int vnr;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002061
Andreas Gruenbachere77a0a52011-01-25 15:43:39 +01002062 while (get_t_state(thi) == RUNNING) {
Philipp Reisner80822282011-02-08 12:46:30 +01002063 drbd_thread_current_set_cpu(thi);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002064
Lars Ellenberg944410e2014-05-06 15:02:05 +02002065 if (list_empty(&work_list)) {
2066 update_worker_timing_details(connection, wait_for_work);
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002067 wait_for_work(connection, &work_list);
Lars Ellenberg944410e2014-05-06 15:02:05 +02002068 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07002069
Lars Ellenberg944410e2014-05-06 15:02:05 +02002070 if (test_and_clear_bit(DEVICE_WORK_PENDING, &connection->flags)) {
2071 update_worker_timing_details(connection, do_unqueued_work);
Lars Ellenberge334f552014-02-11 09:30:49 +01002072 do_unqueued_work(connection);
Lars Ellenberg944410e2014-05-06 15:02:05 +02002073 }
Lars Ellenberg5ab7d2c2014-01-27 15:58:22 +01002074
Lars Ellenberg8c0785a2011-10-19 11:50:57 +02002075 if (signal_pending(current)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07002076 flush_signals(current);
Philipp Reisner19393e12011-02-09 10:09:07 +01002077 if (get_t_state(thi) == RUNNING) {
Andreas Gruenbacher1ec861e2011-07-06 11:01:44 +02002078 drbd_warn(connection, "Worker got an unexpected signal\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07002079 continue;
Philipp Reisner19393e12011-02-09 10:09:07 +01002080 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07002081 break;
2082 }
2083
Andreas Gruenbachere77a0a52011-01-25 15:43:39 +01002084 if (get_t_state(thi) != RUNNING)
Philipp Reisnerb411b362009-09-25 16:07:19 -07002085 break;
Philipp Reisnerb411b362009-09-25 16:07:19 -07002086
Lars Ellenberg729e8b82014-09-11 14:29:12 +02002087 if (!list_empty(&work_list)) {
Andreas Gruenbacher6db7e502011-08-26 23:50:08 +02002088 w = list_first_entry(&work_list, struct drbd_work, list);
2089 list_del_init(&w->list);
Lars Ellenberg944410e2014-05-06 15:02:05 +02002090 update_worker_timing_details(connection, w->cb);
Andreas Gruenbacher6db7e502011-08-26 23:50:08 +02002091 if (w->cb(w, connection->cstate < C_WF_REPORT_PARAMS) == 0)
Lars Ellenberg8c0785a2011-10-19 11:50:57 +02002092 continue;
Andreas Gruenbacherbde89a92011-05-30 16:32:41 +02002093 if (connection->cstate >= C_WF_REPORT_PARAMS)
2094 conn_request_state(connection, NS(conn, C_NETWORK_FAILURE), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -07002095 }
2096 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07002097
Lars Ellenberg8c0785a2011-10-19 11:50:57 +02002098 do {
Lars Ellenberg944410e2014-05-06 15:02:05 +02002099 if (test_and_clear_bit(DEVICE_WORK_PENDING, &connection->flags)) {
2100 update_worker_timing_details(connection, do_unqueued_work);
Lars Ellenberge334f552014-02-11 09:30:49 +01002101 do_unqueued_work(connection);
Lars Ellenberg944410e2014-05-06 15:02:05 +02002102 }
Lars Ellenberg729e8b82014-09-11 14:29:12 +02002103 if (!list_empty(&work_list)) {
Andreas Gruenbacher6db7e502011-08-26 23:50:08 +02002104 w = list_first_entry(&work_list, struct drbd_work, list);
2105 list_del_init(&w->list);
Lars Ellenberg944410e2014-05-06 15:02:05 +02002106 update_worker_timing_details(connection, w->cb);
Andreas Gruenbacher6db7e502011-08-26 23:50:08 +02002107 w->cb(w, 1);
Lars Ellenberg729e8b82014-09-11 14:29:12 +02002108 } else
2109 dequeue_work_batch(&connection->sender_work, &work_list);
Lars Ellenberge334f552014-02-11 09:30:49 +01002110 } while (!list_empty(&work_list) || test_bit(DEVICE_WORK_PENDING, &connection->flags));
Philipp Reisnerb411b362009-09-25 16:07:19 -07002111
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02002112 rcu_read_lock();
Andreas Gruenbacherc06ece62011-06-21 17:23:59 +02002113 idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
2114 struct drbd_device *device = peer_device->device;
Andreas Gruenbacher0b0ba1e2011-06-27 16:23:33 +02002115 D_ASSERT(device, device->state.disk == D_DISKLESS && device->state.conn == C_STANDALONE);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002116 kref_get(&device->kref);
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02002117 rcu_read_unlock();
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02002118 drbd_device_cleanup(device);
Andreas Gruenbacher05a10ec2011-06-07 22:54:17 +02002119 kref_put(&device->kref, drbd_destroy_device);
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02002120 rcu_read_lock();
Philipp Reisner0e29d162011-02-18 14:23:11 +01002121 }
Philipp Reisnerc141ebd2011-05-05 16:13:10 +02002122 rcu_read_unlock();
Philipp Reisnerb411b362009-09-25 16:07:19 -07002123
2124 return 0;
2125}