blob: 05a1780ffa850483cdf4d73a89b3a9a78964ed4c [file] [log] [blame]
Philipp Reisnerb411b362009-09-25 16:07:19 -07001/*
2 drbd_actlog.c
3
4 This file is part of DRBD by Philipp Reisner and Lars Ellenberg.
5
6 Copyright (C) 2003-2008, LINBIT Information Technologies GmbH.
7 Copyright (C) 2003-2008, Philipp Reisner <philipp.reisner@linbit.com>.
8 Copyright (C) 2003-2008, Lars Ellenberg <lars.ellenberg@linbit.com>.
9
10 drbd is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2, or (at your option)
13 any later version.
14
15 drbd is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with drbd; see the file COPYING. If not, write to
22 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23
24 */
25
26#include <linux/slab.h>
Lars Ellenberg7ad651b2011-02-21 13:21:03 +010027#include <linux/crc32c.h>
Philipp Reisnerb411b362009-09-25 16:07:19 -070028#include <linux/drbd.h>
Lars Ellenberg7ad651b2011-02-21 13:21:03 +010029#include <linux/drbd_limits.h>
30#include <linux/dynamic_debug.h>
Philipp Reisnerb411b362009-09-25 16:07:19 -070031#include "drbd_int.h"
Philipp Reisnerb411b362009-09-25 16:07:19 -070032
Lars Ellenberg85f103d2011-03-31 12:06:48 +020033
34enum al_transaction_types {
35 AL_TR_UPDATE = 0,
36 AL_TR_INITIALIZED = 0xffff
37};
Lars Ellenberg7ad651b2011-02-21 13:21:03 +010038/* all fields on disc in big endian */
39struct __packed al_transaction_on_disk {
40 /* don't we all like magic */
41 __be32 magic;
42
43 /* to identify the most recent transaction block
44 * in the on disk ring buffer */
45 __be32 tr_number;
46
47 /* checksum on the full 4k block, with this field set to 0. */
48 __be32 crc32c;
49
50 /* type of transaction, special transaction types like:
Lars Ellenberg85f103d2011-03-31 12:06:48 +020051 * purge-all, set-all-idle, set-all-active, ... to-be-defined
52 * see also enum al_transaction_types */
Lars Ellenberg7ad651b2011-02-21 13:21:03 +010053 __be16 transaction_type;
54
55 /* we currently allow only a few thousand extents,
56 * so 16bit will be enough for the slot number. */
57
58 /* how many updates in this transaction */
59 __be16 n_updates;
60
61 /* maximum slot number, "al-extents" in drbd.conf speak.
62 * Having this in each transaction should make reconfiguration
63 * of that parameter easier. */
64 __be16 context_size;
65
66 /* slot number the context starts with */
67 __be16 context_start_slot_nr;
68
69 /* Some reserved bytes. Expected usage is a 64bit counter of
70 * sectors-written since device creation, and other data generation tag
71 * supporting usage */
72 __be32 __reserved[4];
73
74 /* --- 36 byte used --- */
75
76 /* Reserve space for up to AL_UPDATES_PER_TRANSACTION changes
77 * in one transaction, then use the remaining byte in the 4k block for
78 * context information. "Flexible" number of updates per transaction
79 * does not help, as we have to account for the case when all update
80 * slots are used anyways, so it would only complicate code without
81 * additional benefit.
82 */
83 __be16 update_slot_nr[AL_UPDATES_PER_TRANSACTION];
84
85 /* but the extent number is 32bit, which at an extent size of 4 MiB
86 * allows to cover device sizes of up to 2**54 Byte (16 PiB) */
87 __be32 update_extent_nr[AL_UPDATES_PER_TRANSACTION];
88
89 /* --- 420 bytes used (36 + 64*6) --- */
90
91 /* 4096 - 420 = 3676 = 919 * 4 */
92 __be32 context[AL_CONTEXT_PER_TRANSACTION];
Philipp Reisnerb411b362009-09-25 16:07:19 -070093};
94
95struct update_odbm_work {
96 struct drbd_work w;
Andreas Gruenbacher84b8c062011-07-28 15:27:51 +020097 struct drbd_device *device;
Philipp Reisnerb411b362009-09-25 16:07:19 -070098 unsigned int enr;
99};
100
101struct update_al_work {
102 struct drbd_work w;
Andreas Gruenbacher84b8c062011-07-28 15:27:51 +0200103 struct drbd_device *device;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700104 struct completion event;
Lars Ellenberg7ad651b2011-02-21 13:21:03 +0100105 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700106};
107
Philipp Reisnerb411b362009-09-25 16:07:19 -0700108
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200109void *drbd_md_get_buffer(struct drbd_device *device)
Philipp Reisnercdfda632011-07-05 15:38:59 +0200110{
111 int r;
112
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200113 wait_event(device->misc_wait,
114 (r = atomic_cmpxchg(&device->md_io_in_use, 0, 1)) == 0 ||
115 device->state.disk <= D_FAILED);
Philipp Reisnercdfda632011-07-05 15:38:59 +0200116
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200117 return r ? NULL : page_address(device->md_io_page);
Philipp Reisnercdfda632011-07-05 15:38:59 +0200118}
119
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200120void drbd_md_put_buffer(struct drbd_device *device)
Philipp Reisnercdfda632011-07-05 15:38:59 +0200121{
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200122 if (atomic_dec_and_test(&device->md_io_in_use))
123 wake_up(&device->misc_wait);
Philipp Reisnercdfda632011-07-05 15:38:59 +0200124}
125
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200126void wait_until_done_or_force_detached(struct drbd_device *device, struct drbd_backing_dev *bdev,
Philipp Reisner32db80f2012-02-22 11:51:57 +0100127 unsigned int *done)
Philipp Reisnercdfda632011-07-05 15:38:59 +0200128{
Philipp Reisner32db80f2012-02-22 11:51:57 +0100129 long dt;
130
131 rcu_read_lock();
132 dt = rcu_dereference(bdev->disk_conf)->disk_timeout;
133 rcu_read_unlock();
134 dt = dt * HZ / 10;
135 if (dt == 0)
136 dt = MAX_SCHEDULE_TIMEOUT;
137
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200138 dt = wait_event_timeout(device->misc_wait,
139 *done || test_bit(FORCE_DETACH, &device->flags), dt);
Lars Ellenberge34b6772012-09-27 15:07:11 +0200140 if (dt == 0) {
Andreas Gruenbacherd0180172011-07-03 17:53:52 +0200141 drbd_err(device, "meta-data IO operation timed out\n");
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200142 drbd_chk_io_error(device, 1, DRBD_FORCE_DETACH);
Lars Ellenberge34b6772012-09-27 15:07:11 +0200143 }
Philipp Reisnercdfda632011-07-05 15:38:59 +0200144}
145
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200146static int _drbd_md_sync_page_io(struct drbd_device *device,
Philipp Reisnerb411b362009-09-25 16:07:19 -0700147 struct drbd_backing_dev *bdev,
148 struct page *page, sector_t sector,
149 int rw, int size)
150{
151 struct bio *bio;
Andreas Gruenbacherac29f402010-12-13 02:20:47 +0100152 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700153
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200154 device->md_io.done = 0;
155 device->md_io.error = -ENODEV;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700156
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200157 if ((rw & WRITE) && !test_bit(MD_NO_FUA, &device->flags))
Lars Ellenberg86e1e982011-06-28 13:22:48 +0200158 rw |= REQ_FUA | REQ_FLUSH;
Jens Axboe721a9602011-03-09 11:56:30 +0100159 rw |= REQ_SYNC;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700160
Lars Ellenbergda4a75d2011-02-23 17:02:01 +0100161 bio = bio_alloc_drbd(GFP_NOIO);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700162 bio->bi_bdev = bdev->md_bdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700163 bio->bi_iter.bi_sector = sector;
Andreas Gruenbacherac29f402010-12-13 02:20:47 +0100164 err = -EIO;
165 if (bio_add_page(bio, page, size, 0) != size)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700166 goto out;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200167 bio->bi_private = &device->md_io;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700168 bio->bi_end_io = drbd_md_io_complete;
169 bio->bi_rw = rw;
170
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200171 if (!(rw & WRITE) && device->state.disk == D_DISKLESS && device->ldev == NULL)
Lars Ellenbergc04ccaa2013-03-19 18:16:47 +0100172 /* special case, drbd_md_read() during drbd_adm_attach(): no get_ldev */
173 ;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200174 else if (!get_ldev_if_state(device, D_ATTACHING)) {
Lars Ellenbergc04ccaa2013-03-19 18:16:47 +0100175 /* Corresponding put_ldev in drbd_md_io_complete() */
Andreas Gruenbacherd0180172011-07-03 17:53:52 +0200176 drbd_err(device, "ASSERT FAILED: get_ldev_if_state() == 1 in _drbd_md_sync_page_io()\n");
Philipp Reisnercdfda632011-07-05 15:38:59 +0200177 err = -ENODEV;
178 goto out;
179 }
180
181 bio_get(bio); /* one bio_put() is in the completion handler */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200182 atomic_inc(&device->md_io_in_use); /* drbd_md_put_buffer() is in the completion handler */
183 if (drbd_insert_fault(device, (rw & WRITE) ? DRBD_FAULT_MD_WR : DRBD_FAULT_MD_RD))
Philipp Reisnerb411b362009-09-25 16:07:19 -0700184 bio_endio(bio, -EIO);
185 else
186 submit_bio(rw, bio);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200187 wait_until_done_or_force_detached(device, bdev, &device->md_io.done);
Andreas Gruenbacherac29f402010-12-13 02:20:47 +0100188 if (bio_flagged(bio, BIO_UPTODATE))
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200189 err = device->md_io.error;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700190
Philipp Reisnerb411b362009-09-25 16:07:19 -0700191 out:
192 bio_put(bio);
Andreas Gruenbacherac29f402010-12-13 02:20:47 +0100193 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700194}
195
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200196int drbd_md_sync_page_io(struct drbd_device *device, struct drbd_backing_dev *bdev,
Philipp Reisnerb411b362009-09-25 16:07:19 -0700197 sector_t sector, int rw)
198{
Andreas Gruenbacher3fbf4d22010-12-13 02:25:41 +0100199 int err;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200200 struct page *iop = device->md_io_page;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700201
Andreas Gruenbacher0b0ba1e2011-06-27 16:23:33 +0200202 D_ASSERT(device, atomic_read(&device->md_io_in_use) == 1);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700203
204 BUG_ON(!bdev->md_bdev);
205
Lars Ellenberge4d7d6f2014-04-28 18:43:28 +0200206 dynamic_drbd_dbg(device, "meta_data io: %s [%d]:%s(,%llus,%s) %pS\n",
Lars Ellenberg7ad651b2011-02-21 13:21:03 +0100207 current->comm, current->pid, __func__,
Lars Ellenbergc04ccaa2013-03-19 18:16:47 +0100208 (unsigned long long)sector, (rw & WRITE) ? "WRITE" : "READ",
209 (void*)_RET_IP_ );
Philipp Reisnerb411b362009-09-25 16:07:19 -0700210
211 if (sector < drbd_md_first_sector(bdev) ||
Lars Ellenberg7ad651b2011-02-21 13:21:03 +0100212 sector + 7 > drbd_md_last_sector(bdev))
Andreas Gruenbacherd0180172011-07-03 17:53:52 +0200213 drbd_alert(device, "%s [%d]:%s(,%llus,%s) out of range md access!\n",
Philipp Reisnerb411b362009-09-25 16:07:19 -0700214 current->comm, current->pid, __func__,
215 (unsigned long long)sector, (rw & WRITE) ? "WRITE" : "READ");
216
Lars Ellenbergae8bf312013-03-19 18:16:43 +0100217 /* we do all our meta data IO in aligned 4k blocks. */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200218 err = _drbd_md_sync_page_io(device, bdev, iop, sector, rw, 4096);
Andreas Gruenbacher3fbf4d22010-12-13 02:25:41 +0100219 if (err) {
Andreas Gruenbacherd0180172011-07-03 17:53:52 +0200220 drbd_err(device, "drbd_md_sync_page_io(,%llus,%s) failed with error %d\n",
Andreas Gruenbacher935be262011-08-19 13:47:31 +0200221 (unsigned long long)sector, (rw & WRITE) ? "WRITE" : "READ", err);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700222 }
Andreas Gruenbacher3fbf4d22010-12-13 02:25:41 +0100223 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700224}
225
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200226static struct bm_extent *find_active_resync_extent(struct drbd_device *device, unsigned int enr)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700227{
Philipp Reisnerb411b362009-09-25 16:07:19 -0700228 struct lc_element *tmp;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200229 tmp = lc_find(device->resync, enr/AL_EXT_PER_BM_SECT);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700230 if (unlikely(tmp != NULL)) {
231 struct bm_extent *bm_ext = lc_entry(tmp, struct bm_extent, lce);
Lars Ellenberg6c3c43552013-03-19 18:16:53 +0100232 if (test_bit(BME_NO_WRITES, &bm_ext->flags))
233 return bm_ext;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700234 }
Lars Ellenberg6c3c43552013-03-19 18:16:53 +0100235 return NULL;
236}
237
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200238static struct lc_element *_al_get(struct drbd_device *device, unsigned int enr, bool nonblock)
Lars Ellenberg6c3c43552013-03-19 18:16:53 +0100239{
240 struct lc_element *al_ext;
241 struct bm_extent *bm_ext;
242 int wake;
243
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200244 spin_lock_irq(&device->al_lock);
245 bm_ext = find_active_resync_extent(device, enr);
Lars Ellenberg6c3c43552013-03-19 18:16:53 +0100246 if (bm_ext) {
247 wake = !test_and_set_bit(BME_PRIORITY, &bm_ext->flags);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200248 spin_unlock_irq(&device->al_lock);
Lars Ellenberg6c3c43552013-03-19 18:16:53 +0100249 if (wake)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200250 wake_up(&device->al_wait);
Lars Ellenberg6c3c43552013-03-19 18:16:53 +0100251 return NULL;
252 }
253 if (nonblock)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200254 al_ext = lc_try_get(device->act_log, enr);
Lars Ellenberg6c3c43552013-03-19 18:16:53 +0100255 else
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200256 al_ext = lc_get(device->act_log, enr);
257 spin_unlock_irq(&device->al_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700258 return al_ext;
259}
260
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200261bool drbd_al_begin_io_fastpath(struct drbd_device *device, struct drbd_interval *i)
Lars Ellenbergb5bc8e02013-03-19 18:16:52 +0100262{
263 /* for bios crossing activity log extent boundaries,
264 * we may need to activate two extents in one go */
265 unsigned first = i->sector >> (AL_EXTENT_SHIFT-9);
266 unsigned last = i->size == 0 ? first : (i->sector + (i->size >> 9) - 1) >> (AL_EXTENT_SHIFT-9);
Lars Ellenbergb5bc8e02013-03-19 18:16:52 +0100267
Andreas Gruenbacher0b0ba1e2011-06-27 16:23:33 +0200268 D_ASSERT(device, (unsigned)(last - first) <= 1);
269 D_ASSERT(device, atomic_read(&device->local_cnt) > 0);
Lars Ellenbergb5bc8e02013-03-19 18:16:52 +0100270
271 /* FIXME figure out a fast path for bios crossing AL extent boundaries */
272 if (first != last)
273 return false;
274
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200275 return _al_get(device, first, true);
Lars Ellenbergb5bc8e02013-03-19 18:16:52 +0100276}
277
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200278bool drbd_al_begin_io_prepare(struct drbd_device *device, struct drbd_interval *i)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700279{
Lars Ellenberg77265472011-03-31 16:00:51 +0200280 /* for bios crossing activity log extent boundaries,
281 * we may need to activate two extents in one go */
Lars Ellenberge15766e2011-04-01 10:38:30 +0200282 unsigned first = i->sector >> (AL_EXTENT_SHIFT-9);
Lars Ellenberg81a35372012-07-30 09:00:54 +0200283 unsigned last = i->size == 0 ? first : (i->sector + (i->size >> 9) - 1) >> (AL_EXTENT_SHIFT-9);
Lars Ellenberge15766e2011-04-01 10:38:30 +0200284 unsigned enr;
Lars Ellenbergebfd5d82013-03-19 18:16:49 +0100285 bool need_transaction = false;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700286
Andreas Gruenbacher0b0ba1e2011-06-27 16:23:33 +0200287 D_ASSERT(device, first <= last);
288 D_ASSERT(device, atomic_read(&device->local_cnt) > 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700289
Lars Ellenbergebfd5d82013-03-19 18:16:49 +0100290 for (enr = first; enr <= last; enr++) {
291 struct lc_element *al_ext;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200292 wait_event(device->al_wait,
293 (al_ext = _al_get(device, enr, false)) != NULL);
Lars Ellenbergebfd5d82013-03-19 18:16:49 +0100294 if (al_ext->lc_number != enr)
295 need_transaction = true;
296 }
Lars Ellenbergb5bc8e02013-03-19 18:16:52 +0100297 return need_transaction;
298}
Lars Ellenbergebfd5d82013-03-19 18:16:49 +0100299
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200300static int al_write_transaction(struct drbd_device *device, bool delegate);
Lars Ellenbergb5bc8e02013-03-19 18:16:52 +0100301
302/* When called through generic_make_request(), we must delegate
303 * activity log I/O to the worker thread: a further request
304 * submitted via generic_make_request() within the same task
305 * would be queued on current->bio_list, and would only start
306 * after this function returns (see generic_make_request()).
307 *
308 * However, if we *are* the worker, we must not delegate to ourselves.
309 */
310
311/*
312 * @delegate: delegate activity log I/O to the worker thread
313 */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200314void drbd_al_begin_io_commit(struct drbd_device *device, bool delegate)
Lars Ellenbergb5bc8e02013-03-19 18:16:52 +0100315{
316 bool locked = false;
317
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +0200318 BUG_ON(delegate && current == first_peer_device(device)->connection->worker.task);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700319
Lars Ellenberg7dc1d672011-05-03 16:49:20 +0200320 /* Serialize multiple transactions.
321 * This uses test_and_set_bit, memory barrier is implicit.
322 */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200323 wait_event(device->al_wait,
324 device->act_log->pending_changes == 0 ||
325 (locked = lc_try_lock_for_transaction(device->act_log)));
Lars Ellenberg7dc1d672011-05-03 16:49:20 +0200326
327 if (locked) {
Lars Ellenberg7ad651b2011-02-21 13:21:03 +0100328 /* Double check: it may have been committed by someone else,
329 * while we have been waiting for the lock. */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200330 if (device->act_log->pending_changes) {
Philipp Reisner9a51ab12012-02-20 21:53:28 +0100331 bool write_al_updates;
332
333 rcu_read_lock();
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200334 write_al_updates = rcu_dereference(device->ldev->disk_conf)->al_updates;
Philipp Reisner9a51ab12012-02-20 21:53:28 +0100335 rcu_read_unlock();
336
Lars Ellenbergb5bc8e02013-03-19 18:16:52 +0100337 if (write_al_updates)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200338 al_write_transaction(device, delegate);
339 spin_lock_irq(&device->al_lock);
Lars Ellenberg7ad651b2011-02-21 13:21:03 +0100340 /* FIXME
Philipp Reisner1b7ab152011-07-15 17:19:02 +0200341 if (err)
Lars Ellenberg7ad651b2011-02-21 13:21:03 +0100342 we need an "lc_cancel" here;
343 */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200344 lc_committed(device->act_log);
345 spin_unlock_irq(&device->al_lock);
Lars Ellenberg7ad651b2011-02-21 13:21:03 +0100346 }
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200347 lc_unlock(device->act_log);
348 wake_up(&device->al_wait);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700349 }
350}
351
Lars Ellenbergb5bc8e02013-03-19 18:16:52 +0100352/*
353 * @delegate: delegate activity log I/O to the worker thread
354 */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200355void drbd_al_begin_io(struct drbd_device *device, struct drbd_interval *i, bool delegate)
Lars Ellenbergb5bc8e02013-03-19 18:16:52 +0100356{
Andreas Gruenbachera6b32bc2011-05-31 14:33:49 +0200357 BUG_ON(delegate && current == first_peer_device(device)->connection->worker.task);
Lars Ellenbergb5bc8e02013-03-19 18:16:52 +0100358
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200359 if (drbd_al_begin_io_prepare(device, i))
360 drbd_al_begin_io_commit(device, delegate);
Lars Ellenbergb5bc8e02013-03-19 18:16:52 +0100361}
362
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200363int drbd_al_begin_io_nonblock(struct drbd_device *device, struct drbd_interval *i)
Lars Ellenberg08a1dda2013-03-19 18:16:56 +0100364{
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200365 struct lru_cache *al = device->act_log;
Lars Ellenberg08a1dda2013-03-19 18:16:56 +0100366 /* for bios crossing activity log extent boundaries,
367 * we may need to activate two extents in one go */
368 unsigned first = i->sector >> (AL_EXTENT_SHIFT-9);
369 unsigned last = i->size == 0 ? first : (i->sector + (i->size >> 9) - 1) >> (AL_EXTENT_SHIFT-9);
370 unsigned nr_al_extents;
371 unsigned available_update_slots;
372 unsigned enr;
373
Andreas Gruenbacher0b0ba1e2011-06-27 16:23:33 +0200374 D_ASSERT(device, first <= last);
Lars Ellenberg08a1dda2013-03-19 18:16:56 +0100375
376 nr_al_extents = 1 + last - first; /* worst case: all touched extends are cold. */
377 available_update_slots = min(al->nr_elements - al->used,
378 al->max_pending_changes - al->pending_changes);
379
380 /* We want all necessary updates for a given request within the same transaction
381 * We could first check how many updates are *actually* needed,
382 * and use that instead of the worst-case nr_al_extents */
383 if (available_update_slots < nr_al_extents)
384 return -EWOULDBLOCK;
385
386 /* Is resync active in this area? */
387 for (enr = first; enr <= last; enr++) {
388 struct lc_element *tmp;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200389 tmp = lc_find(device->resync, enr/AL_EXT_PER_BM_SECT);
Lars Ellenberg08a1dda2013-03-19 18:16:56 +0100390 if (unlikely(tmp != NULL)) {
391 struct bm_extent *bm_ext = lc_entry(tmp, struct bm_extent, lce);
392 if (test_bit(BME_NO_WRITES, &bm_ext->flags)) {
Lars Ellenberg0b6ef412013-03-27 14:08:49 +0100393 if (!test_and_set_bit(BME_PRIORITY, &bm_ext->flags))
Lars Ellenberg08a1dda2013-03-19 18:16:56 +0100394 return -EBUSY;
395 return -EWOULDBLOCK;
396 }
397 }
398 }
399
400 /* Checkout the refcounts.
401 * Given that we checked for available elements and update slots above,
402 * this has to be successful. */
403 for (enr = first; enr <= last; enr++) {
404 struct lc_element *al_ext;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200405 al_ext = lc_get_cumulative(device->act_log, enr);
Lars Ellenberg08a1dda2013-03-19 18:16:56 +0100406 if (!al_ext)
Andreas Gruenbacherd0180172011-07-03 17:53:52 +0200407 drbd_info(device, "LOGIC BUG for enr=%u\n", enr);
Lars Ellenberg08a1dda2013-03-19 18:16:56 +0100408 }
409 return 0;
410}
411
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200412void drbd_al_complete_io(struct drbd_device *device, struct drbd_interval *i)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700413{
Lars Ellenberge15766e2011-04-01 10:38:30 +0200414 /* for bios crossing activity log extent boundaries,
415 * we may need to activate two extents in one go */
416 unsigned first = i->sector >> (AL_EXTENT_SHIFT-9);
Lars Ellenberg81a35372012-07-30 09:00:54 +0200417 unsigned last = i->size == 0 ? first : (i->sector + (i->size >> 9) - 1) >> (AL_EXTENT_SHIFT-9);
Lars Ellenberge15766e2011-04-01 10:38:30 +0200418 unsigned enr;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700419 struct lc_element *extent;
420 unsigned long flags;
421
Andreas Gruenbacher0b0ba1e2011-06-27 16:23:33 +0200422 D_ASSERT(device, first <= last);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200423 spin_lock_irqsave(&device->al_lock, flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700424
Lars Ellenberge15766e2011-04-01 10:38:30 +0200425 for (enr = first; enr <= last; enr++) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200426 extent = lc_find(device->act_log, enr);
Lars Ellenberge15766e2011-04-01 10:38:30 +0200427 if (!extent) {
Andreas Gruenbacherd0180172011-07-03 17:53:52 +0200428 drbd_err(device, "al_complete_io() called on inactive extent %u\n", enr);
Lars Ellenberge15766e2011-04-01 10:38:30 +0200429 continue;
430 }
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200431 lc_put(device->act_log, extent);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700432 }
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200433 spin_unlock_irqrestore(&device->al_lock, flags);
434 wake_up(&device->al_wait);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700435}
436
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100437#if (PAGE_SHIFT + 3) < (AL_EXTENT_SHIFT - BM_BLOCK_SHIFT)
438/* Currently BM_BLOCK_SHIFT, BM_EXT_SHIFT and AL_EXTENT_SHIFT
439 * are still coupled, or assume too much about their relation.
440 * Code below will not work if this is violated.
441 * Will be cleaned up with some followup patch.
442 */
443# error FIXME
444#endif
445
446static unsigned int al_extent_to_bm_page(unsigned int al_enr)
447{
448 return al_enr >>
449 /* bit to page */
450 ((PAGE_SHIFT + 3) -
451 /* al extent number to bit */
452 (AL_EXTENT_SHIFT - BM_BLOCK_SHIFT));
453}
454
455static unsigned int rs_extent_to_bm_page(unsigned int rs_enr)
456{
457 return rs_enr >>
458 /* bit to page */
459 ((PAGE_SHIFT + 3) -
Lars Ellenbergacb104c32011-04-28 07:58:24 +0200460 /* resync extent number to bit */
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100461 (BM_EXT_SHIFT - BM_BLOCK_SHIFT));
462}
463
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200464static sector_t al_tr_number_to_on_disk_sector(struct drbd_device *device)
Lars Ellenbergae8bf312013-03-19 18:16:43 +0100465{
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200466 const unsigned int stripes = device->ldev->md.al_stripes;
467 const unsigned int stripe_size_4kB = device->ldev->md.al_stripe_size_4k;
Lars Ellenbergae8bf312013-03-19 18:16:43 +0100468
469 /* transaction number, modulo on-disk ring buffer wrap around */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200470 unsigned int t = device->al_tr_number % (device->ldev->md.al_size_4k);
Lars Ellenbergae8bf312013-03-19 18:16:43 +0100471
472 /* ... to aligned 4k on disk block */
473 t = ((t % stripes) * stripe_size_4kB) + t/stripes;
474
475 /* ... to 512 byte sector in activity log */
476 t *= 8;
477
478 /* ... plus offset to the on disk position */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200479 return device->ldev->md.md_offset + device->ldev->md.al_offset + t;
Lars Ellenbergae8bf312013-03-19 18:16:43 +0100480}
481
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +0100482static int
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200483_al_write_transaction(struct drbd_device *device)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700484{
Lars Ellenberg7ad651b2011-02-21 13:21:03 +0100485 struct al_transaction_on_disk *buffer;
486 struct lc_element *e;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700487 sector_t sector;
Lars Ellenberg7ad651b2011-02-21 13:21:03 +0100488 int i, mx;
489 unsigned extent_nr;
490 unsigned crc = 0;
Philipp Reisner1b7ab152011-07-15 17:19:02 +0200491 int err = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700492
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200493 if (!get_ldev(device)) {
Andreas Gruenbacherd0180172011-07-03 17:53:52 +0200494 drbd_err(device, "disk is %s, cannot start al transaction\n",
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200495 drbd_disk_str(device->state.disk));
Philipp Reisner1b7ab152011-07-15 17:19:02 +0200496 return -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700497 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700498
Lars Ellenberg6719fb02010-10-18 23:04:07 +0200499 /* The bitmap write may have failed, causing a state change. */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200500 if (device->state.disk < D_INCONSISTENT) {
Andreas Gruenbacherd0180172011-07-03 17:53:52 +0200501 drbd_err(device,
Lars Ellenberg7ad651b2011-02-21 13:21:03 +0100502 "disk is %s, cannot write al transaction\n",
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200503 drbd_disk_str(device->state.disk));
504 put_ldev(device);
Philipp Reisner1b7ab152011-07-15 17:19:02 +0200505 return -EIO;
Lars Ellenberg6719fb02010-10-18 23:04:07 +0200506 }
507
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200508 buffer = drbd_md_get_buffer(device); /* protects md_io_buffer, al_tr_cycle, ... */
Philipp Reisnercdfda632011-07-05 15:38:59 +0200509 if (!buffer) {
Andreas Gruenbacherd0180172011-07-03 17:53:52 +0200510 drbd_err(device, "disk failed while waiting for md_io buffer\n");
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200511 put_ldev(device);
Philipp Reisner1b7ab152011-07-15 17:19:02 +0200512 return -ENODEV;
Philipp Reisnercdfda632011-07-05 15:38:59 +0200513 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700514
Lars Ellenberg7ad651b2011-02-21 13:21:03 +0100515 memset(buffer, 0, sizeof(*buffer));
516 buffer->magic = cpu_to_be32(DRBD_AL_MAGIC);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200517 buffer->tr_number = cpu_to_be32(device->al_tr_number);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700518
Lars Ellenberg7ad651b2011-02-21 13:21:03 +0100519 i = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700520
Lars Ellenberg7ad651b2011-02-21 13:21:03 +0100521 /* Even though no one can start to change this list
522 * once we set the LC_LOCKED -- from drbd_al_begin_io(),
523 * lc_try_lock_for_transaction() --, someone may still
524 * be in the process of changing it. */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200525 spin_lock_irq(&device->al_lock);
526 list_for_each_entry(e, &device->act_log->to_be_changed, list) {
Lars Ellenberg7ad651b2011-02-21 13:21:03 +0100527 if (i == AL_UPDATES_PER_TRANSACTION) {
528 i++;
529 break;
530 }
531 buffer->update_slot_nr[i] = cpu_to_be16(e->lc_index);
532 buffer->update_extent_nr[i] = cpu_to_be32(e->lc_new_number);
533 if (e->lc_number != LC_FREE)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200534 drbd_bm_mark_for_writeout(device,
Lars Ellenberg7ad651b2011-02-21 13:21:03 +0100535 al_extent_to_bm_page(e->lc_number));
536 i++;
537 }
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200538 spin_unlock_irq(&device->al_lock);
Lars Ellenberg7ad651b2011-02-21 13:21:03 +0100539 BUG_ON(i > AL_UPDATES_PER_TRANSACTION);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700540
Lars Ellenberg7ad651b2011-02-21 13:21:03 +0100541 buffer->n_updates = cpu_to_be16(i);
542 for ( ; i < AL_UPDATES_PER_TRANSACTION; i++) {
543 buffer->update_slot_nr[i] = cpu_to_be16(-1);
544 buffer->update_extent_nr[i] = cpu_to_be32(LC_FREE);
545 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700546
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200547 buffer->context_size = cpu_to_be16(device->act_log->nr_elements);
548 buffer->context_start_slot_nr = cpu_to_be16(device->al_tr_cycle);
Lars Ellenberg7ad651b2011-02-21 13:21:03 +0100549
550 mx = min_t(int, AL_CONTEXT_PER_TRANSACTION,
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200551 device->act_log->nr_elements - device->al_tr_cycle);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700552 for (i = 0; i < mx; i++) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200553 unsigned idx = device->al_tr_cycle + i;
554 extent_nr = lc_element_by_index(device->act_log, idx)->lc_number;
Lars Ellenberg7ad651b2011-02-21 13:21:03 +0100555 buffer->context[i] = cpu_to_be32(extent_nr);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700556 }
Lars Ellenberg7ad651b2011-02-21 13:21:03 +0100557 for (; i < AL_CONTEXT_PER_TRANSACTION; i++)
558 buffer->context[i] = cpu_to_be32(LC_FREE);
559
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200560 device->al_tr_cycle += AL_CONTEXT_PER_TRANSACTION;
561 if (device->al_tr_cycle >= device->act_log->nr_elements)
562 device->al_tr_cycle = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700563
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200564 sector = al_tr_number_to_on_disk_sector(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700565
Lars Ellenberg7ad651b2011-02-21 13:21:03 +0100566 crc = crc32c(0, buffer, 4096);
567 buffer->crc32c = cpu_to_be32(crc);
568
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200569 if (drbd_bm_write_hinted(device))
Philipp Reisner1b7ab152011-07-15 17:19:02 +0200570 err = -EIO;
Lars Ellenbergb5bc8e02013-03-19 18:16:52 +0100571 else {
572 bool write_al_updates;
573 rcu_read_lock();
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200574 write_al_updates = rcu_dereference(device->ldev->disk_conf)->al_updates;
Lars Ellenbergb5bc8e02013-03-19 18:16:52 +0100575 rcu_read_unlock();
576 if (write_al_updates) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200577 if (drbd_md_sync_page_io(device, device->ldev, sector, WRITE)) {
Lars Ellenbergb5bc8e02013-03-19 18:16:52 +0100578 err = -EIO;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200579 drbd_chk_io_error(device, 1, DRBD_META_IO_ERROR);
Lars Ellenbergb5bc8e02013-03-19 18:16:52 +0100580 } else {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200581 device->al_tr_number++;
582 device->al_writ_cnt++;
Lars Ellenbergb5bc8e02013-03-19 18:16:52 +0100583 }
584 }
Lars Ellenberg7ad651b2011-02-21 13:21:03 +0100585 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700586
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200587 drbd_md_put_buffer(device);
588 put_ldev(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700589
Philipp Reisner1b7ab152011-07-15 17:19:02 +0200590 return err;
591}
592
593
594static int w_al_write_transaction(struct drbd_work *w, int unused)
595{
596 struct update_al_work *aw = container_of(w, struct update_al_work, w);
Andreas Gruenbacher84b8c062011-07-28 15:27:51 +0200597 struct drbd_device *device = aw->device;
Philipp Reisner1b7ab152011-07-15 17:19:02 +0200598 int err;
599
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200600 err = _al_write_transaction(device);
Philipp Reisner1b7ab152011-07-15 17:19:02 +0200601 aw->err = err;
602 complete(&aw->event);
603
604 return err != -EIO ? err : 0;
605}
606
607/* Calls from worker context (see w_restart_disk_io()) need to write the
608 transaction directly. Others came through generic_make_request(),
609 those need to delegate it to the worker. */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200610static int al_write_transaction(struct drbd_device *device, bool delegate)
Philipp Reisner1b7ab152011-07-15 17:19:02 +0200611{
Lars Ellenberg56392d22013-03-19 18:16:48 +0100612 if (delegate) {
613 struct update_al_work al_work;
614 init_completion(&al_work.event);
615 al_work.w.cb = w_al_write_transaction;
Andreas Gruenbacher84b8c062011-07-28 15:27:51 +0200616 al_work.device = device;
617 drbd_queue_work_front(&first_peer_device(device)->connection->sender_work,
618 &al_work.w);
Lars Ellenberg56392d22013-03-19 18:16:48 +0100619 wait_for_completion(&al_work.event);
620 return al_work.err;
621 } else
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200622 return _al_write_transaction(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700623}
624
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200625static int _try_lc_del(struct drbd_device *device, struct lc_element *al_ext)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700626{
627 int rv;
628
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200629 spin_lock_irq(&device->al_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700630 rv = (al_ext->refcnt == 0);
631 if (likely(rv))
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200632 lc_del(device->act_log, al_ext);
633 spin_unlock_irq(&device->al_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700634
635 return rv;
636}
637
638/**
639 * drbd_al_shrink() - Removes all active extents form the activity log
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200640 * @device: DRBD device.
Philipp Reisnerb411b362009-09-25 16:07:19 -0700641 *
642 * Removes all active extents form the activity log, waiting until
643 * the reference count of each entry dropped to 0 first, of course.
644 *
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200645 * You need to lock device->act_log with lc_try_lock() / lc_unlock()
Philipp Reisnerb411b362009-09-25 16:07:19 -0700646 */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200647void drbd_al_shrink(struct drbd_device *device)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700648{
649 struct lc_element *al_ext;
650 int i;
651
Andreas Gruenbacher0b0ba1e2011-06-27 16:23:33 +0200652 D_ASSERT(device, test_bit(__LC_LOCKED, &device->act_log->flags));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700653
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200654 for (i = 0; i < device->act_log->nr_elements; i++) {
655 al_ext = lc_element_by_index(device->act_log, i);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700656 if (al_ext->lc_number == LC_FREE)
657 continue;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200658 wait_event(device->al_wait, _try_lc_del(device, al_ext));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700659 }
660
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200661 wake_up(&device->al_wait);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700662}
663
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200664int drbd_initialize_al(struct drbd_device *device, void *buffer)
Philipp Reisnerd752b262013-06-25 16:50:08 +0200665{
666 struct al_transaction_on_disk *al = buffer;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200667 struct drbd_md *md = &device->ldev->md;
Philipp Reisnerd752b262013-06-25 16:50:08 +0200668 sector_t al_base = md->md_offset + md->al_offset;
669 int al_size_4k = md->al_stripes * md->al_stripe_size_4k;
670 int i;
671
672 memset(al, 0, 4096);
673 al->magic = cpu_to_be32(DRBD_AL_MAGIC);
674 al->transaction_type = cpu_to_be16(AL_TR_INITIALIZED);
675 al->crc32c = cpu_to_be32(crc32c(0, al, 4096));
676
677 for (i = 0; i < al_size_4k; i++) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200678 int err = drbd_md_sync_page_io(device, device->ldev, al_base + i * 8, WRITE);
Philipp Reisnerd752b262013-06-25 16:50:08 +0200679 if (err)
680 return err;
681 }
682 return 0;
683}
684
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +0100685static int w_update_odbm(struct drbd_work *w, int unused)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700686{
687 struct update_odbm_work *udw = container_of(w, struct update_odbm_work, w);
Andreas Gruenbacher84b8c062011-07-28 15:27:51 +0200688 struct drbd_device *device = udw->device;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100689 struct sib_info sib = { .sib_reason = SIB_SYNC_PROGRESS, };
Philipp Reisnerb411b362009-09-25 16:07:19 -0700690
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200691 if (!get_ldev(device)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700692 if (__ratelimit(&drbd_ratelimit_state))
Andreas Gruenbacherd0180172011-07-03 17:53:52 +0200693 drbd_warn(device, "Can not update on disk bitmap, local IO disabled.\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700694 kfree(udw);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +0100695 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700696 }
697
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200698 drbd_bm_write_page(device, rs_extent_to_bm_page(udw->enr));
699 put_ldev(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700700
701 kfree(udw);
702
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200703 if (drbd_bm_total_weight(device) <= device->rs_failed) {
704 switch (device->state.conn) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700705 case C_SYNC_SOURCE: case C_SYNC_TARGET:
706 case C_PAUSED_SYNC_S: case C_PAUSED_SYNC_T:
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200707 drbd_resync_finished(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700708 default:
709 /* nothing to do */
710 break;
711 }
712 }
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200713 drbd_bcast_event(device, &sib);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700714
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +0100715 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700716}
717
718
719/* ATTENTION. The AL's extents are 4MB each, while the extents in the
720 * resync LRU-cache are 16MB each.
721 * The caller of this function has to hold an get_ldev() reference.
722 *
723 * TODO will be obsoleted once we have a caching lru of the on disk bitmap
724 */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200725static void drbd_try_clear_on_disk_bm(struct drbd_device *device, sector_t sector,
Philipp Reisnerb411b362009-09-25 16:07:19 -0700726 int count, int success)
727{
728 struct lc_element *e;
729 struct update_odbm_work *udw;
730
731 unsigned int enr;
732
Andreas Gruenbacher0b0ba1e2011-06-27 16:23:33 +0200733 D_ASSERT(device, atomic_read(&device->local_cnt));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700734
735 /* I simply assume that a sector/size pair never crosses
736 * a 16 MB extent border. (Currently this is true...) */
737 enr = BM_SECT_TO_EXT(sector);
738
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200739 e = lc_get(device->resync, enr);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700740 if (e) {
741 struct bm_extent *ext = lc_entry(e, struct bm_extent, lce);
742 if (ext->lce.lc_number == enr) {
743 if (success)
744 ext->rs_left -= count;
745 else
746 ext->rs_failed += count;
747 if (ext->rs_left < ext->rs_failed) {
Andreas Gruenbacherd0180172011-07-03 17:53:52 +0200748 drbd_warn(device, "BAD! sector=%llus enr=%u rs_left=%d "
Philipp Reisner975b2972011-11-17 10:11:47 +0100749 "rs_failed=%d count=%d cstate=%s\n",
Philipp Reisnerb411b362009-09-25 16:07:19 -0700750 (unsigned long long)sector,
751 ext->lce.lc_number, ext->rs_left,
Philipp Reisner975b2972011-11-17 10:11:47 +0100752 ext->rs_failed, count,
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200753 drbd_conn_str(device->state.conn));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700754
Philipp Reisner975b2972011-11-17 10:11:47 +0100755 /* We don't expect to be able to clear more bits
756 * than have been set when we originally counted
757 * the set bits to cache that value in ext->rs_left.
758 * Whatever the reason (disconnect during resync,
759 * delayed local completion of an application write),
760 * try to fix it up by recounting here. */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200761 ext->rs_left = drbd_bm_e_weight(device, enr);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700762 }
763 } else {
764 /* Normally this element should be in the cache,
765 * since drbd_rs_begin_io() pulled it already in.
766 *
767 * But maybe an application write finished, and we set
768 * something outside the resync lru_cache in sync.
769 */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200770 int rs_left = drbd_bm_e_weight(device, enr);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700771 if (ext->flags != 0) {
Andreas Gruenbacherd0180172011-07-03 17:53:52 +0200772 drbd_warn(device, "changing resync lce: %d[%u;%02lx]"
Philipp Reisnerb411b362009-09-25 16:07:19 -0700773 " -> %d[%u;00]\n",
774 ext->lce.lc_number, ext->rs_left,
775 ext->flags, enr, rs_left);
776 ext->flags = 0;
777 }
778 if (ext->rs_failed) {
Andreas Gruenbacherd0180172011-07-03 17:53:52 +0200779 drbd_warn(device, "Kicking resync_lru element enr=%u "
Philipp Reisnerb411b362009-09-25 16:07:19 -0700780 "out with rs_failed=%d\n",
781 ext->lce.lc_number, ext->rs_failed);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700782 }
783 ext->rs_left = rs_left;
784 ext->rs_failed = success ? 0 : count;
Lars Ellenberg46a15bc2011-02-21 13:21:01 +0100785 /* we don't keep a persistent log of the resync lru,
786 * we can commit any change right away. */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200787 lc_committed(device->resync);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700788 }
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200789 lc_put(device->resync, &ext->lce);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700790 /* no race, we are within the al_lock! */
791
792 if (ext->rs_left == ext->rs_failed) {
793 ext->rs_failed = 0;
794
795 udw = kmalloc(sizeof(*udw), GFP_ATOMIC);
796 if (udw) {
797 udw->enr = ext->lce.lc_number;
798 udw->w.cb = w_update_odbm;
Andreas Gruenbacher84b8c062011-07-28 15:27:51 +0200799 udw->device = device;
800 drbd_queue_work_front(&first_peer_device(device)->connection->sender_work,
801 &udw->w);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700802 } else {
Andreas Gruenbacherd0180172011-07-03 17:53:52 +0200803 drbd_warn(device, "Could not kmalloc an udw\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700804 }
805 }
806 } else {
Andreas Gruenbacherd0180172011-07-03 17:53:52 +0200807 drbd_err(device, "lc_get() failed! locked=%d/%d flags=%lu\n",
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200808 device->resync_locked,
809 device->resync->nr_elements,
810 device->resync->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700811 }
812}
813
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200814void drbd_advance_rs_marks(struct drbd_device *device, unsigned long still_to_go)
Lars Ellenbergc6ea14d2010-11-05 09:23:37 +0100815{
816 unsigned long now = jiffies;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200817 unsigned long last = device->rs_mark_time[device->rs_last_mark];
818 int next = (device->rs_last_mark + 1) % DRBD_SYNC_MARKS;
Lars Ellenbergc6ea14d2010-11-05 09:23:37 +0100819 if (time_after_eq(now, last + DRBD_SYNC_MARK_STEP)) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200820 if (device->rs_mark_left[device->rs_last_mark] != still_to_go &&
821 device->state.conn != C_PAUSED_SYNC_T &&
822 device->state.conn != C_PAUSED_SYNC_S) {
823 device->rs_mark_time[next] = now;
824 device->rs_mark_left[next] = still_to_go;
825 device->rs_last_mark = next;
Lars Ellenbergc6ea14d2010-11-05 09:23:37 +0100826 }
827 }
828}
829
Philipp Reisnerb411b362009-09-25 16:07:19 -0700830/* clear the bit corresponding to the piece of storage in question:
831 * size byte of data starting from sector. Only clear a bits of the affected
832 * one ore more _aligned_ BM_BLOCK_SIZE blocks.
833 *
834 * called by worker on C_SYNC_TARGET and receiver on SyncSource.
835 *
836 */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200837void __drbd_set_in_sync(struct drbd_device *device, sector_t sector, int size,
Philipp Reisnerb411b362009-09-25 16:07:19 -0700838 const char *file, const unsigned int line)
839{
840 /* Is called from worker and receiver context _only_ */
841 unsigned long sbnr, ebnr, lbnr;
842 unsigned long count = 0;
843 sector_t esector, nr_sectors;
844 int wake_up = 0;
845 unsigned long flags;
846
Lars Ellenberga0fb3c42014-04-28 18:43:23 +0200847 if (size <= 0 || !IS_ALIGNED(size, 512) || size > DRBD_MAX_DISCARD_SIZE) {
Andreas Gruenbacherd0180172011-07-03 17:53:52 +0200848 drbd_err(device, "drbd_set_in_sync: sector=%llus size=%d nonsense!\n",
Philipp Reisnerb411b362009-09-25 16:07:19 -0700849 (unsigned long long)sector, size);
850 return;
851 }
Philipp Reisner518a4d52012-10-19 14:21:22 +0200852
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200853 if (!get_ldev(device))
Philipp Reisner518a4d52012-10-19 14:21:22 +0200854 return; /* no disk, no metadata, no bitmap to clear bits in */
855
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200856 nr_sectors = drbd_get_capacity(device->this_bdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700857 esector = sector + (size >> 9) - 1;
858
Andreas Gruenbacher841ce242010-12-15 19:31:20 +0100859 if (!expect(sector < nr_sectors))
Philipp Reisner518a4d52012-10-19 14:21:22 +0200860 goto out;
Andreas Gruenbacher841ce242010-12-15 19:31:20 +0100861 if (!expect(esector < nr_sectors))
862 esector = nr_sectors - 1;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700863
864 lbnr = BM_SECT_TO_BIT(nr_sectors-1);
865
866 /* we clear it (in sync).
867 * round up start sector, round down end sector. we make sure we only
868 * clear full, aligned, BM_BLOCK_SIZE (4K) blocks */
869 if (unlikely(esector < BM_SECT_PER_BIT-1))
Philipp Reisner518a4d52012-10-19 14:21:22 +0200870 goto out;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700871 if (unlikely(esector == (nr_sectors-1)))
872 ebnr = lbnr;
873 else
874 ebnr = BM_SECT_TO_BIT(esector - (BM_SECT_PER_BIT-1));
875 sbnr = BM_SECT_TO_BIT(sector + BM_SECT_PER_BIT-1);
876
Philipp Reisnerb411b362009-09-25 16:07:19 -0700877 if (sbnr > ebnr)
Philipp Reisner518a4d52012-10-19 14:21:22 +0200878 goto out;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700879
880 /*
881 * ok, (capacity & 7) != 0 sometimes, but who cares...
882 * we count rs_{total,left} in bits, not sectors.
883 */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200884 count = drbd_bm_clear_bits(device, sbnr, ebnr);
Philipp Reisner518a4d52012-10-19 14:21:22 +0200885 if (count) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200886 drbd_advance_rs_marks(device, drbd_bm_total_weight(device));
887 spin_lock_irqsave(&device->al_lock, flags);
888 drbd_try_clear_on_disk_bm(device, sector, count, true);
889 spin_unlock_irqrestore(&device->al_lock, flags);
Lars Ellenberg1d7734a2010-08-11 21:21:50 +0200890
Philipp Reisnerb411b362009-09-25 16:07:19 -0700891 /* just wake_up unconditional now, various lc_chaged(),
892 * lc_put() in drbd_try_clear_on_disk_bm(). */
893 wake_up = 1;
894 }
Philipp Reisner518a4d52012-10-19 14:21:22 +0200895out:
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200896 put_ldev(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700897 if (wake_up)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200898 wake_up(&device->al_wait);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700899}
900
901/*
902 * this is intended to set one request worth of data out of sync.
903 * affects at least 1 bit,
Lars Ellenberg1816a2b2010-11-11 15:19:07 +0100904 * and at most 1+DRBD_MAX_BIO_SIZE/BM_BLOCK_SIZE bits.
Philipp Reisnerb411b362009-09-25 16:07:19 -0700905 *
906 * called by tl_clear and drbd_send_dblock (==drbd_make_request).
907 * so this can be _any_ process.
908 */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200909int __drbd_set_out_of_sync(struct drbd_device *device, sector_t sector, int size,
Philipp Reisnerb411b362009-09-25 16:07:19 -0700910 const char *file, const unsigned int line)
911{
Philipp Reisner376694a2011-11-07 10:54:28 +0100912 unsigned long sbnr, ebnr, flags;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700913 sector_t esector, nr_sectors;
Philipp Reisner73a01a12010-10-27 14:33:00 +0200914 unsigned int enr, count = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700915 struct lc_element *e;
916
Lars Ellenberg81a35372012-07-30 09:00:54 +0200917 /* this should be an empty REQ_FLUSH */
918 if (size == 0)
919 return 0;
920
Lars Ellenberga0fb3c42014-04-28 18:43:23 +0200921 if (size < 0 || !IS_ALIGNED(size, 512) || size > DRBD_MAX_DISCARD_SIZE) {
Andreas Gruenbacherd0180172011-07-03 17:53:52 +0200922 drbd_err(device, "sector: %llus, size: %d\n",
Philipp Reisnerb411b362009-09-25 16:07:19 -0700923 (unsigned long long)sector, size);
Philipp Reisner73a01a12010-10-27 14:33:00 +0200924 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700925 }
926
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200927 if (!get_ldev(device))
Philipp Reisner73a01a12010-10-27 14:33:00 +0200928 return 0; /* no disk, no metadata, no bitmap to set bits in */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700929
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200930 nr_sectors = drbd_get_capacity(device->this_bdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700931 esector = sector + (size >> 9) - 1;
932
Andreas Gruenbacher841ce242010-12-15 19:31:20 +0100933 if (!expect(sector < nr_sectors))
Philipp Reisnerb411b362009-09-25 16:07:19 -0700934 goto out;
Andreas Gruenbacher841ce242010-12-15 19:31:20 +0100935 if (!expect(esector < nr_sectors))
936 esector = nr_sectors - 1;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700937
Philipp Reisnerb411b362009-09-25 16:07:19 -0700938 /* we set it out of sync,
939 * we do not need to round anything here */
940 sbnr = BM_SECT_TO_BIT(sector);
941 ebnr = BM_SECT_TO_BIT(esector);
942
Philipp Reisnerb411b362009-09-25 16:07:19 -0700943 /* ok, (capacity & 7) != 0 sometimes, but who cares...
944 * we count rs_{total,left} in bits, not sectors. */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200945 spin_lock_irqsave(&device->al_lock, flags);
946 count = drbd_bm_set_bits(device, sbnr, ebnr);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700947
948 enr = BM_SECT_TO_EXT(sector);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200949 e = lc_find(device->resync, enr);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700950 if (e)
951 lc_entry(e, struct bm_extent, lce)->rs_left += count;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200952 spin_unlock_irqrestore(&device->al_lock, flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700953
954out:
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200955 put_ldev(device);
Philipp Reisner73a01a12010-10-27 14:33:00 +0200956
957 return count;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700958}
959
960static
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200961struct bm_extent *_bme_get(struct drbd_device *device, unsigned int enr)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700962{
963 struct lc_element *e;
964 struct bm_extent *bm_ext;
965 int wakeup = 0;
966 unsigned long rs_flags;
967
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200968 spin_lock_irq(&device->al_lock);
969 if (device->resync_locked > device->resync->nr_elements/2) {
970 spin_unlock_irq(&device->al_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700971 return NULL;
972 }
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200973 e = lc_get(device->resync, enr);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700974 bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL;
975 if (bm_ext) {
976 if (bm_ext->lce.lc_number != enr) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200977 bm_ext->rs_left = drbd_bm_e_weight(device, enr);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700978 bm_ext->rs_failed = 0;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200979 lc_committed(device->resync);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700980 wakeup = 1;
981 }
982 if (bm_ext->lce.refcnt == 1)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200983 device->resync_locked++;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700984 set_bit(BME_NO_WRITES, &bm_ext->flags);
985 }
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200986 rs_flags = device->resync->flags;
987 spin_unlock_irq(&device->al_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700988 if (wakeup)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +0200989 wake_up(&device->al_wait);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700990
991 if (!bm_ext) {
992 if (rs_flags & LC_STARVING)
Andreas Gruenbacherd0180172011-07-03 17:53:52 +0200993 drbd_warn(device, "Have to wait for element"
Philipp Reisnerb411b362009-09-25 16:07:19 -0700994 " (resync LRU too small?)\n");
Lars Ellenberg46a15bc2011-02-21 13:21:01 +0100995 BUG_ON(rs_flags & LC_LOCKED);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700996 }
997
998 return bm_ext;
999}
1000
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001001static int _is_in_al(struct drbd_device *device, unsigned int enr)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001002{
Lars Ellenberg46a15bc2011-02-21 13:21:01 +01001003 int rv;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001004
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001005 spin_lock_irq(&device->al_lock);
1006 rv = lc_is_used(device->act_log, enr);
1007 spin_unlock_irq(&device->al_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001008
Philipp Reisnerb411b362009-09-25 16:07:19 -07001009 return rv;
1010}
1011
1012/**
1013 * drbd_rs_begin_io() - Gets an extent in the resync LRU cache and sets it to BME_LOCKED
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001014 * @device: DRBD device.
Philipp Reisnerb411b362009-09-25 16:07:19 -07001015 * @sector: The sector number.
1016 *
Lars Ellenberg80a40e42010-08-11 23:28:00 +02001017 * This functions sleeps on al_wait. Returns 0 on success, -EINTR if interrupted.
Philipp Reisnerb411b362009-09-25 16:07:19 -07001018 */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001019int drbd_rs_begin_io(struct drbd_device *device, sector_t sector)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001020{
1021 unsigned int enr = BM_SECT_TO_EXT(sector);
1022 struct bm_extent *bm_ext;
1023 int i, sig;
Lars Ellenberge8299872014-04-28 18:43:19 +02001024 bool sa;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001025
Philipp Reisnerf91ab622010-11-09 13:59:41 +01001026retry:
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001027 sig = wait_event_interruptible(device->al_wait,
1028 (bm_ext = _bme_get(device, enr)));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001029 if (sig)
Lars Ellenberg80a40e42010-08-11 23:28:00 +02001030 return -EINTR;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001031
1032 if (test_bit(BME_LOCKED, &bm_ext->flags))
Lars Ellenberg80a40e42010-08-11 23:28:00 +02001033 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001034
Lars Ellenberge8299872014-04-28 18:43:19 +02001035 /* step aside only while we are above c-min-rate; unless disabled. */
1036 sa = drbd_rs_c_min_rate_throttle(device);
1037
Philipp Reisnerb411b362009-09-25 16:07:19 -07001038 for (i = 0; i < AL_EXT_PER_BM_SECT; i++) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001039 sig = wait_event_interruptible(device->al_wait,
1040 !_is_in_al(device, enr * AL_EXT_PER_BM_SECT + i) ||
Lars Ellenberge8299872014-04-28 18:43:19 +02001041 (sa && test_bit(BME_PRIORITY, &bm_ext->flags)));
Philipp Reisnerf91ab622010-11-09 13:59:41 +01001042
Lars Ellenberge8299872014-04-28 18:43:19 +02001043 if (sig || (sa && test_bit(BME_PRIORITY, &bm_ext->flags))) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001044 spin_lock_irq(&device->al_lock);
1045 if (lc_put(device->resync, &bm_ext->lce) == 0) {
Philipp Reisnerf91ab622010-11-09 13:59:41 +01001046 bm_ext->flags = 0; /* clears BME_NO_WRITES and eventually BME_PRIORITY */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001047 device->resync_locked--;
1048 wake_up(&device->al_wait);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001049 }
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001050 spin_unlock_irq(&device->al_lock);
Philipp Reisnerf91ab622010-11-09 13:59:41 +01001051 if (sig)
1052 return -EINTR;
1053 if (schedule_timeout_interruptible(HZ/10))
1054 return -EINTR;
Philipp Reisnerf91ab622010-11-09 13:59:41 +01001055 goto retry;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001056 }
1057 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001058 set_bit(BME_LOCKED, &bm_ext->flags);
Lars Ellenberg80a40e42010-08-11 23:28:00 +02001059 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001060}
1061
1062/**
1063 * drbd_try_rs_begin_io() - Gets an extent in the resync LRU cache, does not sleep
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001064 * @device: DRBD device.
Philipp Reisnerb411b362009-09-25 16:07:19 -07001065 * @sector: The sector number.
1066 *
1067 * Gets an extent in the resync LRU cache, sets it to BME_NO_WRITES, then
1068 * tries to set it to BME_LOCKED. Returns 0 upon success, and -EAGAIN
1069 * if there is still application IO going on in this area.
1070 */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001071int drbd_try_rs_begin_io(struct drbd_device *device, sector_t sector)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001072{
1073 unsigned int enr = BM_SECT_TO_EXT(sector);
1074 const unsigned int al_enr = enr*AL_EXT_PER_BM_SECT;
1075 struct lc_element *e;
1076 struct bm_extent *bm_ext;
1077 int i;
1078
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001079 spin_lock_irq(&device->al_lock);
1080 if (device->resync_wenr != LC_FREE && device->resync_wenr != enr) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001081 /* in case you have very heavy scattered io, it may
1082 * stall the syncer undefined if we give up the ref count
1083 * when we try again and requeue.
1084 *
1085 * if we don't give up the refcount, but the next time
1086 * we are scheduled this extent has been "synced" by new
1087 * application writes, we'd miss the lc_put on the
1088 * extent we keep the refcount on.
1089 * so we remembered which extent we had to try again, and
1090 * if the next requested one is something else, we do
1091 * the lc_put here...
1092 * we also have to wake_up
1093 */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001094 e = lc_find(device->resync, device->resync_wenr);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001095 bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL;
1096 if (bm_ext) {
Andreas Gruenbacher0b0ba1e2011-06-27 16:23:33 +02001097 D_ASSERT(device, !test_bit(BME_LOCKED, &bm_ext->flags));
1098 D_ASSERT(device, test_bit(BME_NO_WRITES, &bm_ext->flags));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001099 clear_bit(BME_NO_WRITES, &bm_ext->flags);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001100 device->resync_wenr = LC_FREE;
1101 if (lc_put(device->resync, &bm_ext->lce) == 0)
1102 device->resync_locked--;
1103 wake_up(&device->al_wait);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001104 } else {
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02001105 drbd_alert(device, "LOGIC BUG\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07001106 }
1107 }
1108 /* TRY. */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001109 e = lc_try_get(device->resync, enr);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001110 bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL;
1111 if (bm_ext) {
1112 if (test_bit(BME_LOCKED, &bm_ext->flags))
1113 goto proceed;
1114 if (!test_and_set_bit(BME_NO_WRITES, &bm_ext->flags)) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001115 device->resync_locked++;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001116 } else {
1117 /* we did set the BME_NO_WRITES,
1118 * but then could not set BME_LOCKED,
1119 * so we tried again.
1120 * drop the extra reference. */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001121 bm_ext->lce.refcnt--;
Andreas Gruenbacher0b0ba1e2011-06-27 16:23:33 +02001122 D_ASSERT(device, bm_ext->lce.refcnt > 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001123 }
1124 goto check_al;
1125 } else {
1126 /* do we rather want to try later? */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001127 if (device->resync_locked > device->resync->nr_elements-3)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001128 goto try_again;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001129 /* Do or do not. There is no try. -- Yoda */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001130 e = lc_get(device->resync, enr);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001131 bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL;
1132 if (!bm_ext) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001133 const unsigned long rs_flags = device->resync->flags;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001134 if (rs_flags & LC_STARVING)
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02001135 drbd_warn(device, "Have to wait for element"
Philipp Reisnerb411b362009-09-25 16:07:19 -07001136 " (resync LRU too small?)\n");
Lars Ellenberg46a15bc2011-02-21 13:21:01 +01001137 BUG_ON(rs_flags & LC_LOCKED);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001138 goto try_again;
1139 }
1140 if (bm_ext->lce.lc_number != enr) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001141 bm_ext->rs_left = drbd_bm_e_weight(device, enr);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001142 bm_ext->rs_failed = 0;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001143 lc_committed(device->resync);
1144 wake_up(&device->al_wait);
Andreas Gruenbacher0b0ba1e2011-06-27 16:23:33 +02001145 D_ASSERT(device, test_bit(BME_LOCKED, &bm_ext->flags) == 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001146 }
1147 set_bit(BME_NO_WRITES, &bm_ext->flags);
Andreas Gruenbacher0b0ba1e2011-06-27 16:23:33 +02001148 D_ASSERT(device, bm_ext->lce.refcnt == 1);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001149 device->resync_locked++;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001150 goto check_al;
1151 }
1152check_al:
Philipp Reisnerb411b362009-09-25 16:07:19 -07001153 for (i = 0; i < AL_EXT_PER_BM_SECT; i++) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001154 if (lc_is_used(device->act_log, al_enr+i))
Philipp Reisnerb411b362009-09-25 16:07:19 -07001155 goto try_again;
1156 }
1157 set_bit(BME_LOCKED, &bm_ext->flags);
1158proceed:
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001159 device->resync_wenr = LC_FREE;
1160 spin_unlock_irq(&device->al_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001161 return 0;
1162
1163try_again:
Philipp Reisnerb411b362009-09-25 16:07:19 -07001164 if (bm_ext)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001165 device->resync_wenr = enr;
1166 spin_unlock_irq(&device->al_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001167 return -EAGAIN;
1168}
1169
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001170void drbd_rs_complete_io(struct drbd_device *device, sector_t sector)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001171{
1172 unsigned int enr = BM_SECT_TO_EXT(sector);
1173 struct lc_element *e;
1174 struct bm_extent *bm_ext;
1175 unsigned long flags;
1176
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001177 spin_lock_irqsave(&device->al_lock, flags);
1178 e = lc_find(device->resync, enr);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001179 bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL;
1180 if (!bm_ext) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001181 spin_unlock_irqrestore(&device->al_lock, flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001182 if (__ratelimit(&drbd_ratelimit_state))
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02001183 drbd_err(device, "drbd_rs_complete_io() called, but extent not found\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -07001184 return;
1185 }
1186
1187 if (bm_ext->lce.refcnt == 0) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001188 spin_unlock_irqrestore(&device->al_lock, flags);
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02001189 drbd_err(device, "drbd_rs_complete_io(,%llu [=%u]) called, "
Philipp Reisnerb411b362009-09-25 16:07:19 -07001190 "but refcnt is 0!?\n",
1191 (unsigned long long)sector, enr);
1192 return;
1193 }
1194
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001195 if (lc_put(device->resync, &bm_ext->lce) == 0) {
Philipp Reisnere3555d82010-11-07 15:56:29 +01001196 bm_ext->flags = 0; /* clear BME_LOCKED, BME_NO_WRITES and BME_PRIORITY */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001197 device->resync_locked--;
1198 wake_up(&device->al_wait);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001199 }
1200
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001201 spin_unlock_irqrestore(&device->al_lock, flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001202}
1203
1204/**
1205 * drbd_rs_cancel_all() - Removes all extents from the resync LRU (even BME_LOCKED)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001206 * @device: DRBD device.
Philipp Reisnerb411b362009-09-25 16:07:19 -07001207 */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001208void drbd_rs_cancel_all(struct drbd_device *device)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001209{
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001210 spin_lock_irq(&device->al_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001211
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001212 if (get_ldev_if_state(device, D_FAILED)) { /* Makes sure ->resync is there. */
1213 lc_reset(device->resync);
1214 put_ldev(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001215 }
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001216 device->resync_locked = 0;
1217 device->resync_wenr = LC_FREE;
1218 spin_unlock_irq(&device->al_lock);
1219 wake_up(&device->al_wait);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001220}
1221
1222/**
1223 * drbd_rs_del_all() - Gracefully remove all extents from the resync LRU
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001224 * @device: DRBD device.
Philipp Reisnerb411b362009-09-25 16:07:19 -07001225 *
1226 * Returns 0 upon success, -EAGAIN if at least one reference count was
1227 * not zero.
1228 */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001229int drbd_rs_del_all(struct drbd_device *device)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001230{
1231 struct lc_element *e;
1232 struct bm_extent *bm_ext;
1233 int i;
1234
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001235 spin_lock_irq(&device->al_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001236
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001237 if (get_ldev_if_state(device, D_FAILED)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001238 /* ok, ->resync is there. */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001239 for (i = 0; i < device->resync->nr_elements; i++) {
1240 e = lc_element_by_index(device->resync, i);
Philipp Reisnerb2b163d2010-04-02 08:40:33 +02001241 bm_ext = lc_entry(e, struct bm_extent, lce);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001242 if (bm_ext->lce.lc_number == LC_FREE)
1243 continue;
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001244 if (bm_ext->lce.lc_number == device->resync_wenr) {
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02001245 drbd_info(device, "dropping %u in drbd_rs_del_all, apparently"
Philipp Reisnerb411b362009-09-25 16:07:19 -07001246 " got 'synced' by application io\n",
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001247 device->resync_wenr);
Andreas Gruenbacher0b0ba1e2011-06-27 16:23:33 +02001248 D_ASSERT(device, !test_bit(BME_LOCKED, &bm_ext->flags));
1249 D_ASSERT(device, test_bit(BME_NO_WRITES, &bm_ext->flags));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001250 clear_bit(BME_NO_WRITES, &bm_ext->flags);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001251 device->resync_wenr = LC_FREE;
1252 lc_put(device->resync, &bm_ext->lce);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001253 }
1254 if (bm_ext->lce.refcnt != 0) {
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02001255 drbd_info(device, "Retrying drbd_rs_del_all() later. "
Philipp Reisnerb411b362009-09-25 16:07:19 -07001256 "refcnt=%d\n", bm_ext->lce.refcnt);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001257 put_ldev(device);
1258 spin_unlock_irq(&device->al_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001259 return -EAGAIN;
1260 }
Andreas Gruenbacher0b0ba1e2011-06-27 16:23:33 +02001261 D_ASSERT(device, !test_bit(BME_LOCKED, &bm_ext->flags));
1262 D_ASSERT(device, !test_bit(BME_NO_WRITES, &bm_ext->flags));
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001263 lc_del(device->resync, &bm_ext->lce);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001264 }
Andreas Gruenbacher0b0ba1e2011-06-27 16:23:33 +02001265 D_ASSERT(device, device->resync->used == 0);
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001266 put_ldev(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001267 }
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001268 spin_unlock_irq(&device->al_lock);
1269 wake_up(&device->al_wait);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001270
1271 return 0;
1272}
1273
1274/**
1275 * drbd_rs_failed_io() - Record information on a failure to resync the specified blocks
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001276 * @device: DRBD device.
Philipp Reisnerb411b362009-09-25 16:07:19 -07001277 * @sector: The sector number.
1278 * @size: Size of failed IO operation, in byte.
1279 */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001280void drbd_rs_failed_io(struct drbd_device *device, sector_t sector, int size)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001281{
1282 /* Is called from worker and receiver context _only_ */
1283 unsigned long sbnr, ebnr, lbnr;
1284 unsigned long count;
1285 sector_t esector, nr_sectors;
1286 int wake_up = 0;
1287
Lars Ellenberga0fb3c42014-04-28 18:43:23 +02001288 if (size <= 0 || !IS_ALIGNED(size, 512) || size > DRBD_MAX_DISCARD_SIZE) {
Andreas Gruenbacherd0180172011-07-03 17:53:52 +02001289 drbd_err(device, "drbd_rs_failed_io: sector=%llus size=%d nonsense!\n",
Philipp Reisnerb411b362009-09-25 16:07:19 -07001290 (unsigned long long)sector, size);
1291 return;
1292 }
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001293 nr_sectors = drbd_get_capacity(device->this_bdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001294 esector = sector + (size >> 9) - 1;
1295
Andreas Gruenbacher841ce242010-12-15 19:31:20 +01001296 if (!expect(sector < nr_sectors))
1297 return;
1298 if (!expect(esector < nr_sectors))
1299 esector = nr_sectors - 1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001300
1301 lbnr = BM_SECT_TO_BIT(nr_sectors-1);
1302
1303 /*
1304 * round up start sector, round down end sector. we make sure we only
1305 * handle full, aligned, BM_BLOCK_SIZE (4K) blocks */
1306 if (unlikely(esector < BM_SECT_PER_BIT-1))
1307 return;
1308 if (unlikely(esector == (nr_sectors-1)))
1309 ebnr = lbnr;
1310 else
1311 ebnr = BM_SECT_TO_BIT(esector - (BM_SECT_PER_BIT-1));
1312 sbnr = BM_SECT_TO_BIT(sector + BM_SECT_PER_BIT-1);
1313
1314 if (sbnr > ebnr)
1315 return;
1316
1317 /*
1318 * ok, (capacity & 7) != 0 sometimes, but who cares...
1319 * we count rs_{total,left} in bits, not sectors.
1320 */
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001321 spin_lock_irq(&device->al_lock);
1322 count = drbd_bm_count_bits(device, sbnr, ebnr);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001323 if (count) {
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001324 device->rs_failed += count;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001325
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001326 if (get_ldev(device)) {
1327 drbd_try_clear_on_disk_bm(device, sector, count, false);
1328 put_ldev(device);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001329 }
1330
1331 /* just wake_up unconditional now, various lc_chaged(),
1332 * lc_put() in drbd_try_clear_on_disk_bm(). */
1333 wake_up = 1;
1334 }
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001335 spin_unlock_irq(&device->al_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001336 if (wake_up)
Andreas Gruenbacherb30ab792011-07-03 13:26:43 +02001337 wake_up(&device->al_wait);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001338}