blob: 5f0eeb74a5702cd932f1ed7ba6f5d18b28df3947 [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#include "drbd_wrappers.h"
33
Lars Ellenberg85f103d2011-03-31 12:06:48 +020034
35enum al_transaction_types {
36 AL_TR_UPDATE = 0,
37 AL_TR_INITIALIZED = 0xffff
38};
Lars Ellenberg7ad651b2011-02-21 13:21:03 +010039/* all fields on disc in big endian */
40struct __packed al_transaction_on_disk {
41 /* don't we all like magic */
42 __be32 magic;
43
44 /* to identify the most recent transaction block
45 * in the on disk ring buffer */
46 __be32 tr_number;
47
48 /* checksum on the full 4k block, with this field set to 0. */
49 __be32 crc32c;
50
51 /* type of transaction, special transaction types like:
Lars Ellenberg85f103d2011-03-31 12:06:48 +020052 * purge-all, set-all-idle, set-all-active, ... to-be-defined
53 * see also enum al_transaction_types */
Lars Ellenberg7ad651b2011-02-21 13:21:03 +010054 __be16 transaction_type;
55
56 /* we currently allow only a few thousand extents,
57 * so 16bit will be enough for the slot number. */
58
59 /* how many updates in this transaction */
60 __be16 n_updates;
61
62 /* maximum slot number, "al-extents" in drbd.conf speak.
63 * Having this in each transaction should make reconfiguration
64 * of that parameter easier. */
65 __be16 context_size;
66
67 /* slot number the context starts with */
68 __be16 context_start_slot_nr;
69
70 /* Some reserved bytes. Expected usage is a 64bit counter of
71 * sectors-written since device creation, and other data generation tag
72 * supporting usage */
73 __be32 __reserved[4];
74
75 /* --- 36 byte used --- */
76
77 /* Reserve space for up to AL_UPDATES_PER_TRANSACTION changes
78 * in one transaction, then use the remaining byte in the 4k block for
79 * context information. "Flexible" number of updates per transaction
80 * does not help, as we have to account for the case when all update
81 * slots are used anyways, so it would only complicate code without
82 * additional benefit.
83 */
84 __be16 update_slot_nr[AL_UPDATES_PER_TRANSACTION];
85
86 /* but the extent number is 32bit, which at an extent size of 4 MiB
87 * allows to cover device sizes of up to 2**54 Byte (16 PiB) */
88 __be32 update_extent_nr[AL_UPDATES_PER_TRANSACTION];
89
90 /* --- 420 bytes used (36 + 64*6) --- */
91
92 /* 4096 - 420 = 3676 = 919 * 4 */
93 __be32 context[AL_CONTEXT_PER_TRANSACTION];
Philipp Reisnerb411b362009-09-25 16:07:19 -070094};
95
96struct update_odbm_work {
97 struct drbd_work w;
98 unsigned int enr;
99};
100
101struct update_al_work {
102 struct drbd_work w;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700103 struct completion event;
Lars Ellenberg7ad651b2011-02-21 13:21:03 +0100104 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700105};
106
107struct drbd_atodb_wait {
108 atomic_t count;
109 struct completion io_done;
110 struct drbd_conf *mdev;
111 int error;
112};
113
114
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +0100115static int w_al_write_transaction(struct drbd_work *, int);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700116
Philipp Reisnerb411b362009-09-25 16:07:19 -0700117static int _drbd_md_sync_page_io(struct drbd_conf *mdev,
118 struct drbd_backing_dev *bdev,
119 struct page *page, sector_t sector,
120 int rw, int size)
121{
122 struct bio *bio;
123 struct drbd_md_io md_io;
Andreas Gruenbacherac29f402010-12-13 02:20:47 +0100124 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700125
126 md_io.mdev = mdev;
127 init_completion(&md_io.event);
128 md_io.error = 0;
129
Philipp Reisnera8a4e512010-08-25 10:21:04 +0200130 if ((rw & WRITE) && !test_bit(MD_NO_FUA, &mdev->flags))
Lars Ellenberg86e1e982011-06-28 13:22:48 +0200131 rw |= REQ_FUA | REQ_FLUSH;
Jens Axboe721a9602011-03-09 11:56:30 +0100132 rw |= REQ_SYNC;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700133
Lars Ellenbergda4a75d2011-02-23 17:02:01 +0100134 bio = bio_alloc_drbd(GFP_NOIO);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700135 bio->bi_bdev = bdev->md_bdev;
136 bio->bi_sector = sector;
Andreas Gruenbacherac29f402010-12-13 02:20:47 +0100137 err = -EIO;
138 if (bio_add_page(bio, page, size, 0) != size)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700139 goto out;
140 bio->bi_private = &md_io;
141 bio->bi_end_io = drbd_md_io_complete;
142 bio->bi_rw = rw;
143
Andreas Gruenbacher0cf9d272010-12-07 10:43:29 +0100144 if (drbd_insert_fault(mdev, (rw & WRITE) ? DRBD_FAULT_MD_WR : DRBD_FAULT_MD_RD))
Philipp Reisnerb411b362009-09-25 16:07:19 -0700145 bio_endio(bio, -EIO);
146 else
147 submit_bio(rw, bio);
148 wait_for_completion(&md_io.event);
Andreas Gruenbacherac29f402010-12-13 02:20:47 +0100149 if (bio_flagged(bio, BIO_UPTODATE))
150 err = md_io.error;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700151
Philipp Reisnerb411b362009-09-25 16:07:19 -0700152 out:
153 bio_put(bio);
Andreas Gruenbacherac29f402010-12-13 02:20:47 +0100154 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700155}
156
157int drbd_md_sync_page_io(struct drbd_conf *mdev, struct drbd_backing_dev *bdev,
158 sector_t sector, int rw)
159{
Andreas Gruenbacher3fbf4d22010-12-13 02:25:41 +0100160 int err;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700161 struct page *iop = mdev->md_io_page;
162
163 D_ASSERT(mutex_is_locked(&mdev->md_io_mutex));
164
165 BUG_ON(!bdev->md_bdev);
166
Lars Ellenberg7ad651b2011-02-21 13:21:03 +0100167 dev_dbg(DEV, "meta_data io: %s [%d]:%s(,%llus,%s)\n",
168 current->comm, current->pid, __func__,
169 (unsigned long long)sector, (rw & WRITE) ? "WRITE" : "READ");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700170
171 if (sector < drbd_md_first_sector(bdev) ||
Lars Ellenberg7ad651b2011-02-21 13:21:03 +0100172 sector + 7 > drbd_md_last_sector(bdev))
Philipp Reisnerb411b362009-09-25 16:07:19 -0700173 dev_alert(DEV, "%s [%d]:%s(,%llus,%s) out of range md access!\n",
174 current->comm, current->pid, __func__,
175 (unsigned long long)sector, (rw & WRITE) ? "WRITE" : "READ");
176
Andreas Gruenbacher3fbf4d22010-12-13 02:25:41 +0100177 err = _drbd_md_sync_page_io(mdev, bdev, iop, sector, rw, MD_BLOCK_SIZE);
178 if (err) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700179 dev_err(DEV, "drbd_md_sync_page_io(,%llus,%s) failed!\n",
180 (unsigned long long)sector, (rw & WRITE) ? "WRITE" : "READ");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700181 }
Andreas Gruenbacher3fbf4d22010-12-13 02:25:41 +0100182 return err;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700183}
184
185static struct lc_element *_al_get(struct drbd_conf *mdev, unsigned int enr)
186{
187 struct lc_element *al_ext;
188 struct lc_element *tmp;
Philipp Reisnerf91ab622010-11-09 13:59:41 +0100189 int wake;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700190
191 spin_lock_irq(&mdev->al_lock);
192 tmp = lc_find(mdev->resync, enr/AL_EXT_PER_BM_SECT);
193 if (unlikely(tmp != NULL)) {
194 struct bm_extent *bm_ext = lc_entry(tmp, struct bm_extent, lce);
195 if (test_bit(BME_NO_WRITES, &bm_ext->flags)) {
Philipp Reisnerf91ab622010-11-09 13:59:41 +0100196 wake = !test_and_set_bit(BME_PRIORITY, &bm_ext->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700197 spin_unlock_irq(&mdev->al_lock);
Philipp Reisnerf91ab622010-11-09 13:59:41 +0100198 if (wake)
199 wake_up(&mdev->al_wait);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700200 return NULL;
201 }
202 }
Lars Ellenberg46a15bc2011-02-21 13:21:01 +0100203 al_ext = lc_get(mdev->act_log, enr);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700204 spin_unlock_irq(&mdev->al_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700205 return al_ext;
206}
207
Lars Ellenberg181286a2011-03-31 15:18:56 +0200208void drbd_al_begin_io(struct drbd_conf *mdev, struct drbd_interval *i)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700209{
Lars Ellenberg181286a2011-03-31 15:18:56 +0200210 unsigned int enr = (i->sector >> (AL_EXTENT_SHIFT-9));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700211 struct lc_element *al_ext;
212 struct update_al_work al_work;
213
214 D_ASSERT(atomic_read(&mdev->local_cnt) > 0);
215
Philipp Reisnerb411b362009-09-25 16:07:19 -0700216 wait_event(mdev->al_wait, (al_ext = _al_get(mdev, enr)));
217
218 if (al_ext->lc_number != enr) {
219 /* drbd_al_write_transaction(mdev,al_ext,enr);
220 * recurses into generic_make_request(), which
221 * disallows recursion, bios being serialized on the
222 * current->bio_tail list now.
223 * we have to delegate updates to the activity log
224 * to the worker thread. */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700225
Lars Ellenberg7ad651b2011-02-21 13:21:03 +0100226 /* Serialize multiple transactions.
227 * This uses test_and_set_bit, memory barrier is implicit.
228 * Optimization potential:
229 * first check for transaction number > old transaction number,
230 * so not all waiters have to lock/unlock. */
231 wait_event(mdev->al_wait, lc_try_lock_for_transaction(mdev->act_log));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700232
Lars Ellenberg7ad651b2011-02-21 13:21:03 +0100233 /* Double check: it may have been committed by someone else,
234 * while we have been waiting for the lock. */
235 if (al_ext->lc_number != enr) {
236 init_completion(&al_work.event);
237 al_work.w.cb = w_al_write_transaction;
238 al_work.w.mdev = mdev;
239 drbd_queue_work_front(&mdev->tconn->data.work, &al_work.w);
240 wait_for_completion(&al_work.event);
241
242 mdev->al_writ_cnt++;
243
244 spin_lock_irq(&mdev->al_lock);
245 /* FIXME
246 if (al_work.err)
247 we need an "lc_cancel" here;
248 */
249 lc_committed(mdev->act_log);
250 spin_unlock_irq(&mdev->al_lock);
251 }
252 lc_unlock(mdev->act_log);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700253 wake_up(&mdev->al_wait);
254 }
255}
256
Lars Ellenberg181286a2011-03-31 15:18:56 +0200257void drbd_al_complete_io(struct drbd_conf *mdev, struct drbd_interval *i)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700258{
Lars Ellenberg181286a2011-03-31 15:18:56 +0200259 unsigned int enr = (i->sector >> (AL_EXTENT_SHIFT-9));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700260 struct lc_element *extent;
261 unsigned long flags;
262
Philipp Reisnerb411b362009-09-25 16:07:19 -0700263 spin_lock_irqsave(&mdev->al_lock, flags);
264
265 extent = lc_find(mdev->act_log, enr);
266
267 if (!extent) {
268 spin_unlock_irqrestore(&mdev->al_lock, flags);
269 dev_err(DEV, "al_complete_io() called on inactive extent %u\n", enr);
270 return;
271 }
272
273 if (lc_put(mdev->act_log, extent) == 0)
274 wake_up(&mdev->al_wait);
275
276 spin_unlock_irqrestore(&mdev->al_lock, flags);
277}
278
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100279#if (PAGE_SHIFT + 3) < (AL_EXTENT_SHIFT - BM_BLOCK_SHIFT)
280/* Currently BM_BLOCK_SHIFT, BM_EXT_SHIFT and AL_EXTENT_SHIFT
281 * are still coupled, or assume too much about their relation.
282 * Code below will not work if this is violated.
283 * Will be cleaned up with some followup patch.
284 */
285# error FIXME
286#endif
287
288static unsigned int al_extent_to_bm_page(unsigned int al_enr)
289{
290 return al_enr >>
291 /* bit to page */
292 ((PAGE_SHIFT + 3) -
293 /* al extent number to bit */
294 (AL_EXTENT_SHIFT - BM_BLOCK_SHIFT));
295}
296
297static unsigned int rs_extent_to_bm_page(unsigned int rs_enr)
298{
299 return rs_enr >>
300 /* bit to page */
301 ((PAGE_SHIFT + 3) -
302 /* al extent number to bit */
303 (BM_EXT_SHIFT - BM_BLOCK_SHIFT));
304}
305
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +0100306static int
Philipp Reisner00d56942011-02-09 18:09:48 +0100307w_al_write_transaction(struct drbd_work *w, int unused)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700308{
309 struct update_al_work *aw = container_of(w, struct update_al_work, w);
Philipp Reisner00d56942011-02-09 18:09:48 +0100310 struct drbd_conf *mdev = w->mdev;
Lars Ellenberg7ad651b2011-02-21 13:21:03 +0100311 struct al_transaction_on_disk *buffer;
312 struct lc_element *e;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700313 sector_t sector;
Lars Ellenberg7ad651b2011-02-21 13:21:03 +0100314 int i, mx;
315 unsigned extent_nr;
316 unsigned crc = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700317
318 if (!get_ldev(mdev)) {
Lars Ellenberg7ad651b2011-02-21 13:21:03 +0100319 dev_err(DEV, "disk is %s, cannot start al transaction\n",
320 drbd_disk_str(mdev->state.disk));
321 aw->err = -EIO;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700322 complete(&((struct update_al_work *)w)->event);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +0100323 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700324 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700325
Lars Ellenberg6719fb02010-10-18 23:04:07 +0200326 /* The bitmap write may have failed, causing a state change. */
327 if (mdev->state.disk < D_INCONSISTENT) {
328 dev_err(DEV,
Lars Ellenberg7ad651b2011-02-21 13:21:03 +0100329 "disk is %s, cannot write al transaction\n",
330 drbd_disk_str(mdev->state.disk));
331 aw->err = -EIO;
Lars Ellenberg6719fb02010-10-18 23:04:07 +0200332 complete(&((struct update_al_work *)w)->event);
333 put_ldev(mdev);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +0100334 return 0;
Lars Ellenberg6719fb02010-10-18 23:04:07 +0200335 }
336
337 mutex_lock(&mdev->md_io_mutex); /* protects md_io_buffer, al_tr_cycle, ... */
Lars Ellenberg7ad651b2011-02-21 13:21:03 +0100338 buffer = page_address(mdev->md_io_page);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700339
Lars Ellenberg7ad651b2011-02-21 13:21:03 +0100340 memset(buffer, 0, sizeof(*buffer));
341 buffer->magic = cpu_to_be32(DRBD_AL_MAGIC);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700342 buffer->tr_number = cpu_to_be32(mdev->al_tr_number);
343
Lars Ellenberg7ad651b2011-02-21 13:21:03 +0100344 i = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700345
Lars Ellenberg7ad651b2011-02-21 13:21:03 +0100346 /* Even though no one can start to change this list
347 * once we set the LC_LOCKED -- from drbd_al_begin_io(),
348 * lc_try_lock_for_transaction() --, someone may still
349 * be in the process of changing it. */
350 spin_lock_irq(&mdev->al_lock);
351 list_for_each_entry(e, &mdev->act_log->to_be_changed, list) {
352 if (i == AL_UPDATES_PER_TRANSACTION) {
353 i++;
354 break;
355 }
356 buffer->update_slot_nr[i] = cpu_to_be16(e->lc_index);
357 buffer->update_extent_nr[i] = cpu_to_be32(e->lc_new_number);
358 if (e->lc_number != LC_FREE)
359 drbd_bm_mark_for_writeout(mdev,
360 al_extent_to_bm_page(e->lc_number));
361 i++;
362 }
363 spin_unlock_irq(&mdev->al_lock);
364 BUG_ON(i > AL_UPDATES_PER_TRANSACTION);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700365
Lars Ellenberg7ad651b2011-02-21 13:21:03 +0100366 buffer->n_updates = cpu_to_be16(i);
367 for ( ; i < AL_UPDATES_PER_TRANSACTION; i++) {
368 buffer->update_slot_nr[i] = cpu_to_be16(-1);
369 buffer->update_extent_nr[i] = cpu_to_be32(LC_FREE);
370 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700371
Lars Ellenberg7ad651b2011-02-21 13:21:03 +0100372 buffer->context_size = cpu_to_be16(mdev->act_log->nr_elements);
373 buffer->context_start_slot_nr = cpu_to_be16(mdev->al_tr_cycle);
374
375 mx = min_t(int, AL_CONTEXT_PER_TRANSACTION,
Philipp Reisnerb411b362009-09-25 16:07:19 -0700376 mdev->act_log->nr_elements - mdev->al_tr_cycle);
377 for (i = 0; i < mx; i++) {
378 unsigned idx = mdev->al_tr_cycle + i;
379 extent_nr = lc_element_by_index(mdev->act_log, idx)->lc_number;
Lars Ellenberg7ad651b2011-02-21 13:21:03 +0100380 buffer->context[i] = cpu_to_be32(extent_nr);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700381 }
Lars Ellenberg7ad651b2011-02-21 13:21:03 +0100382 for (; i < AL_CONTEXT_PER_TRANSACTION; i++)
383 buffer->context[i] = cpu_to_be32(LC_FREE);
384
385 mdev->al_tr_cycle += AL_CONTEXT_PER_TRANSACTION;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700386 if (mdev->al_tr_cycle >= mdev->act_log->nr_elements)
387 mdev->al_tr_cycle = 0;
388
Philipp Reisnerb411b362009-09-25 16:07:19 -0700389 sector = mdev->ldev->md.md_offset
Lars Ellenberg7ad651b2011-02-21 13:21:03 +0100390 + mdev->ldev->md.al_offset
391 + mdev->al_tr_pos * (MD_BLOCK_SIZE>>9);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700392
Lars Ellenberg7ad651b2011-02-21 13:21:03 +0100393 crc = crc32c(0, buffer, 4096);
394 buffer->crc32c = cpu_to_be32(crc);
395
396 if (drbd_bm_write_hinted(mdev))
397 aw->err = -EIO;
398 /* drbd_chk_io_error done already */
Andreas Gruenbacher3fbf4d22010-12-13 02:25:41 +0100399 else if (drbd_md_sync_page_io(mdev, mdev->ldev, sector, WRITE)) {
Lars Ellenberg7ad651b2011-02-21 13:21:03 +0100400 aw->err = -EIO;
Andreas Gruenbacher81e84652010-12-09 15:03:57 +0100401 drbd_chk_io_error(mdev, 1, true);
Lars Ellenberg7ad651b2011-02-21 13:21:03 +0100402 } else {
403 /* advance ringbuffer position and transaction counter */
404 mdev->al_tr_pos = (mdev->al_tr_pos + 1) % (MD_AL_SECTORS*512/MD_BLOCK_SIZE);
405 mdev->al_tr_number++;
406 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700407
408 mutex_unlock(&mdev->md_io_mutex);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700409 complete(&((struct update_al_work *)w)->event);
410 put_ldev(mdev);
411
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +0100412 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700413}
414
Lars Ellenberg7ad651b2011-02-21 13:21:03 +0100415/* FIXME
416 * reading of the activity log,
417 * and potentially dirtying of the affected bitmap regions,
418 * should be done from userland only.
419 * DRBD would simply always attach with an empty activity log,
420 * and refuse to attach to something that looks like a crashed primary.
421 */
422
Philipp Reisnerb411b362009-09-25 16:07:19 -0700423/**
424 * drbd_al_read_tr() - Read a single transaction from the on disk activity log
425 * @mdev: DRBD device.
426 * @bdev: Block device to read form.
427 * @b: pointer to an al_transaction.
428 * @index: On disk slot of the transaction to read.
429 *
430 * Returns -1 on IO error, 0 on checksum error and 1 upon success.
431 */
432static int drbd_al_read_tr(struct drbd_conf *mdev,
433 struct drbd_backing_dev *bdev,
Philipp Reisnerb411b362009-09-25 16:07:19 -0700434 int index)
435{
Lars Ellenberg7ad651b2011-02-21 13:21:03 +0100436 struct al_transaction_on_disk *b = page_address(mdev->md_io_page);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700437 sector_t sector;
Lars Ellenberg7ad651b2011-02-21 13:21:03 +0100438 u32 crc;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700439
Lars Ellenberg7ad651b2011-02-21 13:21:03 +0100440 sector = bdev->md.md_offset
441 + bdev->md.al_offset
442 + index * (MD_BLOCK_SIZE>>9);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700443
444 /* Dont process error normally,
445 * as this is done before disk is attached! */
Andreas Gruenbacher3fbf4d22010-12-13 02:25:41 +0100446 if (drbd_md_sync_page_io(mdev, bdev, sector, READ))
Philipp Reisnerb411b362009-09-25 16:07:19 -0700447 return -1;
448
Lars Ellenberg7ad651b2011-02-21 13:21:03 +0100449 if (!expect(b->magic == cpu_to_be32(DRBD_AL_MAGIC)))
450 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700451
Lars Ellenberg7ad651b2011-02-21 13:21:03 +0100452 if (!expect(be16_to_cpu(b->n_updates) <= AL_UPDATES_PER_TRANSACTION))
453 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700454
Lars Ellenberg7ad651b2011-02-21 13:21:03 +0100455 if (!expect(be16_to_cpu(b->context_size) <= DRBD_AL_EXTENTS_MAX))
456 return 0;
457
458 if (!expect(be16_to_cpu(b->context_start_slot_nr) < DRBD_AL_EXTENTS_MAX))
459 return 0;
460
461 crc = be32_to_cpu(b->crc32c);
462 b->crc32c = 0;
463 if (!expect(crc == crc32c(0, b, 4096)))
464 return 0;
465
466 return 1;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700467}
468
469/**
470 * drbd_al_read_log() - Restores the activity log from its on disk representation.
471 * @mdev: DRBD device.
472 * @bdev: Block device to read form.
473 *
474 * Returns 1 on success, returns 0 when reading the log failed due to IO errors.
475 */
476int drbd_al_read_log(struct drbd_conf *mdev, struct drbd_backing_dev *bdev)
477{
Lars Ellenberg7ad651b2011-02-21 13:21:03 +0100478 struct al_transaction_on_disk *b;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700479 int i;
480 int rv;
481 int mx;
482 int active_extents = 0;
483 int transactions = 0;
484 int found_valid = 0;
Lars Ellenberg85f103d2011-03-31 12:06:48 +0200485 int found_initialized = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700486 int from = 0;
487 int to = 0;
488 u32 from_tnr = 0;
489 u32 to_tnr = 0;
490 u32 cnr;
491
Lars Ellenberg7ad651b2011-02-21 13:21:03 +0100492 /* Note that this is expected to be called with a newly created,
493 * clean and all unused activity log of the "expected size".
494 */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700495
496 /* lock out all other meta data io for now,
497 * and make sure the page is mapped.
498 */
499 mutex_lock(&mdev->md_io_mutex);
Lars Ellenberg7ad651b2011-02-21 13:21:03 +0100500 b = page_address(mdev->md_io_page);
501
502 /* Always use the full ringbuffer space for now.
503 * possible optimization: read in all of it,
504 * then scan the in-memory pages. */
505
506 mx = (MD_AL_SECTORS*512/MD_BLOCK_SIZE);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700507
508 /* Find the valid transaction in the log */
Lars Ellenberg7ad651b2011-02-21 13:21:03 +0100509 for (i = 0; i < mx; i++) {
510 rv = drbd_al_read_tr(mdev, bdev, i);
511 /* invalid data in that block */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700512 if (rv == 0)
513 continue;
Lars Ellenberg85f103d2011-03-31 12:06:48 +0200514 if (be16_to_cpu(b->transaction_type) == AL_TR_INITIALIZED) {
515 ++found_initialized;
516 continue;
517 }
Lars Ellenberg7ad651b2011-02-21 13:21:03 +0100518
519 /* IO error */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700520 if (rv == -1) {
521 mutex_unlock(&mdev->md_io_mutex);
522 return 0;
523 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700524
Lars Ellenberg7ad651b2011-02-21 13:21:03 +0100525 cnr = be32_to_cpu(b->tr_number);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700526 if (++found_valid == 1) {
527 from = i;
528 to = i;
529 from_tnr = cnr;
530 to_tnr = cnr;
531 continue;
532 }
Lars Ellenberg7ad651b2011-02-21 13:21:03 +0100533
534 D_ASSERT(cnr != to_tnr);
535 D_ASSERT(cnr != from_tnr);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700536 if ((int)cnr - (int)from_tnr < 0) {
Lars Ellenberg7ad651b2011-02-21 13:21:03 +0100537 D_ASSERT(from_tnr - cnr + i - from == mx);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700538 from = i;
539 from_tnr = cnr;
540 }
541 if ((int)cnr - (int)to_tnr > 0) {
542 D_ASSERT(cnr - to_tnr == i - to);
543 to = i;
544 to_tnr = cnr;
545 }
546 }
547
548 if (!found_valid) {
Lars Ellenberg85f103d2011-03-31 12:06:48 +0200549 if (found_initialized != mx)
550 dev_warn(DEV, "No usable activity log found.\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700551 mutex_unlock(&mdev->md_io_mutex);
552 return 1;
553 }
554
555 /* Read the valid transactions.
556 * dev_info(DEV, "Reading from %d to %d.\n",from,to); */
557 i = from;
558 while (1) {
Lars Ellenberg7ad651b2011-02-21 13:21:03 +0100559 struct lc_element *e;
560 unsigned j, n, slot, extent_nr;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700561
Lars Ellenberg7ad651b2011-02-21 13:21:03 +0100562 rv = drbd_al_read_tr(mdev, bdev, i);
Andreas Gruenbacher841ce242010-12-15 19:31:20 +0100563 if (!expect(rv != 0))
564 goto cancel;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700565 if (rv == -1) {
566 mutex_unlock(&mdev->md_io_mutex);
567 return 0;
568 }
569
Lars Ellenberg7ad651b2011-02-21 13:21:03 +0100570 /* deal with different transaction types.
571 * not yet implemented */
572 if (!expect(b->transaction_type == 0))
573 goto cancel;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700574
Lars Ellenberg7ad651b2011-02-21 13:21:03 +0100575 /* on the fly re-create/resize activity log?
576 * will be a special transaction type flag. */
577 if (!expect(be16_to_cpu(b->context_size) == mdev->act_log->nr_elements))
578 goto cancel;
579 if (!expect(be16_to_cpu(b->context_start_slot_nr) < mdev->act_log->nr_elements))
580 goto cancel;
581
582 /* We are the only user of the activity log right now,
583 * don't actually need to take that lock. */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700584 spin_lock_irq(&mdev->al_lock);
585
Lars Ellenberg7ad651b2011-02-21 13:21:03 +0100586 /* first, apply the context, ... */
587 for (j = 0, slot = be16_to_cpu(b->context_start_slot_nr);
588 j < AL_CONTEXT_PER_TRANSACTION &&
589 slot < mdev->act_log->nr_elements; j++, slot++) {
590 extent_nr = be32_to_cpu(b->context[j]);
591 e = lc_element_by_index(mdev->act_log, slot);
592 if (e->lc_number != extent_nr) {
593 if (extent_nr != LC_FREE)
594 active_extents++;
595 else
596 active_extents--;
597 }
598 lc_set(mdev->act_log, extent_nr, slot);
599 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700600
Lars Ellenberg7ad651b2011-02-21 13:21:03 +0100601 /* ... then apply the updates,
602 * which override the context information.
603 * drbd_al_read_tr already did the rangecheck
604 * on n <= AL_UPDATES_PER_TRANSACTION */
605 n = be16_to_cpu(b->n_updates);
606 for (j = 0; j < n; j++) {
607 slot = be16_to_cpu(b->update_slot_nr[j]);
608 extent_nr = be32_to_cpu(b->update_extent_nr[j]);
609 if (!expect(slot < mdev->act_log->nr_elements))
610 break;
611 e = lc_element_by_index(mdev->act_log, slot);
612 if (e->lc_number != extent_nr) {
613 if (extent_nr != LC_FREE)
614 active_extents++;
615 else
616 active_extents--;
617 }
618 lc_set(mdev->act_log, extent_nr, slot);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700619 }
620 spin_unlock_irq(&mdev->al_lock);
621
622 transactions++;
623
624cancel:
625 if (i == to)
626 break;
627 i++;
Lars Ellenberg7ad651b2011-02-21 13:21:03 +0100628 if (i >= mx)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700629 i = 0;
630 }
631
632 mdev->al_tr_number = to_tnr+1;
Lars Ellenberg7ad651b2011-02-21 13:21:03 +0100633 mdev->al_tr_pos = (to + 1) % (MD_AL_SECTORS*512/MD_BLOCK_SIZE);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700634
635 /* ok, we are done with it */
636 mutex_unlock(&mdev->md_io_mutex);
637
638 dev_info(DEV, "Found %d transactions (%d active extents) in activity log.\n",
639 transactions, active_extents);
640
641 return 1;
642}
643
Philipp Reisnerb411b362009-09-25 16:07:19 -0700644/**
Lars Ellenberg867f5742011-02-21 13:20:53 +0100645 * drbd_al_apply_to_bm() - Sets the bitmap to dirty(1) where covered by active AL extents
Philipp Reisnerb411b362009-09-25 16:07:19 -0700646 * @mdev: DRBD device.
647 */
648void drbd_al_apply_to_bm(struct drbd_conf *mdev)
649{
650 unsigned int enr;
651 unsigned long add = 0;
652 char ppb[10];
Lars Ellenberg6719fb02010-10-18 23:04:07 +0200653 int i, tmp;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700654
655 wait_event(mdev->al_wait, lc_try_lock(mdev->act_log));
656
657 for (i = 0; i < mdev->act_log->nr_elements; i++) {
658 enr = lc_element_by_index(mdev->act_log, i)->lc_number;
659 if (enr == LC_FREE)
660 continue;
Lars Ellenberg6719fb02010-10-18 23:04:07 +0200661 tmp = drbd_bm_ALe_set_all(mdev, enr);
662 dynamic_dev_dbg(DEV, "AL: set %d bits in extent %u\n", tmp, enr);
663 add += tmp;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700664 }
665
666 lc_unlock(mdev->act_log);
667 wake_up(&mdev->al_wait);
668
669 dev_info(DEV, "Marked additional %s as out-of-sync based on AL.\n",
670 ppsize(ppb, Bit2KB(add)));
671}
672
673static int _try_lc_del(struct drbd_conf *mdev, struct lc_element *al_ext)
674{
675 int rv;
676
677 spin_lock_irq(&mdev->al_lock);
678 rv = (al_ext->refcnt == 0);
679 if (likely(rv))
680 lc_del(mdev->act_log, al_ext);
681 spin_unlock_irq(&mdev->al_lock);
682
683 return rv;
684}
685
686/**
687 * drbd_al_shrink() - Removes all active extents form the activity log
688 * @mdev: DRBD device.
689 *
690 * Removes all active extents form the activity log, waiting until
691 * the reference count of each entry dropped to 0 first, of course.
692 *
693 * You need to lock mdev->act_log with lc_try_lock() / lc_unlock()
694 */
695void drbd_al_shrink(struct drbd_conf *mdev)
696{
697 struct lc_element *al_ext;
698 int i;
699
Lars Ellenberg46a15bc2011-02-21 13:21:01 +0100700 D_ASSERT(test_bit(__LC_LOCKED, &mdev->act_log->flags));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700701
702 for (i = 0; i < mdev->act_log->nr_elements; i++) {
703 al_ext = lc_element_by_index(mdev->act_log, i);
704 if (al_ext->lc_number == LC_FREE)
705 continue;
706 wait_event(mdev->al_wait, _try_lc_del(mdev, al_ext));
707 }
708
709 wake_up(&mdev->al_wait);
710}
711
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +0100712static int w_update_odbm(struct drbd_work *w, int unused)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700713{
714 struct update_odbm_work *udw = container_of(w, struct update_odbm_work, w);
Philipp Reisner00d56942011-02-09 18:09:48 +0100715 struct drbd_conf *mdev = w->mdev;
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100716 struct sib_info sib = { .sib_reason = SIB_SYNC_PROGRESS, };
Philipp Reisnerb411b362009-09-25 16:07:19 -0700717
718 if (!get_ldev(mdev)) {
719 if (__ratelimit(&drbd_ratelimit_state))
720 dev_warn(DEV, "Can not update on disk bitmap, local IO disabled.\n");
721 kfree(udw);
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +0100722 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700723 }
724
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100725 drbd_bm_write_page(mdev, rs_extent_to_bm_page(udw->enr));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700726 put_ldev(mdev);
727
728 kfree(udw);
729
730 if (drbd_bm_total_weight(mdev) <= mdev->rs_failed) {
731 switch (mdev->state.conn) {
732 case C_SYNC_SOURCE: case C_SYNC_TARGET:
733 case C_PAUSED_SYNC_S: case C_PAUSED_SYNC_T:
734 drbd_resync_finished(mdev);
735 default:
736 /* nothing to do */
737 break;
738 }
739 }
Lars Ellenberg3b98c0c2011-03-07 12:49:34 +0100740 drbd_bcast_event(mdev, &sib);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700741
Andreas Gruenbacher99920dc2011-03-16 15:31:39 +0100742 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700743}
744
745
746/* ATTENTION. The AL's extents are 4MB each, while the extents in the
747 * resync LRU-cache are 16MB each.
748 * The caller of this function has to hold an get_ldev() reference.
749 *
750 * TODO will be obsoleted once we have a caching lru of the on disk bitmap
751 */
752static void drbd_try_clear_on_disk_bm(struct drbd_conf *mdev, sector_t sector,
753 int count, int success)
754{
755 struct lc_element *e;
756 struct update_odbm_work *udw;
757
758 unsigned int enr;
759
760 D_ASSERT(atomic_read(&mdev->local_cnt));
761
762 /* I simply assume that a sector/size pair never crosses
763 * a 16 MB extent border. (Currently this is true...) */
764 enr = BM_SECT_TO_EXT(sector);
765
766 e = lc_get(mdev->resync, enr);
767 if (e) {
768 struct bm_extent *ext = lc_entry(e, struct bm_extent, lce);
769 if (ext->lce.lc_number == enr) {
770 if (success)
771 ext->rs_left -= count;
772 else
773 ext->rs_failed += count;
774 if (ext->rs_left < ext->rs_failed) {
775 dev_err(DEV, "BAD! sector=%llus enr=%u rs_left=%d "
776 "rs_failed=%d count=%d\n",
777 (unsigned long long)sector,
778 ext->lce.lc_number, ext->rs_left,
779 ext->rs_failed, count);
780 dump_stack();
781
782 lc_put(mdev->resync, &ext->lce);
Philipp Reisner38fa9982011-03-15 18:24:49 +0100783 conn_request_state(mdev->tconn, NS(conn, C_DISCONNECTING), CS_HARD);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700784 return;
785 }
786 } else {
787 /* Normally this element should be in the cache,
788 * since drbd_rs_begin_io() pulled it already in.
789 *
790 * But maybe an application write finished, and we set
791 * something outside the resync lru_cache in sync.
792 */
793 int rs_left = drbd_bm_e_weight(mdev, enr);
794 if (ext->flags != 0) {
795 dev_warn(DEV, "changing resync lce: %d[%u;%02lx]"
796 " -> %d[%u;00]\n",
797 ext->lce.lc_number, ext->rs_left,
798 ext->flags, enr, rs_left);
799 ext->flags = 0;
800 }
801 if (ext->rs_failed) {
802 dev_warn(DEV, "Kicking resync_lru element enr=%u "
803 "out with rs_failed=%d\n",
804 ext->lce.lc_number, ext->rs_failed);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700805 }
806 ext->rs_left = rs_left;
807 ext->rs_failed = success ? 0 : count;
Lars Ellenberg46a15bc2011-02-21 13:21:01 +0100808 /* we don't keep a persistent log of the resync lru,
809 * we can commit any change right away. */
810 lc_committed(mdev->resync);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700811 }
812 lc_put(mdev->resync, &ext->lce);
813 /* no race, we are within the al_lock! */
814
815 if (ext->rs_left == ext->rs_failed) {
816 ext->rs_failed = 0;
817
818 udw = kmalloc(sizeof(*udw), GFP_ATOMIC);
819 if (udw) {
820 udw->enr = ext->lce.lc_number;
821 udw->w.cb = w_update_odbm;
Philipp Reisnera21e9292011-02-08 15:08:49 +0100822 udw->w.mdev = mdev;
Philipp Reisnere42325a2011-01-19 13:55:45 +0100823 drbd_queue_work_front(&mdev->tconn->data.work, &udw->w);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700824 } else {
825 dev_warn(DEV, "Could not kmalloc an udw\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700826 }
827 }
828 } else {
829 dev_err(DEV, "lc_get() failed! locked=%d/%d flags=%lu\n",
830 mdev->resync_locked,
831 mdev->resync->nr_elements,
832 mdev->resync->flags);
833 }
834}
835
Lars Ellenbergc6ea14d2010-11-05 09:23:37 +0100836void drbd_advance_rs_marks(struct drbd_conf *mdev, unsigned long still_to_go)
837{
838 unsigned long now = jiffies;
839 unsigned long last = mdev->rs_mark_time[mdev->rs_last_mark];
840 int next = (mdev->rs_last_mark + 1) % DRBD_SYNC_MARKS;
841 if (time_after_eq(now, last + DRBD_SYNC_MARK_STEP)) {
842 if (mdev->rs_mark_left[mdev->rs_last_mark] != still_to_go &&
843 mdev->state.conn != C_PAUSED_SYNC_T &&
844 mdev->state.conn != C_PAUSED_SYNC_S) {
845 mdev->rs_mark_time[next] = now;
846 mdev->rs_mark_left[next] = still_to_go;
847 mdev->rs_last_mark = next;
848 }
849 }
850}
851
Philipp Reisnerb411b362009-09-25 16:07:19 -0700852/* clear the bit corresponding to the piece of storage in question:
853 * size byte of data starting from sector. Only clear a bits of the affected
854 * one ore more _aligned_ BM_BLOCK_SIZE blocks.
855 *
856 * called by worker on C_SYNC_TARGET and receiver on SyncSource.
857 *
858 */
859void __drbd_set_in_sync(struct drbd_conf *mdev, sector_t sector, int size,
860 const char *file, const unsigned int line)
861{
862 /* Is called from worker and receiver context _only_ */
863 unsigned long sbnr, ebnr, lbnr;
864 unsigned long count = 0;
865 sector_t esector, nr_sectors;
866 int wake_up = 0;
867 unsigned long flags;
868
Andreas Gruenbacherc670a392011-02-21 12:41:39 +0100869 if (size <= 0 || !IS_ALIGNED(size, 512) || size > DRBD_MAX_BIO_SIZE) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700870 dev_err(DEV, "drbd_set_in_sync: sector=%llus size=%d nonsense!\n",
871 (unsigned long long)sector, size);
872 return;
873 }
874 nr_sectors = drbd_get_capacity(mdev->this_bdev);
875 esector = sector + (size >> 9) - 1;
876
Andreas Gruenbacher841ce242010-12-15 19:31:20 +0100877 if (!expect(sector < nr_sectors))
878 return;
879 if (!expect(esector < nr_sectors))
880 esector = nr_sectors - 1;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700881
882 lbnr = BM_SECT_TO_BIT(nr_sectors-1);
883
884 /* we clear it (in sync).
885 * round up start sector, round down end sector. we make sure we only
886 * clear full, aligned, BM_BLOCK_SIZE (4K) blocks */
887 if (unlikely(esector < BM_SECT_PER_BIT-1))
888 return;
889 if (unlikely(esector == (nr_sectors-1)))
890 ebnr = lbnr;
891 else
892 ebnr = BM_SECT_TO_BIT(esector - (BM_SECT_PER_BIT-1));
893 sbnr = BM_SECT_TO_BIT(sector + BM_SECT_PER_BIT-1);
894
Philipp Reisnerb411b362009-09-25 16:07:19 -0700895 if (sbnr > ebnr)
896 return;
897
898 /*
899 * ok, (capacity & 7) != 0 sometimes, but who cares...
900 * we count rs_{total,left} in bits, not sectors.
901 */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700902 count = drbd_bm_clear_bits(mdev, sbnr, ebnr);
Lars Ellenberg1d7734a2010-08-11 21:21:50 +0200903 if (count && get_ldev(mdev)) {
Lars Ellenbergc6ea14d2010-11-05 09:23:37 +0100904 drbd_advance_rs_marks(mdev, drbd_bm_total_weight(mdev));
Lars Ellenberg1d7734a2010-08-11 21:21:50 +0200905 spin_lock_irqsave(&mdev->al_lock, flags);
Andreas Gruenbacher81e84652010-12-09 15:03:57 +0100906 drbd_try_clear_on_disk_bm(mdev, sector, count, true);
Lars Ellenberg1d7734a2010-08-11 21:21:50 +0200907 spin_unlock_irqrestore(&mdev->al_lock, flags);
908
Philipp Reisnerb411b362009-09-25 16:07:19 -0700909 /* just wake_up unconditional now, various lc_chaged(),
910 * lc_put() in drbd_try_clear_on_disk_bm(). */
911 wake_up = 1;
Lars Ellenberg1d7734a2010-08-11 21:21:50 +0200912 put_ldev(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700913 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700914 if (wake_up)
915 wake_up(&mdev->al_wait);
916}
917
918/*
919 * this is intended to set one request worth of data out of sync.
920 * affects at least 1 bit,
Lars Ellenberg1816a2b2010-11-11 15:19:07 +0100921 * and at most 1+DRBD_MAX_BIO_SIZE/BM_BLOCK_SIZE bits.
Philipp Reisnerb411b362009-09-25 16:07:19 -0700922 *
923 * called by tl_clear and drbd_send_dblock (==drbd_make_request).
924 * so this can be _any_ process.
925 */
Philipp Reisner73a01a12010-10-27 14:33:00 +0200926int __drbd_set_out_of_sync(struct drbd_conf *mdev, sector_t sector, int size,
Philipp Reisnerb411b362009-09-25 16:07:19 -0700927 const char *file, const unsigned int line)
928{
929 unsigned long sbnr, ebnr, lbnr, flags;
930 sector_t esector, nr_sectors;
Philipp Reisner73a01a12010-10-27 14:33:00 +0200931 unsigned int enr, count = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700932 struct lc_element *e;
933
Andreas Gruenbacherc670a392011-02-21 12:41:39 +0100934 if (size <= 0 || !IS_ALIGNED(size, 512) || size > DRBD_MAX_BIO_SIZE) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700935 dev_err(DEV, "sector: %llus, size: %d\n",
936 (unsigned long long)sector, size);
Philipp Reisner73a01a12010-10-27 14:33:00 +0200937 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700938 }
939
940 if (!get_ldev(mdev))
Philipp Reisner73a01a12010-10-27 14:33:00 +0200941 return 0; /* no disk, no metadata, no bitmap to set bits in */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700942
943 nr_sectors = drbd_get_capacity(mdev->this_bdev);
944 esector = sector + (size >> 9) - 1;
945
Andreas Gruenbacher841ce242010-12-15 19:31:20 +0100946 if (!expect(sector < nr_sectors))
Philipp Reisnerb411b362009-09-25 16:07:19 -0700947 goto out;
Andreas Gruenbacher841ce242010-12-15 19:31:20 +0100948 if (!expect(esector < nr_sectors))
949 esector = nr_sectors - 1;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700950
951 lbnr = BM_SECT_TO_BIT(nr_sectors-1);
952
953 /* we set it out of sync,
954 * we do not need to round anything here */
955 sbnr = BM_SECT_TO_BIT(sector);
956 ebnr = BM_SECT_TO_BIT(esector);
957
Philipp Reisnerb411b362009-09-25 16:07:19 -0700958 /* ok, (capacity & 7) != 0 sometimes, but who cares...
959 * we count rs_{total,left} in bits, not sectors. */
960 spin_lock_irqsave(&mdev->al_lock, flags);
961 count = drbd_bm_set_bits(mdev, sbnr, ebnr);
962
963 enr = BM_SECT_TO_EXT(sector);
964 e = lc_find(mdev->resync, enr);
965 if (e)
966 lc_entry(e, struct bm_extent, lce)->rs_left += count;
967 spin_unlock_irqrestore(&mdev->al_lock, flags);
968
969out:
970 put_ldev(mdev);
Philipp Reisner73a01a12010-10-27 14:33:00 +0200971
972 return count;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700973}
974
975static
976struct bm_extent *_bme_get(struct drbd_conf *mdev, unsigned int enr)
977{
978 struct lc_element *e;
979 struct bm_extent *bm_ext;
980 int wakeup = 0;
981 unsigned long rs_flags;
982
983 spin_lock_irq(&mdev->al_lock);
984 if (mdev->resync_locked > mdev->resync->nr_elements/2) {
985 spin_unlock_irq(&mdev->al_lock);
986 return NULL;
987 }
988 e = lc_get(mdev->resync, enr);
989 bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL;
990 if (bm_ext) {
991 if (bm_ext->lce.lc_number != enr) {
992 bm_ext->rs_left = drbd_bm_e_weight(mdev, enr);
993 bm_ext->rs_failed = 0;
Lars Ellenberg46a15bc2011-02-21 13:21:01 +0100994 lc_committed(mdev->resync);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700995 wakeup = 1;
996 }
997 if (bm_ext->lce.refcnt == 1)
998 mdev->resync_locked++;
999 set_bit(BME_NO_WRITES, &bm_ext->flags);
1000 }
1001 rs_flags = mdev->resync->flags;
1002 spin_unlock_irq(&mdev->al_lock);
1003 if (wakeup)
1004 wake_up(&mdev->al_wait);
1005
1006 if (!bm_ext) {
1007 if (rs_flags & LC_STARVING)
1008 dev_warn(DEV, "Have to wait for element"
1009 " (resync LRU too small?)\n");
Lars Ellenberg46a15bc2011-02-21 13:21:01 +01001010 BUG_ON(rs_flags & LC_LOCKED);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001011 }
1012
1013 return bm_ext;
1014}
1015
1016static int _is_in_al(struct drbd_conf *mdev, unsigned int enr)
1017{
Lars Ellenberg46a15bc2011-02-21 13:21:01 +01001018 int rv;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001019
1020 spin_lock_irq(&mdev->al_lock);
Lars Ellenberg46a15bc2011-02-21 13:21:01 +01001021 rv = lc_is_used(mdev->act_log, enr);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001022 spin_unlock_irq(&mdev->al_lock);
1023
Philipp Reisnerb411b362009-09-25 16:07:19 -07001024 return rv;
1025}
1026
1027/**
1028 * drbd_rs_begin_io() - Gets an extent in the resync LRU cache and sets it to BME_LOCKED
1029 * @mdev: DRBD device.
1030 * @sector: The sector number.
1031 *
Lars Ellenberg80a40e42010-08-11 23:28:00 +02001032 * This functions sleeps on al_wait. Returns 0 on success, -EINTR if interrupted.
Philipp Reisnerb411b362009-09-25 16:07:19 -07001033 */
1034int drbd_rs_begin_io(struct drbd_conf *mdev, sector_t sector)
1035{
1036 unsigned int enr = BM_SECT_TO_EXT(sector);
1037 struct bm_extent *bm_ext;
1038 int i, sig;
Philipp Reisnerf91ab622010-11-09 13:59:41 +01001039 int sa = 200; /* Step aside 200 times, then grab the extent and let app-IO wait.
1040 200 times -> 20 seconds. */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001041
Philipp Reisnerf91ab622010-11-09 13:59:41 +01001042retry:
Philipp Reisnerb411b362009-09-25 16:07:19 -07001043 sig = wait_event_interruptible(mdev->al_wait,
1044 (bm_ext = _bme_get(mdev, enr)));
1045 if (sig)
Lars Ellenberg80a40e42010-08-11 23:28:00 +02001046 return -EINTR;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001047
1048 if (test_bit(BME_LOCKED, &bm_ext->flags))
Lars Ellenberg80a40e42010-08-11 23:28:00 +02001049 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001050
1051 for (i = 0; i < AL_EXT_PER_BM_SECT; i++) {
1052 sig = wait_event_interruptible(mdev->al_wait,
Philipp Reisnerf91ab622010-11-09 13:59:41 +01001053 !_is_in_al(mdev, enr * AL_EXT_PER_BM_SECT + i) ||
Philipp Reisnerc507f462010-11-22 15:49:17 +01001054 test_bit(BME_PRIORITY, &bm_ext->flags));
Philipp Reisnerf91ab622010-11-09 13:59:41 +01001055
1056 if (sig || (test_bit(BME_PRIORITY, &bm_ext->flags) && sa)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001057 spin_lock_irq(&mdev->al_lock);
1058 if (lc_put(mdev->resync, &bm_ext->lce) == 0) {
Philipp Reisnerf91ab622010-11-09 13:59:41 +01001059 bm_ext->flags = 0; /* clears BME_NO_WRITES and eventually BME_PRIORITY */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001060 mdev->resync_locked--;
1061 wake_up(&mdev->al_wait);
1062 }
1063 spin_unlock_irq(&mdev->al_lock);
Philipp Reisnerf91ab622010-11-09 13:59:41 +01001064 if (sig)
1065 return -EINTR;
1066 if (schedule_timeout_interruptible(HZ/10))
1067 return -EINTR;
Philipp Reisnerc507f462010-11-22 15:49:17 +01001068 if (sa && --sa == 0)
1069 dev_warn(DEV,"drbd_rs_begin_io() stepped aside for 20sec."
1070 "Resync stalled?\n");
Philipp Reisnerf91ab622010-11-09 13:59:41 +01001071 goto retry;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001072 }
1073 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001074 set_bit(BME_LOCKED, &bm_ext->flags);
Lars Ellenberg80a40e42010-08-11 23:28:00 +02001075 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001076}
1077
1078/**
1079 * drbd_try_rs_begin_io() - Gets an extent in the resync LRU cache, does not sleep
1080 * @mdev: DRBD device.
1081 * @sector: The sector number.
1082 *
1083 * Gets an extent in the resync LRU cache, sets it to BME_NO_WRITES, then
1084 * tries to set it to BME_LOCKED. Returns 0 upon success, and -EAGAIN
1085 * if there is still application IO going on in this area.
1086 */
1087int drbd_try_rs_begin_io(struct drbd_conf *mdev, sector_t sector)
1088{
1089 unsigned int enr = BM_SECT_TO_EXT(sector);
1090 const unsigned int al_enr = enr*AL_EXT_PER_BM_SECT;
1091 struct lc_element *e;
1092 struct bm_extent *bm_ext;
1093 int i;
1094
Philipp Reisnerb411b362009-09-25 16:07:19 -07001095 spin_lock_irq(&mdev->al_lock);
1096 if (mdev->resync_wenr != LC_FREE && mdev->resync_wenr != enr) {
1097 /* in case you have very heavy scattered io, it may
1098 * stall the syncer undefined if we give up the ref count
1099 * when we try again and requeue.
1100 *
1101 * if we don't give up the refcount, but the next time
1102 * we are scheduled this extent has been "synced" by new
1103 * application writes, we'd miss the lc_put on the
1104 * extent we keep the refcount on.
1105 * so we remembered which extent we had to try again, and
1106 * if the next requested one is something else, we do
1107 * the lc_put here...
1108 * we also have to wake_up
1109 */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001110 e = lc_find(mdev->resync, mdev->resync_wenr);
1111 bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL;
1112 if (bm_ext) {
1113 D_ASSERT(!test_bit(BME_LOCKED, &bm_ext->flags));
1114 D_ASSERT(test_bit(BME_NO_WRITES, &bm_ext->flags));
1115 clear_bit(BME_NO_WRITES, &bm_ext->flags);
1116 mdev->resync_wenr = LC_FREE;
1117 if (lc_put(mdev->resync, &bm_ext->lce) == 0)
1118 mdev->resync_locked--;
1119 wake_up(&mdev->al_wait);
1120 } else {
1121 dev_alert(DEV, "LOGIC BUG\n");
1122 }
1123 }
1124 /* TRY. */
1125 e = lc_try_get(mdev->resync, enr);
1126 bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL;
1127 if (bm_ext) {
1128 if (test_bit(BME_LOCKED, &bm_ext->flags))
1129 goto proceed;
1130 if (!test_and_set_bit(BME_NO_WRITES, &bm_ext->flags)) {
1131 mdev->resync_locked++;
1132 } else {
1133 /* we did set the BME_NO_WRITES,
1134 * but then could not set BME_LOCKED,
1135 * so we tried again.
1136 * drop the extra reference. */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001137 bm_ext->lce.refcnt--;
1138 D_ASSERT(bm_ext->lce.refcnt > 0);
1139 }
1140 goto check_al;
1141 } else {
1142 /* do we rather want to try later? */
Jens Axboe6a0afdf2009-10-01 09:04:14 +02001143 if (mdev->resync_locked > mdev->resync->nr_elements-3)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001144 goto try_again;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001145 /* Do or do not. There is no try. -- Yoda */
1146 e = lc_get(mdev->resync, enr);
1147 bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL;
1148 if (!bm_ext) {
1149 const unsigned long rs_flags = mdev->resync->flags;
1150 if (rs_flags & LC_STARVING)
1151 dev_warn(DEV, "Have to wait for element"
1152 " (resync LRU too small?)\n");
Lars Ellenberg46a15bc2011-02-21 13:21:01 +01001153 BUG_ON(rs_flags & LC_LOCKED);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001154 goto try_again;
1155 }
1156 if (bm_ext->lce.lc_number != enr) {
1157 bm_ext->rs_left = drbd_bm_e_weight(mdev, enr);
1158 bm_ext->rs_failed = 0;
Lars Ellenberg46a15bc2011-02-21 13:21:01 +01001159 lc_committed(mdev->resync);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001160 wake_up(&mdev->al_wait);
1161 D_ASSERT(test_bit(BME_LOCKED, &bm_ext->flags) == 0);
1162 }
1163 set_bit(BME_NO_WRITES, &bm_ext->flags);
1164 D_ASSERT(bm_ext->lce.refcnt == 1);
1165 mdev->resync_locked++;
1166 goto check_al;
1167 }
1168check_al:
Philipp Reisnerb411b362009-09-25 16:07:19 -07001169 for (i = 0; i < AL_EXT_PER_BM_SECT; i++) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001170 if (lc_is_used(mdev->act_log, al_enr+i))
1171 goto try_again;
1172 }
1173 set_bit(BME_LOCKED, &bm_ext->flags);
1174proceed:
1175 mdev->resync_wenr = LC_FREE;
1176 spin_unlock_irq(&mdev->al_lock);
1177 return 0;
1178
1179try_again:
Philipp Reisnerb411b362009-09-25 16:07:19 -07001180 if (bm_ext)
1181 mdev->resync_wenr = enr;
1182 spin_unlock_irq(&mdev->al_lock);
1183 return -EAGAIN;
1184}
1185
1186void drbd_rs_complete_io(struct drbd_conf *mdev, sector_t sector)
1187{
1188 unsigned int enr = BM_SECT_TO_EXT(sector);
1189 struct lc_element *e;
1190 struct bm_extent *bm_ext;
1191 unsigned long flags;
1192
Philipp Reisnerb411b362009-09-25 16:07:19 -07001193 spin_lock_irqsave(&mdev->al_lock, flags);
1194 e = lc_find(mdev->resync, enr);
1195 bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL;
1196 if (!bm_ext) {
1197 spin_unlock_irqrestore(&mdev->al_lock, flags);
1198 if (__ratelimit(&drbd_ratelimit_state))
1199 dev_err(DEV, "drbd_rs_complete_io() called, but extent not found\n");
1200 return;
1201 }
1202
1203 if (bm_ext->lce.refcnt == 0) {
1204 spin_unlock_irqrestore(&mdev->al_lock, flags);
1205 dev_err(DEV, "drbd_rs_complete_io(,%llu [=%u]) called, "
1206 "but refcnt is 0!?\n",
1207 (unsigned long long)sector, enr);
1208 return;
1209 }
1210
1211 if (lc_put(mdev->resync, &bm_ext->lce) == 0) {
Philipp Reisnere3555d82010-11-07 15:56:29 +01001212 bm_ext->flags = 0; /* clear BME_LOCKED, BME_NO_WRITES and BME_PRIORITY */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001213 mdev->resync_locked--;
1214 wake_up(&mdev->al_wait);
1215 }
1216
1217 spin_unlock_irqrestore(&mdev->al_lock, flags);
1218}
1219
1220/**
1221 * drbd_rs_cancel_all() - Removes all extents from the resync LRU (even BME_LOCKED)
1222 * @mdev: DRBD device.
1223 */
1224void drbd_rs_cancel_all(struct drbd_conf *mdev)
1225{
Philipp Reisnerb411b362009-09-25 16:07:19 -07001226 spin_lock_irq(&mdev->al_lock);
1227
1228 if (get_ldev_if_state(mdev, D_FAILED)) { /* Makes sure ->resync is there. */
1229 lc_reset(mdev->resync);
1230 put_ldev(mdev);
1231 }
1232 mdev->resync_locked = 0;
1233 mdev->resync_wenr = LC_FREE;
1234 spin_unlock_irq(&mdev->al_lock);
1235 wake_up(&mdev->al_wait);
1236}
1237
1238/**
1239 * drbd_rs_del_all() - Gracefully remove all extents from the resync LRU
1240 * @mdev: DRBD device.
1241 *
1242 * Returns 0 upon success, -EAGAIN if at least one reference count was
1243 * not zero.
1244 */
1245int drbd_rs_del_all(struct drbd_conf *mdev)
1246{
1247 struct lc_element *e;
1248 struct bm_extent *bm_ext;
1249 int i;
1250
Philipp Reisnerb411b362009-09-25 16:07:19 -07001251 spin_lock_irq(&mdev->al_lock);
1252
1253 if (get_ldev_if_state(mdev, D_FAILED)) {
1254 /* ok, ->resync is there. */
1255 for (i = 0; i < mdev->resync->nr_elements; i++) {
1256 e = lc_element_by_index(mdev->resync, i);
Philipp Reisnerb2b163d2010-04-02 08:40:33 +02001257 bm_ext = lc_entry(e, struct bm_extent, lce);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001258 if (bm_ext->lce.lc_number == LC_FREE)
1259 continue;
1260 if (bm_ext->lce.lc_number == mdev->resync_wenr) {
1261 dev_info(DEV, "dropping %u in drbd_rs_del_all, apparently"
1262 " got 'synced' by application io\n",
1263 mdev->resync_wenr);
1264 D_ASSERT(!test_bit(BME_LOCKED, &bm_ext->flags));
1265 D_ASSERT(test_bit(BME_NO_WRITES, &bm_ext->flags));
1266 clear_bit(BME_NO_WRITES, &bm_ext->flags);
1267 mdev->resync_wenr = LC_FREE;
1268 lc_put(mdev->resync, &bm_ext->lce);
1269 }
1270 if (bm_ext->lce.refcnt != 0) {
1271 dev_info(DEV, "Retrying drbd_rs_del_all() later. "
1272 "refcnt=%d\n", bm_ext->lce.refcnt);
1273 put_ldev(mdev);
1274 spin_unlock_irq(&mdev->al_lock);
1275 return -EAGAIN;
1276 }
1277 D_ASSERT(!test_bit(BME_LOCKED, &bm_ext->flags));
1278 D_ASSERT(!test_bit(BME_NO_WRITES, &bm_ext->flags));
1279 lc_del(mdev->resync, &bm_ext->lce);
1280 }
1281 D_ASSERT(mdev->resync->used == 0);
1282 put_ldev(mdev);
1283 }
1284 spin_unlock_irq(&mdev->al_lock);
1285
1286 return 0;
1287}
1288
1289/**
1290 * drbd_rs_failed_io() - Record information on a failure to resync the specified blocks
1291 * @mdev: DRBD device.
1292 * @sector: The sector number.
1293 * @size: Size of failed IO operation, in byte.
1294 */
1295void drbd_rs_failed_io(struct drbd_conf *mdev, sector_t sector, int size)
1296{
1297 /* Is called from worker and receiver context _only_ */
1298 unsigned long sbnr, ebnr, lbnr;
1299 unsigned long count;
1300 sector_t esector, nr_sectors;
1301 int wake_up = 0;
1302
Andreas Gruenbacherc670a392011-02-21 12:41:39 +01001303 if (size <= 0 || !IS_ALIGNED(size, 512) || size > DRBD_MAX_BIO_SIZE) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001304 dev_err(DEV, "drbd_rs_failed_io: sector=%llus size=%d nonsense!\n",
1305 (unsigned long long)sector, size);
1306 return;
1307 }
1308 nr_sectors = drbd_get_capacity(mdev->this_bdev);
1309 esector = sector + (size >> 9) - 1;
1310
Andreas Gruenbacher841ce242010-12-15 19:31:20 +01001311 if (!expect(sector < nr_sectors))
1312 return;
1313 if (!expect(esector < nr_sectors))
1314 esector = nr_sectors - 1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001315
1316 lbnr = BM_SECT_TO_BIT(nr_sectors-1);
1317
1318 /*
1319 * round up start sector, round down end sector. we make sure we only
1320 * handle full, aligned, BM_BLOCK_SIZE (4K) blocks */
1321 if (unlikely(esector < BM_SECT_PER_BIT-1))
1322 return;
1323 if (unlikely(esector == (nr_sectors-1)))
1324 ebnr = lbnr;
1325 else
1326 ebnr = BM_SECT_TO_BIT(esector - (BM_SECT_PER_BIT-1));
1327 sbnr = BM_SECT_TO_BIT(sector + BM_SECT_PER_BIT-1);
1328
1329 if (sbnr > ebnr)
1330 return;
1331
1332 /*
1333 * ok, (capacity & 7) != 0 sometimes, but who cares...
1334 * we count rs_{total,left} in bits, not sectors.
1335 */
1336 spin_lock_irq(&mdev->al_lock);
1337 count = drbd_bm_count_bits(mdev, sbnr, ebnr);
1338 if (count) {
1339 mdev->rs_failed += count;
1340
1341 if (get_ldev(mdev)) {
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01001342 drbd_try_clear_on_disk_bm(mdev, sector, count, false);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001343 put_ldev(mdev);
1344 }
1345
1346 /* just wake_up unconditional now, various lc_chaged(),
1347 * lc_put() in drbd_try_clear_on_disk_bm(). */
1348 wake_up = 1;
1349 }
1350 spin_unlock_irq(&mdev->al_lock);
1351 if (wake_up)
1352 wake_up(&mdev->al_wait);
1353}