xref: /openbmc/linux/drivers/block/drbd/drbd_actlog.c (revision 7ad651b52218eea3f9280dbb353dfe0c42742d85)
1b411b363SPhilipp Reisner /*
2b411b363SPhilipp Reisner    drbd_actlog.c
3b411b363SPhilipp Reisner 
4b411b363SPhilipp Reisner    This file is part of DRBD by Philipp Reisner and Lars Ellenberg.
5b411b363SPhilipp Reisner 
6b411b363SPhilipp Reisner    Copyright (C) 2003-2008, LINBIT Information Technologies GmbH.
7b411b363SPhilipp Reisner    Copyright (C) 2003-2008, Philipp Reisner <philipp.reisner@linbit.com>.
8b411b363SPhilipp Reisner    Copyright (C) 2003-2008, Lars Ellenberg <lars.ellenberg@linbit.com>.
9b411b363SPhilipp Reisner 
10b411b363SPhilipp Reisner    drbd is free software; you can redistribute it and/or modify
11b411b363SPhilipp Reisner    it under the terms of the GNU General Public License as published by
12b411b363SPhilipp Reisner    the Free Software Foundation; either version 2, or (at your option)
13b411b363SPhilipp Reisner    any later version.
14b411b363SPhilipp Reisner 
15b411b363SPhilipp Reisner    drbd is distributed in the hope that it will be useful,
16b411b363SPhilipp Reisner    but WITHOUT ANY WARRANTY; without even the implied warranty of
17b411b363SPhilipp Reisner    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18b411b363SPhilipp Reisner    GNU General Public License for more details.
19b411b363SPhilipp Reisner 
20b411b363SPhilipp Reisner    You should have received a copy of the GNU General Public License
21b411b363SPhilipp Reisner    along with drbd; see the file COPYING.  If not, write to
22b411b363SPhilipp Reisner    the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23b411b363SPhilipp Reisner 
24b411b363SPhilipp Reisner  */
25b411b363SPhilipp Reisner 
26b411b363SPhilipp Reisner #include <linux/slab.h>
27*7ad651b5SLars Ellenberg #include <linux/crc32c.h>
28b411b363SPhilipp Reisner #include <linux/drbd.h>
29*7ad651b5SLars Ellenberg #include <linux/drbd_limits.h>
30*7ad651b5SLars Ellenberg #include <linux/dynamic_debug.h>
31b411b363SPhilipp Reisner #include "drbd_int.h"
32b411b363SPhilipp Reisner #include "drbd_wrappers.h"
33b411b363SPhilipp Reisner 
34*7ad651b5SLars Ellenberg /* all fields on disc in big endian */
35*7ad651b5SLars Ellenberg struct __packed al_transaction_on_disk {
36*7ad651b5SLars Ellenberg 	/* don't we all like magic */
37*7ad651b5SLars Ellenberg 	__be32	magic;
38*7ad651b5SLars Ellenberg 
39*7ad651b5SLars Ellenberg 	/* to identify the most recent transaction block
40*7ad651b5SLars Ellenberg 	 * in the on disk ring buffer */
41*7ad651b5SLars Ellenberg 	__be32	tr_number;
42*7ad651b5SLars Ellenberg 
43*7ad651b5SLars Ellenberg 	/* checksum on the full 4k block, with this field set to 0. */
44*7ad651b5SLars Ellenberg 	__be32	crc32c;
45*7ad651b5SLars Ellenberg 
46*7ad651b5SLars Ellenberg 	/* type of transaction, special transaction types like:
47*7ad651b5SLars Ellenberg 	 * purge-all, set-all-idle, set-all-active, ... to-be-defined */
48*7ad651b5SLars Ellenberg 	__be16	transaction_type;
49*7ad651b5SLars Ellenberg 
50*7ad651b5SLars Ellenberg 	/* we currently allow only a few thousand extents,
51*7ad651b5SLars Ellenberg 	 * so 16bit will be enough for the slot number. */
52*7ad651b5SLars Ellenberg 
53*7ad651b5SLars Ellenberg 	/* how many updates in this transaction */
54*7ad651b5SLars Ellenberg 	__be16	n_updates;
55*7ad651b5SLars Ellenberg 
56*7ad651b5SLars Ellenberg 	/* maximum slot number, "al-extents" in drbd.conf speak.
57*7ad651b5SLars Ellenberg 	 * Having this in each transaction should make reconfiguration
58*7ad651b5SLars Ellenberg 	 * of that parameter easier. */
59*7ad651b5SLars Ellenberg 	__be16	context_size;
60*7ad651b5SLars Ellenberg 
61*7ad651b5SLars Ellenberg 	/* slot number the context starts with */
62*7ad651b5SLars Ellenberg 	__be16	context_start_slot_nr;
63*7ad651b5SLars Ellenberg 
64*7ad651b5SLars Ellenberg 	/* Some reserved bytes.  Expected usage is a 64bit counter of
65*7ad651b5SLars Ellenberg 	 * sectors-written since device creation, and other data generation tag
66*7ad651b5SLars Ellenberg 	 * supporting usage */
67*7ad651b5SLars Ellenberg 	__be32	__reserved[4];
68*7ad651b5SLars Ellenberg 
69*7ad651b5SLars Ellenberg 	/* --- 36 byte used --- */
70*7ad651b5SLars Ellenberg 
71*7ad651b5SLars Ellenberg 	/* Reserve space for up to AL_UPDATES_PER_TRANSACTION changes
72*7ad651b5SLars Ellenberg 	 * in one transaction, then use the remaining byte in the 4k block for
73*7ad651b5SLars Ellenberg 	 * context information.  "Flexible" number of updates per transaction
74*7ad651b5SLars Ellenberg 	 * does not help, as we have to account for the case when all update
75*7ad651b5SLars Ellenberg 	 * slots are used anyways, so it would only complicate code without
76*7ad651b5SLars Ellenberg 	 * additional benefit.
77b411b363SPhilipp Reisner 	 */
78*7ad651b5SLars Ellenberg 	__be16	update_slot_nr[AL_UPDATES_PER_TRANSACTION];
79*7ad651b5SLars Ellenberg 
80*7ad651b5SLars Ellenberg 	/* but the extent number is 32bit, which at an extent size of 4 MiB
81*7ad651b5SLars Ellenberg 	 * allows to cover device sizes of up to 2**54 Byte (16 PiB) */
82*7ad651b5SLars Ellenberg 	__be32	update_extent_nr[AL_UPDATES_PER_TRANSACTION];
83*7ad651b5SLars Ellenberg 
84*7ad651b5SLars Ellenberg 	/* --- 420 bytes used (36 + 64*6) --- */
85*7ad651b5SLars Ellenberg 
86*7ad651b5SLars Ellenberg 	/* 4096 - 420 = 3676 = 919 * 4 */
87*7ad651b5SLars Ellenberg 	__be32	context[AL_CONTEXT_PER_TRANSACTION];
88b411b363SPhilipp Reisner };
89b411b363SPhilipp Reisner 
90b411b363SPhilipp Reisner struct update_odbm_work {
91b411b363SPhilipp Reisner 	struct drbd_work w;
92b411b363SPhilipp Reisner 	unsigned int enr;
93b411b363SPhilipp Reisner };
94b411b363SPhilipp Reisner 
95b411b363SPhilipp Reisner struct update_al_work {
96b411b363SPhilipp Reisner 	struct drbd_work w;
97b411b363SPhilipp Reisner 	struct completion event;
98*7ad651b5SLars Ellenberg 	int err;
99b411b363SPhilipp Reisner };
100b411b363SPhilipp Reisner 
101b411b363SPhilipp Reisner struct drbd_atodb_wait {
102b411b363SPhilipp Reisner 	atomic_t           count;
103b411b363SPhilipp Reisner 	struct completion  io_done;
104b411b363SPhilipp Reisner 	struct drbd_conf   *mdev;
105b411b363SPhilipp Reisner 	int                error;
106b411b363SPhilipp Reisner };
107b411b363SPhilipp Reisner 
108b411b363SPhilipp Reisner 
10900d56944SPhilipp Reisner int w_al_write_transaction(struct drbd_work *, int);
110b411b363SPhilipp Reisner 
111b411b363SPhilipp Reisner static int _drbd_md_sync_page_io(struct drbd_conf *mdev,
112b411b363SPhilipp Reisner 				 struct drbd_backing_dev *bdev,
113b411b363SPhilipp Reisner 				 struct page *page, sector_t sector,
114b411b363SPhilipp Reisner 				 int rw, int size)
115b411b363SPhilipp Reisner {
116b411b363SPhilipp Reisner 	struct bio *bio;
117b411b363SPhilipp Reisner 	struct drbd_md_io md_io;
118b411b363SPhilipp Reisner 	int ok;
119b411b363SPhilipp Reisner 
120b411b363SPhilipp Reisner 	md_io.mdev = mdev;
121b411b363SPhilipp Reisner 	init_completion(&md_io.event);
122b411b363SPhilipp Reisner 	md_io.error = 0;
123b411b363SPhilipp Reisner 
124a8a4e51eSPhilipp Reisner 	if ((rw & WRITE) && !test_bit(MD_NO_FUA, &mdev->flags))
12586e1e98eSLars Ellenberg 		rw |= REQ_FUA | REQ_FLUSH;
126721a9602SJens Axboe 	rw |= REQ_SYNC;
127b411b363SPhilipp Reisner 
128b411b363SPhilipp Reisner 	bio = bio_alloc(GFP_NOIO, 1);
129b411b363SPhilipp Reisner 	bio->bi_bdev = bdev->md_bdev;
130b411b363SPhilipp Reisner 	bio->bi_sector = sector;
131b411b363SPhilipp Reisner 	ok = (bio_add_page(bio, page, size, 0) == size);
132b411b363SPhilipp Reisner 	if (!ok)
133b411b363SPhilipp Reisner 		goto out;
134b411b363SPhilipp Reisner 	bio->bi_private = &md_io;
135b411b363SPhilipp Reisner 	bio->bi_end_io = drbd_md_io_complete;
136b411b363SPhilipp Reisner 	bio->bi_rw = rw;
137b411b363SPhilipp Reisner 
1380cf9d27eSAndreas Gruenbacher 	if (drbd_insert_fault(mdev, (rw & WRITE) ? DRBD_FAULT_MD_WR : DRBD_FAULT_MD_RD))
139b411b363SPhilipp Reisner 		bio_endio(bio, -EIO);
140b411b363SPhilipp Reisner 	else
141b411b363SPhilipp Reisner 		submit_bio(rw, bio);
142b411b363SPhilipp Reisner 	wait_for_completion(&md_io.event);
143b411b363SPhilipp Reisner 	ok = bio_flagged(bio, BIO_UPTODATE) && md_io.error == 0;
144b411b363SPhilipp Reisner 
145b411b363SPhilipp Reisner  out:
146b411b363SPhilipp Reisner 	bio_put(bio);
147b411b363SPhilipp Reisner 	return ok;
148b411b363SPhilipp Reisner }
149b411b363SPhilipp Reisner 
150b411b363SPhilipp Reisner int drbd_md_sync_page_io(struct drbd_conf *mdev, struct drbd_backing_dev *bdev,
151b411b363SPhilipp Reisner 			 sector_t sector, int rw)
152b411b363SPhilipp Reisner {
153*7ad651b5SLars Ellenberg 	int ok;
154b411b363SPhilipp Reisner 	struct page *iop = mdev->md_io_page;
155b411b363SPhilipp Reisner 
156b411b363SPhilipp Reisner 	D_ASSERT(mutex_is_locked(&mdev->md_io_mutex));
157b411b363SPhilipp Reisner 
158b411b363SPhilipp Reisner 	BUG_ON(!bdev->md_bdev);
159b411b363SPhilipp Reisner 
160*7ad651b5SLars Ellenberg 	dev_dbg(DEV, "meta_data io: %s [%d]:%s(,%llus,%s)\n",
161*7ad651b5SLars Ellenberg 	     current->comm, current->pid, __func__,
162*7ad651b5SLars Ellenberg 	     (unsigned long long)sector, (rw & WRITE) ? "WRITE" : "READ");
163b411b363SPhilipp Reisner 
164b411b363SPhilipp Reisner 	if (sector < drbd_md_first_sector(bdev) ||
165*7ad651b5SLars Ellenberg 	    sector + 7 > drbd_md_last_sector(bdev))
166b411b363SPhilipp Reisner 		dev_alert(DEV, "%s [%d]:%s(,%llus,%s) out of range md access!\n",
167b411b363SPhilipp Reisner 		     current->comm, current->pid, __func__,
168b411b363SPhilipp Reisner 		     (unsigned long long)sector, (rw & WRITE) ? "WRITE" : "READ");
169b411b363SPhilipp Reisner 
170*7ad651b5SLars Ellenberg 	ok = _drbd_md_sync_page_io(mdev, bdev, iop, sector, rw, MD_BLOCK_SIZE);
171b411b363SPhilipp Reisner 	if (unlikely(!ok)) {
172b411b363SPhilipp Reisner 		dev_err(DEV, "drbd_md_sync_page_io(,%llus,%s) failed!\n",
173b411b363SPhilipp Reisner 		    (unsigned long long)sector, (rw & WRITE) ? "WRITE" : "READ");
174b411b363SPhilipp Reisner 		return 0;
175b411b363SPhilipp Reisner 	}
176b411b363SPhilipp Reisner 
177b411b363SPhilipp Reisner 	return ok;
178b411b363SPhilipp Reisner }
179b411b363SPhilipp Reisner 
180b411b363SPhilipp Reisner static struct lc_element *_al_get(struct drbd_conf *mdev, unsigned int enr)
181b411b363SPhilipp Reisner {
182b411b363SPhilipp Reisner 	struct lc_element *al_ext;
183b411b363SPhilipp Reisner 	struct lc_element *tmp;
184f91ab628SPhilipp Reisner 	int wake;
185b411b363SPhilipp Reisner 
186b411b363SPhilipp Reisner 	spin_lock_irq(&mdev->al_lock);
187b411b363SPhilipp Reisner 	tmp = lc_find(mdev->resync, enr/AL_EXT_PER_BM_SECT);
188b411b363SPhilipp Reisner 	if (unlikely(tmp != NULL)) {
189b411b363SPhilipp Reisner 		struct bm_extent  *bm_ext = lc_entry(tmp, struct bm_extent, lce);
190b411b363SPhilipp Reisner 		if (test_bit(BME_NO_WRITES, &bm_ext->flags)) {
191f91ab628SPhilipp Reisner 			wake = !test_and_set_bit(BME_PRIORITY, &bm_ext->flags);
192b411b363SPhilipp Reisner 			spin_unlock_irq(&mdev->al_lock);
193f91ab628SPhilipp Reisner 			if (wake)
194f91ab628SPhilipp Reisner 				wake_up(&mdev->al_wait);
195b411b363SPhilipp Reisner 			return NULL;
196b411b363SPhilipp Reisner 		}
197b411b363SPhilipp Reisner 	}
198b411b363SPhilipp Reisner 	al_ext = lc_get(mdev->act_log, enr);
199b411b363SPhilipp Reisner 	spin_unlock_irq(&mdev->al_lock);
200b411b363SPhilipp Reisner 	return al_ext;
201b411b363SPhilipp Reisner }
202b411b363SPhilipp Reisner 
203b411b363SPhilipp Reisner void drbd_al_begin_io(struct drbd_conf *mdev, sector_t sector)
204b411b363SPhilipp Reisner {
205b411b363SPhilipp Reisner 	unsigned int enr = (sector >> (AL_EXTENT_SHIFT-9));
206b411b363SPhilipp Reisner 	struct lc_element *al_ext;
207b411b363SPhilipp Reisner 	struct update_al_work al_work;
208b411b363SPhilipp Reisner 
209b411b363SPhilipp Reisner 	D_ASSERT(atomic_read(&mdev->local_cnt) > 0);
210b411b363SPhilipp Reisner 
211b411b363SPhilipp Reisner 	wait_event(mdev->al_wait, (al_ext = _al_get(mdev, enr)));
212b411b363SPhilipp Reisner 
213b411b363SPhilipp Reisner 	if (al_ext->lc_number != enr) {
214b411b363SPhilipp Reisner 		/* drbd_al_write_transaction(mdev,al_ext,enr);
215b411b363SPhilipp Reisner 		 * recurses into generic_make_request(), which
216b411b363SPhilipp Reisner 		 * disallows recursion, bios being serialized on the
217b411b363SPhilipp Reisner 		 * current->bio_tail list now.
218b411b363SPhilipp Reisner 		 * we have to delegate updates to the activity log
219b411b363SPhilipp Reisner 		 * to the worker thread. */
220*7ad651b5SLars Ellenberg 
221*7ad651b5SLars Ellenberg 		/* Serialize multiple transactions.
222*7ad651b5SLars Ellenberg 		 * This uses test_and_set_bit, memory barrier is implicit.
223*7ad651b5SLars Ellenberg 		 * Optimization potential:
224*7ad651b5SLars Ellenberg 		 * first check for transaction number > old transaction number,
225*7ad651b5SLars Ellenberg 		 * so not all waiters have to lock/unlock.  */
226*7ad651b5SLars Ellenberg 		wait_event(mdev->al_wait, lc_try_lock_for_transaction(mdev->act_log));
227*7ad651b5SLars Ellenberg 
228*7ad651b5SLars Ellenberg 		/* Double check: it may have been committed by someone else,
229*7ad651b5SLars Ellenberg 		 * while we have been waiting for the lock. */
230*7ad651b5SLars Ellenberg 		if (al_ext->lc_number != enr) {
231b411b363SPhilipp Reisner 			init_completion(&al_work.event);
232b411b363SPhilipp Reisner 			al_work.w.cb = w_al_write_transaction;
233a21e9298SPhilipp Reisner 			al_work.w.mdev = mdev;
234e42325a5SPhilipp Reisner 			drbd_queue_work_front(&mdev->tconn->data.work, &al_work.w);
235b411b363SPhilipp Reisner 			wait_for_completion(&al_work.event);
236b411b363SPhilipp Reisner 
237b411b363SPhilipp Reisner 			mdev->al_writ_cnt++;
238b411b363SPhilipp Reisner 
239b411b363SPhilipp Reisner 			spin_lock_irq(&mdev->al_lock);
240*7ad651b5SLars Ellenberg 			/* FIXME
241*7ad651b5SLars Ellenberg 			if (al_work.err)
242*7ad651b5SLars Ellenberg 				we need an "lc_cancel" here;
243*7ad651b5SLars Ellenberg 			*/
24446a15bc3SLars Ellenberg 			lc_committed(mdev->act_log);
245b411b363SPhilipp Reisner 			spin_unlock_irq(&mdev->al_lock);
246*7ad651b5SLars Ellenberg 		}
247*7ad651b5SLars Ellenberg 		lc_unlock(mdev->act_log);
248b411b363SPhilipp Reisner 		wake_up(&mdev->al_wait);
249b411b363SPhilipp Reisner 	}
250b411b363SPhilipp Reisner }
251b411b363SPhilipp Reisner 
252b411b363SPhilipp Reisner void drbd_al_complete_io(struct drbd_conf *mdev, sector_t sector)
253b411b363SPhilipp Reisner {
254b411b363SPhilipp Reisner 	unsigned int enr = (sector >> (AL_EXTENT_SHIFT-9));
255b411b363SPhilipp Reisner 	struct lc_element *extent;
256b411b363SPhilipp Reisner 	unsigned long flags;
257b411b363SPhilipp Reisner 
258b411b363SPhilipp Reisner 	spin_lock_irqsave(&mdev->al_lock, flags);
259b411b363SPhilipp Reisner 
260b411b363SPhilipp Reisner 	extent = lc_find(mdev->act_log, enr);
261b411b363SPhilipp Reisner 
262b411b363SPhilipp Reisner 	if (!extent) {
263b411b363SPhilipp Reisner 		spin_unlock_irqrestore(&mdev->al_lock, flags);
264b411b363SPhilipp Reisner 		dev_err(DEV, "al_complete_io() called on inactive extent %u\n", enr);
265b411b363SPhilipp Reisner 		return;
266b411b363SPhilipp Reisner 	}
267b411b363SPhilipp Reisner 
268b411b363SPhilipp Reisner 	if (lc_put(mdev->act_log, extent) == 0)
269b411b363SPhilipp Reisner 		wake_up(&mdev->al_wait);
270b411b363SPhilipp Reisner 
271b411b363SPhilipp Reisner 	spin_unlock_irqrestore(&mdev->al_lock, flags);
272b411b363SPhilipp Reisner }
273b411b363SPhilipp Reisner 
27419f843aaSLars Ellenberg #if (PAGE_SHIFT + 3) < (AL_EXTENT_SHIFT - BM_BLOCK_SHIFT)
27519f843aaSLars Ellenberg /* Currently BM_BLOCK_SHIFT, BM_EXT_SHIFT and AL_EXTENT_SHIFT
27619f843aaSLars Ellenberg  * are still coupled, or assume too much about their relation.
27719f843aaSLars Ellenberg  * Code below will not work if this is violated.
27819f843aaSLars Ellenberg  * Will be cleaned up with some followup patch.
27919f843aaSLars Ellenberg  */
28019f843aaSLars Ellenberg # error FIXME
28119f843aaSLars Ellenberg #endif
28219f843aaSLars Ellenberg 
28319f843aaSLars Ellenberg static unsigned int al_extent_to_bm_page(unsigned int al_enr)
28419f843aaSLars Ellenberg {
28519f843aaSLars Ellenberg 	return al_enr >>
28619f843aaSLars Ellenberg 		/* bit to page */
28719f843aaSLars Ellenberg 		((PAGE_SHIFT + 3) -
28819f843aaSLars Ellenberg 		/* al extent number to bit */
28919f843aaSLars Ellenberg 		 (AL_EXTENT_SHIFT - BM_BLOCK_SHIFT));
29019f843aaSLars Ellenberg }
29119f843aaSLars Ellenberg 
29219f843aaSLars Ellenberg static unsigned int rs_extent_to_bm_page(unsigned int rs_enr)
29319f843aaSLars Ellenberg {
29419f843aaSLars Ellenberg 	return rs_enr >>
29519f843aaSLars Ellenberg 		/* bit to page */
29619f843aaSLars Ellenberg 		((PAGE_SHIFT + 3) -
29719f843aaSLars Ellenberg 		/* al extent number to bit */
29819f843aaSLars Ellenberg 		 (BM_EXT_SHIFT - BM_BLOCK_SHIFT));
29919f843aaSLars Ellenberg }
30019f843aaSLars Ellenberg 
301b411b363SPhilipp Reisner int
30200d56944SPhilipp Reisner w_al_write_transaction(struct drbd_work *w, int unused)
303b411b363SPhilipp Reisner {
304b411b363SPhilipp Reisner 	struct update_al_work *aw = container_of(w, struct update_al_work, w);
30500d56944SPhilipp Reisner 	struct drbd_conf *mdev = w->mdev;
306*7ad651b5SLars Ellenberg 	struct al_transaction_on_disk *buffer;
307*7ad651b5SLars Ellenberg 	struct lc_element *e;
308b411b363SPhilipp Reisner 	sector_t sector;
309*7ad651b5SLars Ellenberg 	int i, mx;
310*7ad651b5SLars Ellenberg 	unsigned extent_nr;
311*7ad651b5SLars Ellenberg 	unsigned crc = 0;
312b411b363SPhilipp Reisner 
313b411b363SPhilipp Reisner 	if (!get_ldev(mdev)) {
314*7ad651b5SLars Ellenberg 		dev_err(DEV, "disk is %s, cannot start al transaction\n",
315*7ad651b5SLars Ellenberg 			drbd_disk_str(mdev->state.disk));
316*7ad651b5SLars Ellenberg 		aw->err = -EIO;
317b411b363SPhilipp Reisner 		complete(&((struct update_al_work *)w)->event);
318b411b363SPhilipp Reisner 		return 1;
319b411b363SPhilipp Reisner 	}
320b411b363SPhilipp Reisner 
3216719fb03SLars Ellenberg 	/* The bitmap write may have failed, causing a state change. */
3226719fb03SLars Ellenberg 	if (mdev->state.disk < D_INCONSISTENT) {
3236719fb03SLars Ellenberg 		dev_err(DEV,
324*7ad651b5SLars Ellenberg 			"disk is %s, cannot write al transaction\n",
325*7ad651b5SLars Ellenberg 			drbd_disk_str(mdev->state.disk));
326*7ad651b5SLars Ellenberg 		aw->err = -EIO;
3276719fb03SLars Ellenberg 		complete(&((struct update_al_work *)w)->event);
3286719fb03SLars Ellenberg 		put_ldev(mdev);
3296719fb03SLars Ellenberg 		return 1;
3306719fb03SLars Ellenberg 	}
3316719fb03SLars Ellenberg 
3326719fb03SLars Ellenberg 	mutex_lock(&mdev->md_io_mutex); /* protects md_io_buffer, al_tr_cycle, ... */
333*7ad651b5SLars Ellenberg 	buffer = page_address(mdev->md_io_page);
334b411b363SPhilipp Reisner 
335*7ad651b5SLars Ellenberg 	memset(buffer, 0, sizeof(*buffer));
336*7ad651b5SLars Ellenberg 	buffer->magic = cpu_to_be32(DRBD_AL_MAGIC);
337b411b363SPhilipp Reisner 	buffer->tr_number = cpu_to_be32(mdev->al_tr_number);
338b411b363SPhilipp Reisner 
339*7ad651b5SLars Ellenberg 	i = 0;
340b411b363SPhilipp Reisner 
341*7ad651b5SLars Ellenberg 	/* Even though no one can start to change this list
342*7ad651b5SLars Ellenberg 	 * once we set the LC_LOCKED -- from drbd_al_begin_io(),
343*7ad651b5SLars Ellenberg 	 * lc_try_lock_for_transaction() --, someone may still
344*7ad651b5SLars Ellenberg 	 * be in the process of changing it. */
345*7ad651b5SLars Ellenberg 	spin_lock_irq(&mdev->al_lock);
346*7ad651b5SLars Ellenberg 	list_for_each_entry(e, &mdev->act_log->to_be_changed, list) {
347*7ad651b5SLars Ellenberg 		if (i == AL_UPDATES_PER_TRANSACTION) {
348*7ad651b5SLars Ellenberg 			i++;
349*7ad651b5SLars Ellenberg 			break;
350*7ad651b5SLars Ellenberg 		}
351*7ad651b5SLars Ellenberg 		buffer->update_slot_nr[i] = cpu_to_be16(e->lc_index);
352*7ad651b5SLars Ellenberg 		buffer->update_extent_nr[i] = cpu_to_be32(e->lc_new_number);
353*7ad651b5SLars Ellenberg 		if (e->lc_number != LC_FREE)
354*7ad651b5SLars Ellenberg 			drbd_bm_mark_for_writeout(mdev,
355*7ad651b5SLars Ellenberg 					al_extent_to_bm_page(e->lc_number));
356*7ad651b5SLars Ellenberg 		i++;
357*7ad651b5SLars Ellenberg 	}
358*7ad651b5SLars Ellenberg 	spin_unlock_irq(&mdev->al_lock);
359*7ad651b5SLars Ellenberg 	BUG_ON(i > AL_UPDATES_PER_TRANSACTION);
360b411b363SPhilipp Reisner 
361*7ad651b5SLars Ellenberg 	buffer->n_updates = cpu_to_be16(i);
362*7ad651b5SLars Ellenberg 	for ( ; i < AL_UPDATES_PER_TRANSACTION; i++) {
363*7ad651b5SLars Ellenberg 		buffer->update_slot_nr[i] = cpu_to_be16(-1);
364*7ad651b5SLars Ellenberg 		buffer->update_extent_nr[i] = cpu_to_be32(LC_FREE);
365*7ad651b5SLars Ellenberg 	}
366b411b363SPhilipp Reisner 
367*7ad651b5SLars Ellenberg 	buffer->context_size = cpu_to_be16(mdev->act_log->nr_elements);
368*7ad651b5SLars Ellenberg 	buffer->context_start_slot_nr = cpu_to_be16(mdev->al_tr_cycle);
369*7ad651b5SLars Ellenberg 
370*7ad651b5SLars Ellenberg 	mx = min_t(int, AL_CONTEXT_PER_TRANSACTION,
371b411b363SPhilipp Reisner 		   mdev->act_log->nr_elements - mdev->al_tr_cycle);
372b411b363SPhilipp Reisner 	for (i = 0; i < mx; i++) {
373b411b363SPhilipp Reisner 		unsigned idx = mdev->al_tr_cycle + i;
374b411b363SPhilipp Reisner 		extent_nr = lc_element_by_index(mdev->act_log, idx)->lc_number;
375*7ad651b5SLars Ellenberg 		buffer->context[i] = cpu_to_be32(extent_nr);
376b411b363SPhilipp Reisner 	}
377*7ad651b5SLars Ellenberg 	for (; i < AL_CONTEXT_PER_TRANSACTION; i++)
378*7ad651b5SLars Ellenberg 		buffer->context[i] = cpu_to_be32(LC_FREE);
379*7ad651b5SLars Ellenberg 
380*7ad651b5SLars Ellenberg 	mdev->al_tr_cycle += AL_CONTEXT_PER_TRANSACTION;
381b411b363SPhilipp Reisner 	if (mdev->al_tr_cycle >= mdev->act_log->nr_elements)
382b411b363SPhilipp Reisner 		mdev->al_tr_cycle = 0;
383b411b363SPhilipp Reisner 
384b411b363SPhilipp Reisner 	sector =  mdev->ldev->md.md_offset
385*7ad651b5SLars Ellenberg 		+ mdev->ldev->md.al_offset
386*7ad651b5SLars Ellenberg 		+ mdev->al_tr_pos * (MD_BLOCK_SIZE>>9);
387b411b363SPhilipp Reisner 
388*7ad651b5SLars Ellenberg 	crc = crc32c(0, buffer, 4096);
389*7ad651b5SLars Ellenberg 	buffer->crc32c = cpu_to_be32(crc);
390*7ad651b5SLars Ellenberg 
391*7ad651b5SLars Ellenberg 	if (drbd_bm_write_hinted(mdev))
392*7ad651b5SLars Ellenberg 		aw->err = -EIO;
393*7ad651b5SLars Ellenberg 		/* drbd_chk_io_error done already */
394*7ad651b5SLars Ellenberg 	else if (!drbd_md_sync_page_io(mdev, mdev->ldev, sector, WRITE)) {
395*7ad651b5SLars Ellenberg 		aw->err = -EIO;
39681e84650SAndreas Gruenbacher 		drbd_chk_io_error(mdev, 1, true);
397*7ad651b5SLars Ellenberg 	} else {
398*7ad651b5SLars Ellenberg 		/* advance ringbuffer position and transaction counter */
399*7ad651b5SLars Ellenberg 		mdev->al_tr_pos = (mdev->al_tr_pos + 1) % (MD_AL_SECTORS*512/MD_BLOCK_SIZE);
400b411b363SPhilipp Reisner 		mdev->al_tr_number++;
401*7ad651b5SLars Ellenberg 	}
402b411b363SPhilipp Reisner 
403b411b363SPhilipp Reisner 	mutex_unlock(&mdev->md_io_mutex);
404b411b363SPhilipp Reisner 	complete(&((struct update_al_work *)w)->event);
405b411b363SPhilipp Reisner 	put_ldev(mdev);
406b411b363SPhilipp Reisner 
407b411b363SPhilipp Reisner 	return 1;
408b411b363SPhilipp Reisner }
409b411b363SPhilipp Reisner 
410*7ad651b5SLars Ellenberg /* FIXME
411*7ad651b5SLars Ellenberg  * reading of the activity log,
412*7ad651b5SLars Ellenberg  * and potentially dirtying of the affected bitmap regions,
413*7ad651b5SLars Ellenberg  * should be done from userland only.
414*7ad651b5SLars Ellenberg  * DRBD would simply always attach with an empty activity log,
415*7ad651b5SLars Ellenberg  * and refuse to attach to something that looks like a crashed primary.
416*7ad651b5SLars Ellenberg  */
417*7ad651b5SLars Ellenberg 
418b411b363SPhilipp Reisner /**
419b411b363SPhilipp Reisner  * drbd_al_read_tr() - Read a single transaction from the on disk activity log
420b411b363SPhilipp Reisner  * @mdev:	DRBD device.
421b411b363SPhilipp Reisner  * @bdev:	Block device to read form.
422b411b363SPhilipp Reisner  * @b:		pointer to an al_transaction.
423b411b363SPhilipp Reisner  * @index:	On disk slot of the transaction to read.
424b411b363SPhilipp Reisner  *
425b411b363SPhilipp Reisner  * Returns -1 on IO error, 0 on checksum error and 1 upon success.
426b411b363SPhilipp Reisner  */
427b411b363SPhilipp Reisner static int drbd_al_read_tr(struct drbd_conf *mdev,
428b411b363SPhilipp Reisner 			   struct drbd_backing_dev *bdev,
429b411b363SPhilipp Reisner 			   int index)
430b411b363SPhilipp Reisner {
431*7ad651b5SLars Ellenberg 	struct al_transaction_on_disk *b = page_address(mdev->md_io_page);
432b411b363SPhilipp Reisner 	sector_t sector;
433*7ad651b5SLars Ellenberg 	u32 crc;
434b411b363SPhilipp Reisner 
435*7ad651b5SLars Ellenberg 	sector =  bdev->md.md_offset
436*7ad651b5SLars Ellenberg 		+ bdev->md.al_offset
437*7ad651b5SLars Ellenberg 		+ index * (MD_BLOCK_SIZE>>9);
438b411b363SPhilipp Reisner 
439b411b363SPhilipp Reisner 	/* Dont process error normally,
440b411b363SPhilipp Reisner 	 * as this is done before disk is attached! */
441b411b363SPhilipp Reisner 	if (!drbd_md_sync_page_io(mdev, bdev, sector, READ))
442b411b363SPhilipp Reisner 		return -1;
443b411b363SPhilipp Reisner 
444*7ad651b5SLars Ellenberg 	if (!expect(b->magic == cpu_to_be32(DRBD_AL_MAGIC)))
445*7ad651b5SLars Ellenberg 		return 0;
446b411b363SPhilipp Reisner 
447*7ad651b5SLars Ellenberg 	if (!expect(be16_to_cpu(b->n_updates) <= AL_UPDATES_PER_TRANSACTION))
448*7ad651b5SLars Ellenberg 		return 0;
449b411b363SPhilipp Reisner 
450*7ad651b5SLars Ellenberg 	if (!expect(be16_to_cpu(b->context_size) <= DRBD_AL_EXTENTS_MAX))
451*7ad651b5SLars Ellenberg 		return 0;
452*7ad651b5SLars Ellenberg 
453*7ad651b5SLars Ellenberg 	if (!expect(be16_to_cpu(b->context_start_slot_nr) < DRBD_AL_EXTENTS_MAX))
454*7ad651b5SLars Ellenberg 		return 0;
455*7ad651b5SLars Ellenberg 
456*7ad651b5SLars Ellenberg 	crc = be32_to_cpu(b->crc32c);
457*7ad651b5SLars Ellenberg 	b->crc32c = 0;
458*7ad651b5SLars Ellenberg 	if (!expect(crc == crc32c(0, b, 4096)))
459*7ad651b5SLars Ellenberg 		return 0;
460*7ad651b5SLars Ellenberg 
461*7ad651b5SLars Ellenberg 	return 1;
462b411b363SPhilipp Reisner }
463b411b363SPhilipp Reisner 
464b411b363SPhilipp Reisner /**
465b411b363SPhilipp Reisner  * drbd_al_read_log() - Restores the activity log from its on disk representation.
466b411b363SPhilipp Reisner  * @mdev:	DRBD device.
467b411b363SPhilipp Reisner  * @bdev:	Block device to read form.
468b411b363SPhilipp Reisner  *
469b411b363SPhilipp Reisner  * Returns 1 on success, returns 0 when reading the log failed due to IO errors.
470b411b363SPhilipp Reisner  */
471b411b363SPhilipp Reisner int drbd_al_read_log(struct drbd_conf *mdev, struct drbd_backing_dev *bdev)
472b411b363SPhilipp Reisner {
473*7ad651b5SLars Ellenberg 	struct al_transaction_on_disk *b;
474b411b363SPhilipp Reisner 	int i;
475b411b363SPhilipp Reisner 	int rv;
476b411b363SPhilipp Reisner 	int mx;
477b411b363SPhilipp Reisner 	int active_extents = 0;
478b411b363SPhilipp Reisner 	int transactions = 0;
479b411b363SPhilipp Reisner 	int found_valid = 0;
480b411b363SPhilipp Reisner 	int from = 0;
481b411b363SPhilipp Reisner 	int to = 0;
482b411b363SPhilipp Reisner 	u32 from_tnr = 0;
483b411b363SPhilipp Reisner 	u32 to_tnr = 0;
484b411b363SPhilipp Reisner 	u32 cnr;
485b411b363SPhilipp Reisner 
486*7ad651b5SLars Ellenberg 	/* Note that this is expected to be called with a newly created,
487*7ad651b5SLars Ellenberg 	 * clean and all unused activity log of the "expected size".
488*7ad651b5SLars Ellenberg 	 */
489b411b363SPhilipp Reisner 
490b411b363SPhilipp Reisner 	/* lock out all other meta data io for now,
491b411b363SPhilipp Reisner 	 * and make sure the page is mapped.
492b411b363SPhilipp Reisner 	 */
493b411b363SPhilipp Reisner 	mutex_lock(&mdev->md_io_mutex);
494*7ad651b5SLars Ellenberg 	b = page_address(mdev->md_io_page);
495*7ad651b5SLars Ellenberg 
496*7ad651b5SLars Ellenberg 	/* Always use the full ringbuffer space for now.
497*7ad651b5SLars Ellenberg 	 * possible optimization: read in all of it,
498*7ad651b5SLars Ellenberg 	 * then scan the in-memory pages. */
499*7ad651b5SLars Ellenberg 
500*7ad651b5SLars Ellenberg 	mx = (MD_AL_SECTORS*512/MD_BLOCK_SIZE);
501b411b363SPhilipp Reisner 
502b411b363SPhilipp Reisner 	/* Find the valid transaction in the log */
503*7ad651b5SLars Ellenberg 	for (i = 0; i < mx; i++) {
504*7ad651b5SLars Ellenberg 		rv = drbd_al_read_tr(mdev, bdev, i);
505*7ad651b5SLars Ellenberg 		/* invalid data in that block */
506b411b363SPhilipp Reisner 		if (rv == 0)
507b411b363SPhilipp Reisner 			continue;
508*7ad651b5SLars Ellenberg 
509*7ad651b5SLars Ellenberg 		/* IO error */
510b411b363SPhilipp Reisner 		if (rv == -1) {
511b411b363SPhilipp Reisner 			mutex_unlock(&mdev->md_io_mutex);
512b411b363SPhilipp Reisner 			return 0;
513b411b363SPhilipp Reisner 		}
514b411b363SPhilipp Reisner 
515*7ad651b5SLars Ellenberg 		cnr = be32_to_cpu(b->tr_number);
516b411b363SPhilipp Reisner 		if (++found_valid == 1) {
517b411b363SPhilipp Reisner 			from = i;
518b411b363SPhilipp Reisner 			to = i;
519b411b363SPhilipp Reisner 			from_tnr = cnr;
520b411b363SPhilipp Reisner 			to_tnr = cnr;
521b411b363SPhilipp Reisner 			continue;
522b411b363SPhilipp Reisner 		}
523*7ad651b5SLars Ellenberg 
524*7ad651b5SLars Ellenberg 		D_ASSERT(cnr != to_tnr);
525*7ad651b5SLars Ellenberg 		D_ASSERT(cnr != from_tnr);
526b411b363SPhilipp Reisner 		if ((int)cnr - (int)from_tnr < 0) {
527*7ad651b5SLars Ellenberg 			D_ASSERT(from_tnr - cnr + i - from == mx);
528b411b363SPhilipp Reisner 			from = i;
529b411b363SPhilipp Reisner 			from_tnr = cnr;
530b411b363SPhilipp Reisner 		}
531b411b363SPhilipp Reisner 		if ((int)cnr - (int)to_tnr > 0) {
532b411b363SPhilipp Reisner 			D_ASSERT(cnr - to_tnr == i - to);
533b411b363SPhilipp Reisner 			to = i;
534b411b363SPhilipp Reisner 			to_tnr = cnr;
535b411b363SPhilipp Reisner 		}
536b411b363SPhilipp Reisner 	}
537b411b363SPhilipp Reisner 
538b411b363SPhilipp Reisner 	if (!found_valid) {
539b411b363SPhilipp Reisner 		dev_warn(DEV, "No usable activity log found.\n");
540b411b363SPhilipp Reisner 		mutex_unlock(&mdev->md_io_mutex);
541b411b363SPhilipp Reisner 		return 1;
542b411b363SPhilipp Reisner 	}
543b411b363SPhilipp Reisner 
544b411b363SPhilipp Reisner 	/* Read the valid transactions.
545b411b363SPhilipp Reisner 	 * dev_info(DEV, "Reading from %d to %d.\n",from,to); */
546b411b363SPhilipp Reisner 	i = from;
547b411b363SPhilipp Reisner 	while (1) {
548*7ad651b5SLars Ellenberg 		struct lc_element *e;
549*7ad651b5SLars Ellenberg 		unsigned j, n, slot, extent_nr;
550b411b363SPhilipp Reisner 
551*7ad651b5SLars Ellenberg 		rv = drbd_al_read_tr(mdev, bdev, i);
552841ce241SAndreas Gruenbacher 		if (!expect(rv != 0))
553841ce241SAndreas Gruenbacher 			goto cancel;
554b411b363SPhilipp Reisner 		if (rv == -1) {
555b411b363SPhilipp Reisner 			mutex_unlock(&mdev->md_io_mutex);
556b411b363SPhilipp Reisner 			return 0;
557b411b363SPhilipp Reisner 		}
558b411b363SPhilipp Reisner 
559*7ad651b5SLars Ellenberg 		/* deal with different transaction types.
560*7ad651b5SLars Ellenberg 		 * not yet implemented */
561*7ad651b5SLars Ellenberg 		if (!expect(b->transaction_type == 0))
562*7ad651b5SLars Ellenberg 			goto cancel;
563b411b363SPhilipp Reisner 
564*7ad651b5SLars Ellenberg 		/* on the fly re-create/resize activity log?
565*7ad651b5SLars Ellenberg 		 * will be a special transaction type flag. */
566*7ad651b5SLars Ellenberg 		if (!expect(be16_to_cpu(b->context_size) == mdev->act_log->nr_elements))
567*7ad651b5SLars Ellenberg 			goto cancel;
568*7ad651b5SLars Ellenberg 		if (!expect(be16_to_cpu(b->context_start_slot_nr) < mdev->act_log->nr_elements))
569*7ad651b5SLars Ellenberg 			goto cancel;
570*7ad651b5SLars Ellenberg 
571*7ad651b5SLars Ellenberg 		/* We are the only user of the activity log right now,
572*7ad651b5SLars Ellenberg 		 * don't actually need to take that lock. */
573b411b363SPhilipp Reisner 		spin_lock_irq(&mdev->al_lock);
574b411b363SPhilipp Reisner 
575*7ad651b5SLars Ellenberg 		/* first, apply the context, ... */
576*7ad651b5SLars Ellenberg 		for (j = 0, slot = be16_to_cpu(b->context_start_slot_nr);
577*7ad651b5SLars Ellenberg 		     j < AL_CONTEXT_PER_TRANSACTION &&
578*7ad651b5SLars Ellenberg 		     slot < mdev->act_log->nr_elements; j++, slot++) {
579*7ad651b5SLars Ellenberg 			extent_nr = be32_to_cpu(b->context[j]);
580*7ad651b5SLars Ellenberg 			e = lc_element_by_index(mdev->act_log, slot);
581*7ad651b5SLars Ellenberg 			if (e->lc_number != extent_nr) {
582*7ad651b5SLars Ellenberg 				if (extent_nr != LC_FREE)
583b411b363SPhilipp Reisner 					active_extents++;
584*7ad651b5SLars Ellenberg 				else
585*7ad651b5SLars Ellenberg 					active_extents--;
586*7ad651b5SLars Ellenberg 			}
587*7ad651b5SLars Ellenberg 			lc_set(mdev->act_log, extent_nr, slot);
588*7ad651b5SLars Ellenberg 		}
589*7ad651b5SLars Ellenberg 
590*7ad651b5SLars Ellenberg 		/* ... then apply the updates,
591*7ad651b5SLars Ellenberg 		 * which override the context information.
592*7ad651b5SLars Ellenberg 		 * drbd_al_read_tr already did the rangecheck
593*7ad651b5SLars Ellenberg 		 * on n <= AL_UPDATES_PER_TRANSACTION */
594*7ad651b5SLars Ellenberg 		n = be16_to_cpu(b->n_updates);
595*7ad651b5SLars Ellenberg 		for (j = 0; j < n; j++) {
596*7ad651b5SLars Ellenberg 			slot = be16_to_cpu(b->update_slot_nr[j]);
597*7ad651b5SLars Ellenberg 			extent_nr = be32_to_cpu(b->update_extent_nr[j]);
598*7ad651b5SLars Ellenberg 			if (!expect(slot < mdev->act_log->nr_elements))
599*7ad651b5SLars Ellenberg 				break;
600*7ad651b5SLars Ellenberg 			e = lc_element_by_index(mdev->act_log, slot);
601*7ad651b5SLars Ellenberg 			if (e->lc_number != extent_nr) {
602*7ad651b5SLars Ellenberg 				if (extent_nr != LC_FREE)
603*7ad651b5SLars Ellenberg 					active_extents++;
604*7ad651b5SLars Ellenberg 				else
605*7ad651b5SLars Ellenberg 					active_extents--;
606*7ad651b5SLars Ellenberg 			}
607*7ad651b5SLars Ellenberg 			lc_set(mdev->act_log, extent_nr, slot);
608b411b363SPhilipp Reisner 		}
609b411b363SPhilipp Reisner 		spin_unlock_irq(&mdev->al_lock);
610b411b363SPhilipp Reisner 
611b411b363SPhilipp Reisner 		transactions++;
612b411b363SPhilipp Reisner 
613b411b363SPhilipp Reisner cancel:
614b411b363SPhilipp Reisner 		if (i == to)
615b411b363SPhilipp Reisner 			break;
616b411b363SPhilipp Reisner 		i++;
617*7ad651b5SLars Ellenberg 		if (i >= mx)
618b411b363SPhilipp Reisner 			i = 0;
619b411b363SPhilipp Reisner 	}
620b411b363SPhilipp Reisner 
621b411b363SPhilipp Reisner 	mdev->al_tr_number = to_tnr+1;
622*7ad651b5SLars Ellenberg 	mdev->al_tr_pos = (to + 1) % (MD_AL_SECTORS*512/MD_BLOCK_SIZE);
623b411b363SPhilipp Reisner 
624b411b363SPhilipp Reisner 	/* ok, we are done with it */
625b411b363SPhilipp Reisner 	mutex_unlock(&mdev->md_io_mutex);
626b411b363SPhilipp Reisner 
627b411b363SPhilipp Reisner 	dev_info(DEV, "Found %d transactions (%d active extents) in activity log.\n",
628b411b363SPhilipp Reisner 	     transactions, active_extents);
629b411b363SPhilipp Reisner 
630b411b363SPhilipp Reisner 	return 1;
631b411b363SPhilipp Reisner }
632b411b363SPhilipp Reisner 
633b411b363SPhilipp Reisner /**
634867f5748SLars Ellenberg  * drbd_al_apply_to_bm() - Sets the bitmap to dirty(1) where covered by active AL extents
635b411b363SPhilipp Reisner  * @mdev:	DRBD device.
636b411b363SPhilipp Reisner  */
637b411b363SPhilipp Reisner void drbd_al_apply_to_bm(struct drbd_conf *mdev)
638b411b363SPhilipp Reisner {
639b411b363SPhilipp Reisner 	unsigned int enr;
640b411b363SPhilipp Reisner 	unsigned long add = 0;
641b411b363SPhilipp Reisner 	char ppb[10];
6426719fb03SLars Ellenberg 	int i, tmp;
643b411b363SPhilipp Reisner 
644b411b363SPhilipp Reisner 	wait_event(mdev->al_wait, lc_try_lock(mdev->act_log));
645b411b363SPhilipp Reisner 
646b411b363SPhilipp Reisner 	for (i = 0; i < mdev->act_log->nr_elements; i++) {
647b411b363SPhilipp Reisner 		enr = lc_element_by_index(mdev->act_log, i)->lc_number;
648b411b363SPhilipp Reisner 		if (enr == LC_FREE)
649b411b363SPhilipp Reisner 			continue;
6506719fb03SLars Ellenberg 		tmp = drbd_bm_ALe_set_all(mdev, enr);
6516719fb03SLars Ellenberg 		dynamic_dev_dbg(DEV, "AL: set %d bits in extent %u\n", tmp, enr);
6526719fb03SLars Ellenberg 		add += tmp;
653b411b363SPhilipp Reisner 	}
654b411b363SPhilipp Reisner 
655b411b363SPhilipp Reisner 	lc_unlock(mdev->act_log);
656b411b363SPhilipp Reisner 	wake_up(&mdev->al_wait);
657b411b363SPhilipp Reisner 
658b411b363SPhilipp Reisner 	dev_info(DEV, "Marked additional %s as out-of-sync based on AL.\n",
659b411b363SPhilipp Reisner 	     ppsize(ppb, Bit2KB(add)));
660b411b363SPhilipp Reisner }
661b411b363SPhilipp Reisner 
662b411b363SPhilipp Reisner static int _try_lc_del(struct drbd_conf *mdev, struct lc_element *al_ext)
663b411b363SPhilipp Reisner {
664b411b363SPhilipp Reisner 	int rv;
665b411b363SPhilipp Reisner 
666b411b363SPhilipp Reisner 	spin_lock_irq(&mdev->al_lock);
667b411b363SPhilipp Reisner 	rv = (al_ext->refcnt == 0);
668b411b363SPhilipp Reisner 	if (likely(rv))
669b411b363SPhilipp Reisner 		lc_del(mdev->act_log, al_ext);
670b411b363SPhilipp Reisner 	spin_unlock_irq(&mdev->al_lock);
671b411b363SPhilipp Reisner 
672b411b363SPhilipp Reisner 	return rv;
673b411b363SPhilipp Reisner }
674b411b363SPhilipp Reisner 
675b411b363SPhilipp Reisner /**
676b411b363SPhilipp Reisner  * drbd_al_shrink() - Removes all active extents form the activity log
677b411b363SPhilipp Reisner  * @mdev:	DRBD device.
678b411b363SPhilipp Reisner  *
679b411b363SPhilipp Reisner  * Removes all active extents form the activity log, waiting until
680b411b363SPhilipp Reisner  * the reference count of each entry dropped to 0 first, of course.
681b411b363SPhilipp Reisner  *
682b411b363SPhilipp Reisner  * You need to lock mdev->act_log with lc_try_lock() / lc_unlock()
683b411b363SPhilipp Reisner  */
684b411b363SPhilipp Reisner void drbd_al_shrink(struct drbd_conf *mdev)
685b411b363SPhilipp Reisner {
686b411b363SPhilipp Reisner 	struct lc_element *al_ext;
687b411b363SPhilipp Reisner 	int i;
688b411b363SPhilipp Reisner 
68946a15bc3SLars Ellenberg 	D_ASSERT(test_bit(__LC_LOCKED, &mdev->act_log->flags));
690b411b363SPhilipp Reisner 
691b411b363SPhilipp Reisner 	for (i = 0; i < mdev->act_log->nr_elements; i++) {
692b411b363SPhilipp Reisner 		al_ext = lc_element_by_index(mdev->act_log, i);
693b411b363SPhilipp Reisner 		if (al_ext->lc_number == LC_FREE)
694b411b363SPhilipp Reisner 			continue;
695b411b363SPhilipp Reisner 		wait_event(mdev->al_wait, _try_lc_del(mdev, al_ext));
696b411b363SPhilipp Reisner 	}
697b411b363SPhilipp Reisner 
698b411b363SPhilipp Reisner 	wake_up(&mdev->al_wait);
699b411b363SPhilipp Reisner }
700b411b363SPhilipp Reisner 
70100d56944SPhilipp Reisner static int w_update_odbm(struct drbd_work *w, int unused)
702b411b363SPhilipp Reisner {
703b411b363SPhilipp Reisner 	struct update_odbm_work *udw = container_of(w, struct update_odbm_work, w);
70400d56944SPhilipp Reisner 	struct drbd_conf *mdev = w->mdev;
705b411b363SPhilipp Reisner 
706b411b363SPhilipp Reisner 	if (!get_ldev(mdev)) {
707b411b363SPhilipp Reisner 		if (__ratelimit(&drbd_ratelimit_state))
708b411b363SPhilipp Reisner 			dev_warn(DEV, "Can not update on disk bitmap, local IO disabled.\n");
709b411b363SPhilipp Reisner 		kfree(udw);
710b411b363SPhilipp Reisner 		return 1;
711b411b363SPhilipp Reisner 	}
712b411b363SPhilipp Reisner 
71319f843aaSLars Ellenberg 	drbd_bm_write_page(mdev, rs_extent_to_bm_page(udw->enr));
714b411b363SPhilipp Reisner 	put_ldev(mdev);
715b411b363SPhilipp Reisner 
716b411b363SPhilipp Reisner 	kfree(udw);
717b411b363SPhilipp Reisner 
718b411b363SPhilipp Reisner 	if (drbd_bm_total_weight(mdev) <= mdev->rs_failed) {
719b411b363SPhilipp Reisner 		switch (mdev->state.conn) {
720b411b363SPhilipp Reisner 		case C_SYNC_SOURCE:  case C_SYNC_TARGET:
721b411b363SPhilipp Reisner 		case C_PAUSED_SYNC_S: case C_PAUSED_SYNC_T:
722b411b363SPhilipp Reisner 			drbd_resync_finished(mdev);
723b411b363SPhilipp Reisner 		default:
724b411b363SPhilipp Reisner 			/* nothing to do */
725b411b363SPhilipp Reisner 			break;
726b411b363SPhilipp Reisner 		}
727b411b363SPhilipp Reisner 	}
728b411b363SPhilipp Reisner 	drbd_bcast_sync_progress(mdev);
729b411b363SPhilipp Reisner 
730b411b363SPhilipp Reisner 	return 1;
731b411b363SPhilipp Reisner }
732b411b363SPhilipp Reisner 
733b411b363SPhilipp Reisner 
734b411b363SPhilipp Reisner /* ATTENTION. The AL's extents are 4MB each, while the extents in the
735b411b363SPhilipp Reisner  * resync LRU-cache are 16MB each.
736b411b363SPhilipp Reisner  * The caller of this function has to hold an get_ldev() reference.
737b411b363SPhilipp Reisner  *
738b411b363SPhilipp Reisner  * TODO will be obsoleted once we have a caching lru of the on disk bitmap
739b411b363SPhilipp Reisner  */
740b411b363SPhilipp Reisner static void drbd_try_clear_on_disk_bm(struct drbd_conf *mdev, sector_t sector,
741b411b363SPhilipp Reisner 				      int count, int success)
742b411b363SPhilipp Reisner {
743b411b363SPhilipp Reisner 	struct lc_element *e;
744b411b363SPhilipp Reisner 	struct update_odbm_work *udw;
745b411b363SPhilipp Reisner 
746b411b363SPhilipp Reisner 	unsigned int enr;
747b411b363SPhilipp Reisner 
748b411b363SPhilipp Reisner 	D_ASSERT(atomic_read(&mdev->local_cnt));
749b411b363SPhilipp Reisner 
750b411b363SPhilipp Reisner 	/* I simply assume that a sector/size pair never crosses
751b411b363SPhilipp Reisner 	 * a 16 MB extent border. (Currently this is true...) */
752b411b363SPhilipp Reisner 	enr = BM_SECT_TO_EXT(sector);
753b411b363SPhilipp Reisner 
754b411b363SPhilipp Reisner 	e = lc_get(mdev->resync, enr);
755b411b363SPhilipp Reisner 	if (e) {
756b411b363SPhilipp Reisner 		struct bm_extent *ext = lc_entry(e, struct bm_extent, lce);
757b411b363SPhilipp Reisner 		if (ext->lce.lc_number == enr) {
758b411b363SPhilipp Reisner 			if (success)
759b411b363SPhilipp Reisner 				ext->rs_left -= count;
760b411b363SPhilipp Reisner 			else
761b411b363SPhilipp Reisner 				ext->rs_failed += count;
762b411b363SPhilipp Reisner 			if (ext->rs_left < ext->rs_failed) {
763b411b363SPhilipp Reisner 				dev_err(DEV, "BAD! sector=%llus enr=%u rs_left=%d "
764b411b363SPhilipp Reisner 				    "rs_failed=%d count=%d\n",
765b411b363SPhilipp Reisner 				     (unsigned long long)sector,
766b411b363SPhilipp Reisner 				     ext->lce.lc_number, ext->rs_left,
767b411b363SPhilipp Reisner 				     ext->rs_failed, count);
768b411b363SPhilipp Reisner 				dump_stack();
769b411b363SPhilipp Reisner 
770b411b363SPhilipp Reisner 				lc_put(mdev->resync, &ext->lce);
771b411b363SPhilipp Reisner 				drbd_force_state(mdev, NS(conn, C_DISCONNECTING));
772b411b363SPhilipp Reisner 				return;
773b411b363SPhilipp Reisner 			}
774b411b363SPhilipp Reisner 		} else {
775b411b363SPhilipp Reisner 			/* Normally this element should be in the cache,
776b411b363SPhilipp Reisner 			 * since drbd_rs_begin_io() pulled it already in.
777b411b363SPhilipp Reisner 			 *
778b411b363SPhilipp Reisner 			 * But maybe an application write finished, and we set
779b411b363SPhilipp Reisner 			 * something outside the resync lru_cache in sync.
780b411b363SPhilipp Reisner 			 */
781b411b363SPhilipp Reisner 			int rs_left = drbd_bm_e_weight(mdev, enr);
782b411b363SPhilipp Reisner 			if (ext->flags != 0) {
783b411b363SPhilipp Reisner 				dev_warn(DEV, "changing resync lce: %d[%u;%02lx]"
784b411b363SPhilipp Reisner 				     " -> %d[%u;00]\n",
785b411b363SPhilipp Reisner 				     ext->lce.lc_number, ext->rs_left,
786b411b363SPhilipp Reisner 				     ext->flags, enr, rs_left);
787b411b363SPhilipp Reisner 				ext->flags = 0;
788b411b363SPhilipp Reisner 			}
789b411b363SPhilipp Reisner 			if (ext->rs_failed) {
790b411b363SPhilipp Reisner 				dev_warn(DEV, "Kicking resync_lru element enr=%u "
791b411b363SPhilipp Reisner 				     "out with rs_failed=%d\n",
792b411b363SPhilipp Reisner 				     ext->lce.lc_number, ext->rs_failed);
793b411b363SPhilipp Reisner 			}
794b411b363SPhilipp Reisner 			ext->rs_left = rs_left;
795b411b363SPhilipp Reisner 			ext->rs_failed = success ? 0 : count;
79646a15bc3SLars Ellenberg 			/* we don't keep a persistent log of the resync lru,
79746a15bc3SLars Ellenberg 			 * we can commit any change right away. */
79846a15bc3SLars Ellenberg 			lc_committed(mdev->resync);
799b411b363SPhilipp Reisner 		}
800b411b363SPhilipp Reisner 		lc_put(mdev->resync, &ext->lce);
801b411b363SPhilipp Reisner 		/* no race, we are within the al_lock! */
802b411b363SPhilipp Reisner 
803b411b363SPhilipp Reisner 		if (ext->rs_left == ext->rs_failed) {
804b411b363SPhilipp Reisner 			ext->rs_failed = 0;
805b411b363SPhilipp Reisner 
806b411b363SPhilipp Reisner 			udw = kmalloc(sizeof(*udw), GFP_ATOMIC);
807b411b363SPhilipp Reisner 			if (udw) {
808b411b363SPhilipp Reisner 				udw->enr = ext->lce.lc_number;
809b411b363SPhilipp Reisner 				udw->w.cb = w_update_odbm;
810a21e9298SPhilipp Reisner 				udw->w.mdev = mdev;
811e42325a5SPhilipp Reisner 				drbd_queue_work_front(&mdev->tconn->data.work, &udw->w);
812b411b363SPhilipp Reisner 			} else {
813b411b363SPhilipp Reisner 				dev_warn(DEV, "Could not kmalloc an udw\n");
814b411b363SPhilipp Reisner 			}
815b411b363SPhilipp Reisner 		}
816b411b363SPhilipp Reisner 	} else {
817b411b363SPhilipp Reisner 		dev_err(DEV, "lc_get() failed! locked=%d/%d flags=%lu\n",
818b411b363SPhilipp Reisner 		    mdev->resync_locked,
819b411b363SPhilipp Reisner 		    mdev->resync->nr_elements,
820b411b363SPhilipp Reisner 		    mdev->resync->flags);
821b411b363SPhilipp Reisner 	}
822b411b363SPhilipp Reisner }
823b411b363SPhilipp Reisner 
824c6ea14dfSLars Ellenberg void drbd_advance_rs_marks(struct drbd_conf *mdev, unsigned long still_to_go)
825c6ea14dfSLars Ellenberg {
826c6ea14dfSLars Ellenberg 	unsigned long now = jiffies;
827c6ea14dfSLars Ellenberg 	unsigned long last = mdev->rs_mark_time[mdev->rs_last_mark];
828c6ea14dfSLars Ellenberg 	int next = (mdev->rs_last_mark + 1) % DRBD_SYNC_MARKS;
829c6ea14dfSLars Ellenberg 	if (time_after_eq(now, last + DRBD_SYNC_MARK_STEP)) {
830c6ea14dfSLars Ellenberg 		if (mdev->rs_mark_left[mdev->rs_last_mark] != still_to_go &&
831c6ea14dfSLars Ellenberg 		    mdev->state.conn != C_PAUSED_SYNC_T &&
832c6ea14dfSLars Ellenberg 		    mdev->state.conn != C_PAUSED_SYNC_S) {
833c6ea14dfSLars Ellenberg 			mdev->rs_mark_time[next] = now;
834c6ea14dfSLars Ellenberg 			mdev->rs_mark_left[next] = still_to_go;
835c6ea14dfSLars Ellenberg 			mdev->rs_last_mark = next;
836c6ea14dfSLars Ellenberg 		}
837c6ea14dfSLars Ellenberg 	}
838c6ea14dfSLars Ellenberg }
839c6ea14dfSLars Ellenberg 
840b411b363SPhilipp Reisner /* clear the bit corresponding to the piece of storage in question:
841b411b363SPhilipp Reisner  * size byte of data starting from sector.  Only clear a bits of the affected
842b411b363SPhilipp Reisner  * one ore more _aligned_ BM_BLOCK_SIZE blocks.
843b411b363SPhilipp Reisner  *
844b411b363SPhilipp Reisner  * called by worker on C_SYNC_TARGET and receiver on SyncSource.
845b411b363SPhilipp Reisner  *
846b411b363SPhilipp Reisner  */
847b411b363SPhilipp Reisner void __drbd_set_in_sync(struct drbd_conf *mdev, sector_t sector, int size,
848b411b363SPhilipp Reisner 		       const char *file, const unsigned int line)
849b411b363SPhilipp Reisner {
850b411b363SPhilipp Reisner 	/* Is called from worker and receiver context _only_ */
851b411b363SPhilipp Reisner 	unsigned long sbnr, ebnr, lbnr;
852b411b363SPhilipp Reisner 	unsigned long count = 0;
853b411b363SPhilipp Reisner 	sector_t esector, nr_sectors;
854b411b363SPhilipp Reisner 	int wake_up = 0;
855b411b363SPhilipp Reisner 	unsigned long flags;
856b411b363SPhilipp Reisner 
857c670a398SAndreas Gruenbacher 	if (size <= 0 || !IS_ALIGNED(size, 512) || size > DRBD_MAX_BIO_SIZE) {
858b411b363SPhilipp Reisner 		dev_err(DEV, "drbd_set_in_sync: sector=%llus size=%d nonsense!\n",
859b411b363SPhilipp Reisner 				(unsigned long long)sector, size);
860b411b363SPhilipp Reisner 		return;
861b411b363SPhilipp Reisner 	}
862b411b363SPhilipp Reisner 	nr_sectors = drbd_get_capacity(mdev->this_bdev);
863b411b363SPhilipp Reisner 	esector = sector + (size >> 9) - 1;
864b411b363SPhilipp Reisner 
865841ce241SAndreas Gruenbacher 	if (!expect(sector < nr_sectors))
866841ce241SAndreas Gruenbacher 		return;
867841ce241SAndreas Gruenbacher 	if (!expect(esector < nr_sectors))
868841ce241SAndreas Gruenbacher 		esector = nr_sectors - 1;
869b411b363SPhilipp Reisner 
870b411b363SPhilipp Reisner 	lbnr = BM_SECT_TO_BIT(nr_sectors-1);
871b411b363SPhilipp Reisner 
872b411b363SPhilipp Reisner 	/* we clear it (in sync).
873b411b363SPhilipp Reisner 	 * round up start sector, round down end sector.  we make sure we only
874b411b363SPhilipp Reisner 	 * clear full, aligned, BM_BLOCK_SIZE (4K) blocks */
875b411b363SPhilipp Reisner 	if (unlikely(esector < BM_SECT_PER_BIT-1))
876b411b363SPhilipp Reisner 		return;
877b411b363SPhilipp Reisner 	if (unlikely(esector == (nr_sectors-1)))
878b411b363SPhilipp Reisner 		ebnr = lbnr;
879b411b363SPhilipp Reisner 	else
880b411b363SPhilipp Reisner 		ebnr = BM_SECT_TO_BIT(esector - (BM_SECT_PER_BIT-1));
881b411b363SPhilipp Reisner 	sbnr = BM_SECT_TO_BIT(sector + BM_SECT_PER_BIT-1);
882b411b363SPhilipp Reisner 
883b411b363SPhilipp Reisner 	if (sbnr > ebnr)
884b411b363SPhilipp Reisner 		return;
885b411b363SPhilipp Reisner 
886b411b363SPhilipp Reisner 	/*
887b411b363SPhilipp Reisner 	 * ok, (capacity & 7) != 0 sometimes, but who cares...
888b411b363SPhilipp Reisner 	 * we count rs_{total,left} in bits, not sectors.
889b411b363SPhilipp Reisner 	 */
890b411b363SPhilipp Reisner 	count = drbd_bm_clear_bits(mdev, sbnr, ebnr);
8911d7734a0SLars Ellenberg 	if (count && get_ldev(mdev)) {
892c6ea14dfSLars Ellenberg 		drbd_advance_rs_marks(mdev, drbd_bm_total_weight(mdev));
8931d7734a0SLars Ellenberg 		spin_lock_irqsave(&mdev->al_lock, flags);
89481e84650SAndreas Gruenbacher 		drbd_try_clear_on_disk_bm(mdev, sector, count, true);
8951d7734a0SLars Ellenberg 		spin_unlock_irqrestore(&mdev->al_lock, flags);
8961d7734a0SLars Ellenberg 
897b411b363SPhilipp Reisner 		/* just wake_up unconditional now, various lc_chaged(),
898b411b363SPhilipp Reisner 		 * lc_put() in drbd_try_clear_on_disk_bm(). */
899b411b363SPhilipp Reisner 		wake_up = 1;
9001d7734a0SLars Ellenberg 		put_ldev(mdev);
901b411b363SPhilipp Reisner 	}
902b411b363SPhilipp Reisner 	if (wake_up)
903b411b363SPhilipp Reisner 		wake_up(&mdev->al_wait);
904b411b363SPhilipp Reisner }
905b411b363SPhilipp Reisner 
906b411b363SPhilipp Reisner /*
907b411b363SPhilipp Reisner  * this is intended to set one request worth of data out of sync.
908b411b363SPhilipp Reisner  * affects at least 1 bit,
9091816a2b4SLars Ellenberg  * and at most 1+DRBD_MAX_BIO_SIZE/BM_BLOCK_SIZE bits.
910b411b363SPhilipp Reisner  *
911b411b363SPhilipp Reisner  * called by tl_clear and drbd_send_dblock (==drbd_make_request).
912b411b363SPhilipp Reisner  * so this can be _any_ process.
913b411b363SPhilipp Reisner  */
91473a01a18SPhilipp Reisner int __drbd_set_out_of_sync(struct drbd_conf *mdev, sector_t sector, int size,
915b411b363SPhilipp Reisner 			    const char *file, const unsigned int line)
916b411b363SPhilipp Reisner {
917b411b363SPhilipp Reisner 	unsigned long sbnr, ebnr, lbnr, flags;
918b411b363SPhilipp Reisner 	sector_t esector, nr_sectors;
91973a01a18SPhilipp Reisner 	unsigned int enr, count = 0;
920b411b363SPhilipp Reisner 	struct lc_element *e;
921b411b363SPhilipp Reisner 
922c670a398SAndreas Gruenbacher 	if (size <= 0 || !IS_ALIGNED(size, 512) || size > DRBD_MAX_BIO_SIZE) {
923b411b363SPhilipp Reisner 		dev_err(DEV, "sector: %llus, size: %d\n",
924b411b363SPhilipp Reisner 			(unsigned long long)sector, size);
92573a01a18SPhilipp Reisner 		return 0;
926b411b363SPhilipp Reisner 	}
927b411b363SPhilipp Reisner 
928b411b363SPhilipp Reisner 	if (!get_ldev(mdev))
92973a01a18SPhilipp Reisner 		return 0; /* no disk, no metadata, no bitmap to set bits in */
930b411b363SPhilipp Reisner 
931b411b363SPhilipp Reisner 	nr_sectors = drbd_get_capacity(mdev->this_bdev);
932b411b363SPhilipp Reisner 	esector = sector + (size >> 9) - 1;
933b411b363SPhilipp Reisner 
934841ce241SAndreas Gruenbacher 	if (!expect(sector < nr_sectors))
935b411b363SPhilipp Reisner 		goto out;
936841ce241SAndreas Gruenbacher 	if (!expect(esector < nr_sectors))
937841ce241SAndreas Gruenbacher 		esector = nr_sectors - 1;
938b411b363SPhilipp Reisner 
939b411b363SPhilipp Reisner 	lbnr = BM_SECT_TO_BIT(nr_sectors-1);
940b411b363SPhilipp Reisner 
941b411b363SPhilipp Reisner 	/* we set it out of sync,
942b411b363SPhilipp Reisner 	 * we do not need to round anything here */
943b411b363SPhilipp Reisner 	sbnr = BM_SECT_TO_BIT(sector);
944b411b363SPhilipp Reisner 	ebnr = BM_SECT_TO_BIT(esector);
945b411b363SPhilipp Reisner 
946b411b363SPhilipp Reisner 	/* ok, (capacity & 7) != 0 sometimes, but who cares...
947b411b363SPhilipp Reisner 	 * we count rs_{total,left} in bits, not sectors.  */
948b411b363SPhilipp Reisner 	spin_lock_irqsave(&mdev->al_lock, flags);
949b411b363SPhilipp Reisner 	count = drbd_bm_set_bits(mdev, sbnr, ebnr);
950b411b363SPhilipp Reisner 
951b411b363SPhilipp Reisner 	enr = BM_SECT_TO_EXT(sector);
952b411b363SPhilipp Reisner 	e = lc_find(mdev->resync, enr);
953b411b363SPhilipp Reisner 	if (e)
954b411b363SPhilipp Reisner 		lc_entry(e, struct bm_extent, lce)->rs_left += count;
955b411b363SPhilipp Reisner 	spin_unlock_irqrestore(&mdev->al_lock, flags);
956b411b363SPhilipp Reisner 
957b411b363SPhilipp Reisner out:
958b411b363SPhilipp Reisner 	put_ldev(mdev);
95973a01a18SPhilipp Reisner 
96073a01a18SPhilipp Reisner 	return count;
961b411b363SPhilipp Reisner }
962b411b363SPhilipp Reisner 
963b411b363SPhilipp Reisner static
964b411b363SPhilipp Reisner struct bm_extent *_bme_get(struct drbd_conf *mdev, unsigned int enr)
965b411b363SPhilipp Reisner {
966b411b363SPhilipp Reisner 	struct lc_element *e;
967b411b363SPhilipp Reisner 	struct bm_extent *bm_ext;
968b411b363SPhilipp Reisner 	int wakeup = 0;
969b411b363SPhilipp Reisner 	unsigned long rs_flags;
970b411b363SPhilipp Reisner 
971b411b363SPhilipp Reisner 	spin_lock_irq(&mdev->al_lock);
972b411b363SPhilipp Reisner 	if (mdev->resync_locked > mdev->resync->nr_elements/2) {
973b411b363SPhilipp Reisner 		spin_unlock_irq(&mdev->al_lock);
974b411b363SPhilipp Reisner 		return NULL;
975b411b363SPhilipp Reisner 	}
976b411b363SPhilipp Reisner 	e = lc_get(mdev->resync, enr);
977b411b363SPhilipp Reisner 	bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL;
978b411b363SPhilipp Reisner 	if (bm_ext) {
979b411b363SPhilipp Reisner 		if (bm_ext->lce.lc_number != enr) {
980b411b363SPhilipp Reisner 			bm_ext->rs_left = drbd_bm_e_weight(mdev, enr);
981b411b363SPhilipp Reisner 			bm_ext->rs_failed = 0;
98246a15bc3SLars Ellenberg 			lc_committed(mdev->resync);
983b411b363SPhilipp Reisner 			wakeup = 1;
984b411b363SPhilipp Reisner 		}
985b411b363SPhilipp Reisner 		if (bm_ext->lce.refcnt == 1)
986b411b363SPhilipp Reisner 			mdev->resync_locked++;
987b411b363SPhilipp Reisner 		set_bit(BME_NO_WRITES, &bm_ext->flags);
988b411b363SPhilipp Reisner 	}
989b411b363SPhilipp Reisner 	rs_flags = mdev->resync->flags;
990b411b363SPhilipp Reisner 	spin_unlock_irq(&mdev->al_lock);
991b411b363SPhilipp Reisner 	if (wakeup)
992b411b363SPhilipp Reisner 		wake_up(&mdev->al_wait);
993b411b363SPhilipp Reisner 
994b411b363SPhilipp Reisner 	if (!bm_ext) {
995b411b363SPhilipp Reisner 		if (rs_flags & LC_STARVING)
996b411b363SPhilipp Reisner 			dev_warn(DEV, "Have to wait for element"
997b411b363SPhilipp Reisner 			     " (resync LRU too small?)\n");
99846a15bc3SLars Ellenberg 		BUG_ON(rs_flags & LC_LOCKED);
999b411b363SPhilipp Reisner 	}
1000b411b363SPhilipp Reisner 
1001b411b363SPhilipp Reisner 	return bm_ext;
1002b411b363SPhilipp Reisner }
1003b411b363SPhilipp Reisner 
1004b411b363SPhilipp Reisner static int _is_in_al(struct drbd_conf *mdev, unsigned int enr)
1005b411b363SPhilipp Reisner {
100646a15bc3SLars Ellenberg 	int rv;
1007b411b363SPhilipp Reisner 
1008b411b363SPhilipp Reisner 	spin_lock_irq(&mdev->al_lock);
100946a15bc3SLars Ellenberg 	rv = lc_is_used(mdev->act_log, enr);
1010b411b363SPhilipp Reisner 	spin_unlock_irq(&mdev->al_lock);
1011b411b363SPhilipp Reisner 
1012b411b363SPhilipp Reisner 	return rv;
1013b411b363SPhilipp Reisner }
1014b411b363SPhilipp Reisner 
1015b411b363SPhilipp Reisner /**
1016b411b363SPhilipp Reisner  * drbd_rs_begin_io() - Gets an extent in the resync LRU cache and sets it to BME_LOCKED
1017b411b363SPhilipp Reisner  * @mdev:	DRBD device.
1018b411b363SPhilipp Reisner  * @sector:	The sector number.
1019b411b363SPhilipp Reisner  *
102080a40e43SLars Ellenberg  * This functions sleeps on al_wait. Returns 0 on success, -EINTR if interrupted.
1021b411b363SPhilipp Reisner  */
1022b411b363SPhilipp Reisner int drbd_rs_begin_io(struct drbd_conf *mdev, sector_t sector)
1023b411b363SPhilipp Reisner {
1024b411b363SPhilipp Reisner 	unsigned int enr = BM_SECT_TO_EXT(sector);
1025b411b363SPhilipp Reisner 	struct bm_extent *bm_ext;
1026b411b363SPhilipp Reisner 	int i, sig;
1027f91ab628SPhilipp Reisner 	int sa = 200; /* Step aside 200 times, then grab the extent and let app-IO wait.
1028f91ab628SPhilipp Reisner 			 200 times -> 20 seconds. */
1029b411b363SPhilipp Reisner 
1030f91ab628SPhilipp Reisner retry:
1031b411b363SPhilipp Reisner 	sig = wait_event_interruptible(mdev->al_wait,
1032b411b363SPhilipp Reisner 			(bm_ext = _bme_get(mdev, enr)));
1033b411b363SPhilipp Reisner 	if (sig)
103480a40e43SLars Ellenberg 		return -EINTR;
1035b411b363SPhilipp Reisner 
1036b411b363SPhilipp Reisner 	if (test_bit(BME_LOCKED, &bm_ext->flags))
103780a40e43SLars Ellenberg 		return 0;
1038b411b363SPhilipp Reisner 
1039b411b363SPhilipp Reisner 	for (i = 0; i < AL_EXT_PER_BM_SECT; i++) {
1040b411b363SPhilipp Reisner 		sig = wait_event_interruptible(mdev->al_wait,
1041f91ab628SPhilipp Reisner 					       !_is_in_al(mdev, enr * AL_EXT_PER_BM_SECT + i) ||
1042c507f46fSPhilipp Reisner 					       test_bit(BME_PRIORITY, &bm_ext->flags));
1043f91ab628SPhilipp Reisner 
1044f91ab628SPhilipp Reisner 		if (sig || (test_bit(BME_PRIORITY, &bm_ext->flags) && sa)) {
1045b411b363SPhilipp Reisner 			spin_lock_irq(&mdev->al_lock);
1046b411b363SPhilipp Reisner 			if (lc_put(mdev->resync, &bm_ext->lce) == 0) {
1047f91ab628SPhilipp Reisner 				bm_ext->flags = 0; /* clears BME_NO_WRITES and eventually BME_PRIORITY */
1048b411b363SPhilipp Reisner 				mdev->resync_locked--;
1049b411b363SPhilipp Reisner 				wake_up(&mdev->al_wait);
1050b411b363SPhilipp Reisner 			}
1051b411b363SPhilipp Reisner 			spin_unlock_irq(&mdev->al_lock);
1052f91ab628SPhilipp Reisner 			if (sig)
105380a40e43SLars Ellenberg 				return -EINTR;
1054f91ab628SPhilipp Reisner 			if (schedule_timeout_interruptible(HZ/10))
1055f91ab628SPhilipp Reisner 				return -EINTR;
1056c507f46fSPhilipp Reisner 			if (sa && --sa == 0)
1057c507f46fSPhilipp Reisner 				dev_warn(DEV,"drbd_rs_begin_io() stepped aside for 20sec."
1058c507f46fSPhilipp Reisner 					 "Resync stalled?\n");
1059f91ab628SPhilipp Reisner 			goto retry;
1060b411b363SPhilipp Reisner 		}
1061b411b363SPhilipp Reisner 	}
1062b411b363SPhilipp Reisner 	set_bit(BME_LOCKED, &bm_ext->flags);
106380a40e43SLars Ellenberg 	return 0;
1064b411b363SPhilipp Reisner }
1065b411b363SPhilipp Reisner 
1066b411b363SPhilipp Reisner /**
1067b411b363SPhilipp Reisner  * drbd_try_rs_begin_io() - Gets an extent in the resync LRU cache, does not sleep
1068b411b363SPhilipp Reisner  * @mdev:	DRBD device.
1069b411b363SPhilipp Reisner  * @sector:	The sector number.
1070b411b363SPhilipp Reisner  *
1071b411b363SPhilipp Reisner  * Gets an extent in the resync LRU cache, sets it to BME_NO_WRITES, then
1072b411b363SPhilipp Reisner  * tries to set it to BME_LOCKED. Returns 0 upon success, and -EAGAIN
1073b411b363SPhilipp Reisner  * if there is still application IO going on in this area.
1074b411b363SPhilipp Reisner  */
1075b411b363SPhilipp Reisner int drbd_try_rs_begin_io(struct drbd_conf *mdev, sector_t sector)
1076b411b363SPhilipp Reisner {
1077b411b363SPhilipp Reisner 	unsigned int enr = BM_SECT_TO_EXT(sector);
1078b411b363SPhilipp Reisner 	const unsigned int al_enr = enr*AL_EXT_PER_BM_SECT;
1079b411b363SPhilipp Reisner 	struct lc_element *e;
1080b411b363SPhilipp Reisner 	struct bm_extent *bm_ext;
1081b411b363SPhilipp Reisner 	int i;
1082b411b363SPhilipp Reisner 
1083b411b363SPhilipp Reisner 	spin_lock_irq(&mdev->al_lock);
1084b411b363SPhilipp Reisner 	if (mdev->resync_wenr != LC_FREE && mdev->resync_wenr != enr) {
1085b411b363SPhilipp Reisner 		/* in case you have very heavy scattered io, it may
1086b411b363SPhilipp Reisner 		 * stall the syncer undefined if we give up the ref count
1087b411b363SPhilipp Reisner 		 * when we try again and requeue.
1088b411b363SPhilipp Reisner 		 *
1089b411b363SPhilipp Reisner 		 * if we don't give up the refcount, but the next time
1090b411b363SPhilipp Reisner 		 * we are scheduled this extent has been "synced" by new
1091b411b363SPhilipp Reisner 		 * application writes, we'd miss the lc_put on the
1092b411b363SPhilipp Reisner 		 * extent we keep the refcount on.
1093b411b363SPhilipp Reisner 		 * so we remembered which extent we had to try again, and
1094b411b363SPhilipp Reisner 		 * if the next requested one is something else, we do
1095b411b363SPhilipp Reisner 		 * the lc_put here...
1096b411b363SPhilipp Reisner 		 * we also have to wake_up
1097b411b363SPhilipp Reisner 		 */
1098b411b363SPhilipp Reisner 		e = lc_find(mdev->resync, mdev->resync_wenr);
1099b411b363SPhilipp Reisner 		bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL;
1100b411b363SPhilipp Reisner 		if (bm_ext) {
1101b411b363SPhilipp Reisner 			D_ASSERT(!test_bit(BME_LOCKED, &bm_ext->flags));
1102b411b363SPhilipp Reisner 			D_ASSERT(test_bit(BME_NO_WRITES, &bm_ext->flags));
1103b411b363SPhilipp Reisner 			clear_bit(BME_NO_WRITES, &bm_ext->flags);
1104b411b363SPhilipp Reisner 			mdev->resync_wenr = LC_FREE;
1105b411b363SPhilipp Reisner 			if (lc_put(mdev->resync, &bm_ext->lce) == 0)
1106b411b363SPhilipp Reisner 				mdev->resync_locked--;
1107b411b363SPhilipp Reisner 			wake_up(&mdev->al_wait);
1108b411b363SPhilipp Reisner 		} else {
1109b411b363SPhilipp Reisner 			dev_alert(DEV, "LOGIC BUG\n");
1110b411b363SPhilipp Reisner 		}
1111b411b363SPhilipp Reisner 	}
1112b411b363SPhilipp Reisner 	/* TRY. */
1113b411b363SPhilipp Reisner 	e = lc_try_get(mdev->resync, enr);
1114b411b363SPhilipp Reisner 	bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL;
1115b411b363SPhilipp Reisner 	if (bm_ext) {
1116b411b363SPhilipp Reisner 		if (test_bit(BME_LOCKED, &bm_ext->flags))
1117b411b363SPhilipp Reisner 			goto proceed;
1118b411b363SPhilipp Reisner 		if (!test_and_set_bit(BME_NO_WRITES, &bm_ext->flags)) {
1119b411b363SPhilipp Reisner 			mdev->resync_locked++;
1120b411b363SPhilipp Reisner 		} else {
1121b411b363SPhilipp Reisner 			/* we did set the BME_NO_WRITES,
1122b411b363SPhilipp Reisner 			 * but then could not set BME_LOCKED,
1123b411b363SPhilipp Reisner 			 * so we tried again.
1124b411b363SPhilipp Reisner 			 * drop the extra reference. */
1125b411b363SPhilipp Reisner 			bm_ext->lce.refcnt--;
1126b411b363SPhilipp Reisner 			D_ASSERT(bm_ext->lce.refcnt > 0);
1127b411b363SPhilipp Reisner 		}
1128b411b363SPhilipp Reisner 		goto check_al;
1129b411b363SPhilipp Reisner 	} else {
1130b411b363SPhilipp Reisner 		/* do we rather want to try later? */
11316a0afdf5SJens Axboe 		if (mdev->resync_locked > mdev->resync->nr_elements-3)
1132b411b363SPhilipp Reisner 			goto try_again;
1133b411b363SPhilipp Reisner 		/* Do or do not. There is no try. -- Yoda */
1134b411b363SPhilipp Reisner 		e = lc_get(mdev->resync, enr);
1135b411b363SPhilipp Reisner 		bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL;
1136b411b363SPhilipp Reisner 		if (!bm_ext) {
1137b411b363SPhilipp Reisner 			const unsigned long rs_flags = mdev->resync->flags;
1138b411b363SPhilipp Reisner 			if (rs_flags & LC_STARVING)
1139b411b363SPhilipp Reisner 				dev_warn(DEV, "Have to wait for element"
1140b411b363SPhilipp Reisner 				     " (resync LRU too small?)\n");
114146a15bc3SLars Ellenberg 			BUG_ON(rs_flags & LC_LOCKED);
1142b411b363SPhilipp Reisner 			goto try_again;
1143b411b363SPhilipp Reisner 		}
1144b411b363SPhilipp Reisner 		if (bm_ext->lce.lc_number != enr) {
1145b411b363SPhilipp Reisner 			bm_ext->rs_left = drbd_bm_e_weight(mdev, enr);
1146b411b363SPhilipp Reisner 			bm_ext->rs_failed = 0;
114746a15bc3SLars Ellenberg 			lc_committed(mdev->resync);
1148b411b363SPhilipp Reisner 			wake_up(&mdev->al_wait);
1149b411b363SPhilipp Reisner 			D_ASSERT(test_bit(BME_LOCKED, &bm_ext->flags) == 0);
1150b411b363SPhilipp Reisner 		}
1151b411b363SPhilipp Reisner 		set_bit(BME_NO_WRITES, &bm_ext->flags);
1152b411b363SPhilipp Reisner 		D_ASSERT(bm_ext->lce.refcnt == 1);
1153b411b363SPhilipp Reisner 		mdev->resync_locked++;
1154b411b363SPhilipp Reisner 		goto check_al;
1155b411b363SPhilipp Reisner 	}
1156b411b363SPhilipp Reisner check_al:
1157b411b363SPhilipp Reisner 	for (i = 0; i < AL_EXT_PER_BM_SECT; i++) {
1158b411b363SPhilipp Reisner 		if (lc_is_used(mdev->act_log, al_enr+i))
1159b411b363SPhilipp Reisner 			goto try_again;
1160b411b363SPhilipp Reisner 	}
1161b411b363SPhilipp Reisner 	set_bit(BME_LOCKED, &bm_ext->flags);
1162b411b363SPhilipp Reisner proceed:
1163b411b363SPhilipp Reisner 	mdev->resync_wenr = LC_FREE;
1164b411b363SPhilipp Reisner 	spin_unlock_irq(&mdev->al_lock);
1165b411b363SPhilipp Reisner 	return 0;
1166b411b363SPhilipp Reisner 
1167b411b363SPhilipp Reisner try_again:
1168b411b363SPhilipp Reisner 	if (bm_ext)
1169b411b363SPhilipp Reisner 		mdev->resync_wenr = enr;
1170b411b363SPhilipp Reisner 	spin_unlock_irq(&mdev->al_lock);
1171b411b363SPhilipp Reisner 	return -EAGAIN;
1172b411b363SPhilipp Reisner }
1173b411b363SPhilipp Reisner 
1174b411b363SPhilipp Reisner void drbd_rs_complete_io(struct drbd_conf *mdev, sector_t sector)
1175b411b363SPhilipp Reisner {
1176b411b363SPhilipp Reisner 	unsigned int enr = BM_SECT_TO_EXT(sector);
1177b411b363SPhilipp Reisner 	struct lc_element *e;
1178b411b363SPhilipp Reisner 	struct bm_extent *bm_ext;
1179b411b363SPhilipp Reisner 	unsigned long flags;
1180b411b363SPhilipp Reisner 
1181b411b363SPhilipp Reisner 	spin_lock_irqsave(&mdev->al_lock, flags);
1182b411b363SPhilipp Reisner 	e = lc_find(mdev->resync, enr);
1183b411b363SPhilipp Reisner 	bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL;
1184b411b363SPhilipp Reisner 	if (!bm_ext) {
1185b411b363SPhilipp Reisner 		spin_unlock_irqrestore(&mdev->al_lock, flags);
1186b411b363SPhilipp Reisner 		if (__ratelimit(&drbd_ratelimit_state))
1187b411b363SPhilipp Reisner 			dev_err(DEV, "drbd_rs_complete_io() called, but extent not found\n");
1188b411b363SPhilipp Reisner 		return;
1189b411b363SPhilipp Reisner 	}
1190b411b363SPhilipp Reisner 
1191b411b363SPhilipp Reisner 	if (bm_ext->lce.refcnt == 0) {
1192b411b363SPhilipp Reisner 		spin_unlock_irqrestore(&mdev->al_lock, flags);
1193b411b363SPhilipp Reisner 		dev_err(DEV, "drbd_rs_complete_io(,%llu [=%u]) called, "
1194b411b363SPhilipp Reisner 		    "but refcnt is 0!?\n",
1195b411b363SPhilipp Reisner 		    (unsigned long long)sector, enr);
1196b411b363SPhilipp Reisner 		return;
1197b411b363SPhilipp Reisner 	}
1198b411b363SPhilipp Reisner 
1199b411b363SPhilipp Reisner 	if (lc_put(mdev->resync, &bm_ext->lce) == 0) {
1200e3555d85SPhilipp Reisner 		bm_ext->flags = 0; /* clear BME_LOCKED, BME_NO_WRITES and BME_PRIORITY */
1201b411b363SPhilipp Reisner 		mdev->resync_locked--;
1202b411b363SPhilipp Reisner 		wake_up(&mdev->al_wait);
1203b411b363SPhilipp Reisner 	}
1204b411b363SPhilipp Reisner 
1205b411b363SPhilipp Reisner 	spin_unlock_irqrestore(&mdev->al_lock, flags);
1206b411b363SPhilipp Reisner }
1207b411b363SPhilipp Reisner 
1208b411b363SPhilipp Reisner /**
1209b411b363SPhilipp Reisner  * drbd_rs_cancel_all() - Removes all extents from the resync LRU (even BME_LOCKED)
1210b411b363SPhilipp Reisner  * @mdev:	DRBD device.
1211b411b363SPhilipp Reisner  */
1212b411b363SPhilipp Reisner void drbd_rs_cancel_all(struct drbd_conf *mdev)
1213b411b363SPhilipp Reisner {
1214b411b363SPhilipp Reisner 	spin_lock_irq(&mdev->al_lock);
1215b411b363SPhilipp Reisner 
1216b411b363SPhilipp Reisner 	if (get_ldev_if_state(mdev, D_FAILED)) { /* Makes sure ->resync is there. */
1217b411b363SPhilipp Reisner 		lc_reset(mdev->resync);
1218b411b363SPhilipp Reisner 		put_ldev(mdev);
1219b411b363SPhilipp Reisner 	}
1220b411b363SPhilipp Reisner 	mdev->resync_locked = 0;
1221b411b363SPhilipp Reisner 	mdev->resync_wenr = LC_FREE;
1222b411b363SPhilipp Reisner 	spin_unlock_irq(&mdev->al_lock);
1223b411b363SPhilipp Reisner 	wake_up(&mdev->al_wait);
1224b411b363SPhilipp Reisner }
1225b411b363SPhilipp Reisner 
1226b411b363SPhilipp Reisner /**
1227b411b363SPhilipp Reisner  * drbd_rs_del_all() - Gracefully remove all extents from the resync LRU
1228b411b363SPhilipp Reisner  * @mdev:	DRBD device.
1229b411b363SPhilipp Reisner  *
1230b411b363SPhilipp Reisner  * Returns 0 upon success, -EAGAIN if at least one reference count was
1231b411b363SPhilipp Reisner  * not zero.
1232b411b363SPhilipp Reisner  */
1233b411b363SPhilipp Reisner int drbd_rs_del_all(struct drbd_conf *mdev)
1234b411b363SPhilipp Reisner {
1235b411b363SPhilipp Reisner 	struct lc_element *e;
1236b411b363SPhilipp Reisner 	struct bm_extent *bm_ext;
1237b411b363SPhilipp Reisner 	int i;
1238b411b363SPhilipp Reisner 
1239b411b363SPhilipp Reisner 	spin_lock_irq(&mdev->al_lock);
1240b411b363SPhilipp Reisner 
1241b411b363SPhilipp Reisner 	if (get_ldev_if_state(mdev, D_FAILED)) {
1242b411b363SPhilipp Reisner 		/* ok, ->resync is there. */
1243b411b363SPhilipp Reisner 		for (i = 0; i < mdev->resync->nr_elements; i++) {
1244b411b363SPhilipp Reisner 			e = lc_element_by_index(mdev->resync, i);
1245b2b163ddSPhilipp Reisner 			bm_ext = lc_entry(e, struct bm_extent, lce);
1246b411b363SPhilipp Reisner 			if (bm_ext->lce.lc_number == LC_FREE)
1247b411b363SPhilipp Reisner 				continue;
1248b411b363SPhilipp Reisner 			if (bm_ext->lce.lc_number == mdev->resync_wenr) {
1249b411b363SPhilipp Reisner 				dev_info(DEV, "dropping %u in drbd_rs_del_all, apparently"
1250b411b363SPhilipp Reisner 				     " got 'synced' by application io\n",
1251b411b363SPhilipp Reisner 				     mdev->resync_wenr);
1252b411b363SPhilipp Reisner 				D_ASSERT(!test_bit(BME_LOCKED, &bm_ext->flags));
1253b411b363SPhilipp Reisner 				D_ASSERT(test_bit(BME_NO_WRITES, &bm_ext->flags));
1254b411b363SPhilipp Reisner 				clear_bit(BME_NO_WRITES, &bm_ext->flags);
1255b411b363SPhilipp Reisner 				mdev->resync_wenr = LC_FREE;
1256b411b363SPhilipp Reisner 				lc_put(mdev->resync, &bm_ext->lce);
1257b411b363SPhilipp Reisner 			}
1258b411b363SPhilipp Reisner 			if (bm_ext->lce.refcnt != 0) {
1259b411b363SPhilipp Reisner 				dev_info(DEV, "Retrying drbd_rs_del_all() later. "
1260b411b363SPhilipp Reisner 				     "refcnt=%d\n", bm_ext->lce.refcnt);
1261b411b363SPhilipp Reisner 				put_ldev(mdev);
1262b411b363SPhilipp Reisner 				spin_unlock_irq(&mdev->al_lock);
1263b411b363SPhilipp Reisner 				return -EAGAIN;
1264b411b363SPhilipp Reisner 			}
1265b411b363SPhilipp Reisner 			D_ASSERT(!test_bit(BME_LOCKED, &bm_ext->flags));
1266b411b363SPhilipp Reisner 			D_ASSERT(!test_bit(BME_NO_WRITES, &bm_ext->flags));
1267b411b363SPhilipp Reisner 			lc_del(mdev->resync, &bm_ext->lce);
1268b411b363SPhilipp Reisner 		}
1269b411b363SPhilipp Reisner 		D_ASSERT(mdev->resync->used == 0);
1270b411b363SPhilipp Reisner 		put_ldev(mdev);
1271b411b363SPhilipp Reisner 	}
1272b411b363SPhilipp Reisner 	spin_unlock_irq(&mdev->al_lock);
1273b411b363SPhilipp Reisner 
1274b411b363SPhilipp Reisner 	return 0;
1275b411b363SPhilipp Reisner }
1276b411b363SPhilipp Reisner 
1277b411b363SPhilipp Reisner /**
1278b411b363SPhilipp Reisner  * drbd_rs_failed_io() - Record information on a failure to resync the specified blocks
1279b411b363SPhilipp Reisner  * @mdev:	DRBD device.
1280b411b363SPhilipp Reisner  * @sector:	The sector number.
1281b411b363SPhilipp Reisner  * @size:	Size of failed IO operation, in byte.
1282b411b363SPhilipp Reisner  */
1283b411b363SPhilipp Reisner void drbd_rs_failed_io(struct drbd_conf *mdev, sector_t sector, int size)
1284b411b363SPhilipp Reisner {
1285b411b363SPhilipp Reisner 	/* Is called from worker and receiver context _only_ */
1286b411b363SPhilipp Reisner 	unsigned long sbnr, ebnr, lbnr;
1287b411b363SPhilipp Reisner 	unsigned long count;
1288b411b363SPhilipp Reisner 	sector_t esector, nr_sectors;
1289b411b363SPhilipp Reisner 	int wake_up = 0;
1290b411b363SPhilipp Reisner 
1291c670a398SAndreas Gruenbacher 	if (size <= 0 || !IS_ALIGNED(size, 512) || size > DRBD_MAX_BIO_SIZE) {
1292b411b363SPhilipp Reisner 		dev_err(DEV, "drbd_rs_failed_io: sector=%llus size=%d nonsense!\n",
1293b411b363SPhilipp Reisner 				(unsigned long long)sector, size);
1294b411b363SPhilipp Reisner 		return;
1295b411b363SPhilipp Reisner 	}
1296b411b363SPhilipp Reisner 	nr_sectors = drbd_get_capacity(mdev->this_bdev);
1297b411b363SPhilipp Reisner 	esector = sector + (size >> 9) - 1;
1298b411b363SPhilipp Reisner 
1299841ce241SAndreas Gruenbacher 	if (!expect(sector < nr_sectors))
1300841ce241SAndreas Gruenbacher 		return;
1301841ce241SAndreas Gruenbacher 	if (!expect(esector < nr_sectors))
1302841ce241SAndreas Gruenbacher 		esector = nr_sectors - 1;
1303b411b363SPhilipp Reisner 
1304b411b363SPhilipp Reisner 	lbnr = BM_SECT_TO_BIT(nr_sectors-1);
1305b411b363SPhilipp Reisner 
1306b411b363SPhilipp Reisner 	/*
1307b411b363SPhilipp Reisner 	 * round up start sector, round down end sector.  we make sure we only
1308b411b363SPhilipp Reisner 	 * handle full, aligned, BM_BLOCK_SIZE (4K) blocks */
1309b411b363SPhilipp Reisner 	if (unlikely(esector < BM_SECT_PER_BIT-1))
1310b411b363SPhilipp Reisner 		return;
1311b411b363SPhilipp Reisner 	if (unlikely(esector == (nr_sectors-1)))
1312b411b363SPhilipp Reisner 		ebnr = lbnr;
1313b411b363SPhilipp Reisner 	else
1314b411b363SPhilipp Reisner 		ebnr = BM_SECT_TO_BIT(esector - (BM_SECT_PER_BIT-1));
1315b411b363SPhilipp Reisner 	sbnr = BM_SECT_TO_BIT(sector + BM_SECT_PER_BIT-1);
1316b411b363SPhilipp Reisner 
1317b411b363SPhilipp Reisner 	if (sbnr > ebnr)
1318b411b363SPhilipp Reisner 		return;
1319b411b363SPhilipp Reisner 
1320b411b363SPhilipp Reisner 	/*
1321b411b363SPhilipp Reisner 	 * ok, (capacity & 7) != 0 sometimes, but who cares...
1322b411b363SPhilipp Reisner 	 * we count rs_{total,left} in bits, not sectors.
1323b411b363SPhilipp Reisner 	 */
1324b411b363SPhilipp Reisner 	spin_lock_irq(&mdev->al_lock);
1325b411b363SPhilipp Reisner 	count = drbd_bm_count_bits(mdev, sbnr, ebnr);
1326b411b363SPhilipp Reisner 	if (count) {
1327b411b363SPhilipp Reisner 		mdev->rs_failed += count;
1328b411b363SPhilipp Reisner 
1329b411b363SPhilipp Reisner 		if (get_ldev(mdev)) {
133081e84650SAndreas Gruenbacher 			drbd_try_clear_on_disk_bm(mdev, sector, count, false);
1331b411b363SPhilipp Reisner 			put_ldev(mdev);
1332b411b363SPhilipp Reisner 		}
1333b411b363SPhilipp Reisner 
1334b411b363SPhilipp Reisner 		/* just wake_up unconditional now, various lc_chaged(),
1335b411b363SPhilipp Reisner 		 * lc_put() in drbd_try_clear_on_disk_bm(). */
1336b411b363SPhilipp Reisner 		wake_up = 1;
1337b411b363SPhilipp Reisner 	}
1338b411b363SPhilipp Reisner 	spin_unlock_irq(&mdev->al_lock);
1339b411b363SPhilipp Reisner 	if (wake_up)
1340b411b363SPhilipp Reisner 		wake_up(&mdev->al_wait);
1341b411b363SPhilipp Reisner }
1342