xref: /openbmc/linux/drivers/block/drbd/drbd_actlog.c (revision 6719fb036cea56a5ee9d0ac912ed8c7cabb27f49)
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>
27b411b363SPhilipp Reisner #include <linux/drbd.h>
28b411b363SPhilipp Reisner #include "drbd_int.h"
29b411b363SPhilipp Reisner #include "drbd_wrappers.h"
30b411b363SPhilipp Reisner 
31b411b363SPhilipp Reisner /* We maintain a trivial check sum in our on disk activity log.
32b411b363SPhilipp Reisner  * With that we can ensure correct operation even when the storage
33b411b363SPhilipp Reisner  * device might do a partial (last) sector write while loosing power.
34b411b363SPhilipp Reisner  */
35b411b363SPhilipp Reisner struct __packed al_transaction {
36b411b363SPhilipp Reisner 	u32       magic;
37b411b363SPhilipp Reisner 	u32       tr_number;
38b411b363SPhilipp Reisner 	struct __packed {
39b411b363SPhilipp Reisner 		u32 pos;
40b411b363SPhilipp Reisner 		u32 extent; } updates[1 + AL_EXTENTS_PT];
41b411b363SPhilipp Reisner 	u32       xor_sum;
42b411b363SPhilipp Reisner };
43b411b363SPhilipp Reisner 
44b411b363SPhilipp Reisner struct update_odbm_work {
45b411b363SPhilipp Reisner 	struct drbd_work w;
46b411b363SPhilipp Reisner 	unsigned int enr;
47b411b363SPhilipp Reisner };
48b411b363SPhilipp Reisner 
49b411b363SPhilipp Reisner struct update_al_work {
50b411b363SPhilipp Reisner 	struct drbd_work w;
51b411b363SPhilipp Reisner 	struct lc_element *al_ext;
52b411b363SPhilipp Reisner 	struct completion event;
53b411b363SPhilipp Reisner 	unsigned int enr;
54b411b363SPhilipp Reisner 	/* if old_enr != LC_FREE, write corresponding bitmap sector, too */
55b411b363SPhilipp Reisner 	unsigned int old_enr;
56b411b363SPhilipp Reisner };
57b411b363SPhilipp Reisner 
58b411b363SPhilipp Reisner struct drbd_atodb_wait {
59b411b363SPhilipp Reisner 	atomic_t           count;
60b411b363SPhilipp Reisner 	struct completion  io_done;
61b411b363SPhilipp Reisner 	struct drbd_conf   *mdev;
62b411b363SPhilipp Reisner 	int                error;
63b411b363SPhilipp Reisner };
64b411b363SPhilipp Reisner 
65b411b363SPhilipp Reisner 
66b411b363SPhilipp Reisner int w_al_write_transaction(struct drbd_conf *, struct drbd_work *, int);
67b411b363SPhilipp Reisner 
68b411b363SPhilipp Reisner static int _drbd_md_sync_page_io(struct drbd_conf *mdev,
69b411b363SPhilipp Reisner 				 struct drbd_backing_dev *bdev,
70b411b363SPhilipp Reisner 				 struct page *page, sector_t sector,
71b411b363SPhilipp Reisner 				 int rw, int size)
72b411b363SPhilipp Reisner {
73b411b363SPhilipp Reisner 	struct bio *bio;
74b411b363SPhilipp Reisner 	struct drbd_md_io md_io;
75b411b363SPhilipp Reisner 	int ok;
76b411b363SPhilipp Reisner 
77b411b363SPhilipp Reisner 	md_io.mdev = mdev;
78b411b363SPhilipp Reisner 	init_completion(&md_io.event);
79b411b363SPhilipp Reisner 	md_io.error = 0;
80b411b363SPhilipp Reisner 
81b411b363SPhilipp Reisner 	if ((rw & WRITE) && !test_bit(MD_NO_BARRIER, &mdev->flags))
827b6d91daSChristoph Hellwig 		rw |= REQ_HARDBARRIER;
837b6d91daSChristoph Hellwig 	rw |= REQ_UNPLUG | REQ_SYNC;
84b411b363SPhilipp Reisner 
85b411b363SPhilipp Reisner  retry:
86b411b363SPhilipp Reisner 	bio = bio_alloc(GFP_NOIO, 1);
87b411b363SPhilipp Reisner 	bio->bi_bdev = bdev->md_bdev;
88b411b363SPhilipp Reisner 	bio->bi_sector = sector;
89b411b363SPhilipp Reisner 	ok = (bio_add_page(bio, page, size, 0) == size);
90b411b363SPhilipp Reisner 	if (!ok)
91b411b363SPhilipp Reisner 		goto out;
92b411b363SPhilipp Reisner 	bio->bi_private = &md_io;
93b411b363SPhilipp Reisner 	bio->bi_end_io = drbd_md_io_complete;
94b411b363SPhilipp Reisner 	bio->bi_rw = rw;
95b411b363SPhilipp Reisner 
96b411b363SPhilipp Reisner 	if (FAULT_ACTIVE(mdev, (rw & WRITE) ? DRBD_FAULT_MD_WR : DRBD_FAULT_MD_RD))
97b411b363SPhilipp Reisner 		bio_endio(bio, -EIO);
98b411b363SPhilipp Reisner 	else
99b411b363SPhilipp Reisner 		submit_bio(rw, bio);
100b411b363SPhilipp Reisner 	wait_for_completion(&md_io.event);
101b411b363SPhilipp Reisner 	ok = bio_flagged(bio, BIO_UPTODATE) && md_io.error == 0;
102b411b363SPhilipp Reisner 
103b411b363SPhilipp Reisner 	/* check for unsupported barrier op.
104b411b363SPhilipp Reisner 	 * would rather check on EOPNOTSUPP, but that is not reliable.
105b411b363SPhilipp Reisner 	 * don't try again for ANY return value != 0 */
1067b6d91daSChristoph Hellwig 	if (unlikely((bio->bi_rw & REQ_HARDBARRIER) && !ok)) {
107b411b363SPhilipp Reisner 		/* Try again with no barrier */
108b411b363SPhilipp Reisner 		dev_warn(DEV, "Barriers not supported on meta data device - disabling\n");
109b411b363SPhilipp Reisner 		set_bit(MD_NO_BARRIER, &mdev->flags);
1107b6d91daSChristoph Hellwig 		rw &= ~REQ_HARDBARRIER;
111b411b363SPhilipp Reisner 		bio_put(bio);
112b411b363SPhilipp Reisner 		goto retry;
113b411b363SPhilipp Reisner 	}
114b411b363SPhilipp Reisner  out:
115b411b363SPhilipp Reisner 	bio_put(bio);
116b411b363SPhilipp Reisner 	return ok;
117b411b363SPhilipp Reisner }
118b411b363SPhilipp Reisner 
119b411b363SPhilipp Reisner int drbd_md_sync_page_io(struct drbd_conf *mdev, struct drbd_backing_dev *bdev,
120b411b363SPhilipp Reisner 			 sector_t sector, int rw)
121b411b363SPhilipp Reisner {
122b411b363SPhilipp Reisner 	int logical_block_size, mask, ok;
123b411b363SPhilipp Reisner 	int offset = 0;
124b411b363SPhilipp Reisner 	struct page *iop = mdev->md_io_page;
125b411b363SPhilipp Reisner 
126b411b363SPhilipp Reisner 	D_ASSERT(mutex_is_locked(&mdev->md_io_mutex));
127b411b363SPhilipp Reisner 
128b411b363SPhilipp Reisner 	BUG_ON(!bdev->md_bdev);
129b411b363SPhilipp Reisner 
130b411b363SPhilipp Reisner 	logical_block_size = bdev_logical_block_size(bdev->md_bdev);
131b411b363SPhilipp Reisner 	if (logical_block_size == 0)
132b411b363SPhilipp Reisner 		logical_block_size = MD_SECTOR_SIZE;
133b411b363SPhilipp Reisner 
134b411b363SPhilipp Reisner 	/* in case logical_block_size != 512 [ s390 only? ] */
135b411b363SPhilipp Reisner 	if (logical_block_size != MD_SECTOR_SIZE) {
136b411b363SPhilipp Reisner 		mask = (logical_block_size / MD_SECTOR_SIZE) - 1;
137b411b363SPhilipp Reisner 		D_ASSERT(mask == 1 || mask == 3 || mask == 7);
138b411b363SPhilipp Reisner 		D_ASSERT(logical_block_size == (mask+1) * MD_SECTOR_SIZE);
139b411b363SPhilipp Reisner 		offset = sector & mask;
140b411b363SPhilipp Reisner 		sector = sector & ~mask;
141b411b363SPhilipp Reisner 		iop = mdev->md_io_tmpp;
142b411b363SPhilipp Reisner 
143b411b363SPhilipp Reisner 		if (rw & WRITE) {
144b411b363SPhilipp Reisner 			/* these are GFP_KERNEL pages, pre-allocated
145b411b363SPhilipp Reisner 			 * on device initialization */
146b411b363SPhilipp Reisner 			void *p = page_address(mdev->md_io_page);
147b411b363SPhilipp Reisner 			void *hp = page_address(mdev->md_io_tmpp);
148b411b363SPhilipp Reisner 
149b411b363SPhilipp Reisner 			ok = _drbd_md_sync_page_io(mdev, bdev, iop, sector,
150b411b363SPhilipp Reisner 					READ, logical_block_size);
151b411b363SPhilipp Reisner 
152b411b363SPhilipp Reisner 			if (unlikely(!ok)) {
153b411b363SPhilipp Reisner 				dev_err(DEV, "drbd_md_sync_page_io(,%llus,"
154b411b363SPhilipp Reisner 				    "READ [logical_block_size!=512]) failed!\n",
155b411b363SPhilipp Reisner 				    (unsigned long long)sector);
156b411b363SPhilipp Reisner 				return 0;
157b411b363SPhilipp Reisner 			}
158b411b363SPhilipp Reisner 
159b411b363SPhilipp Reisner 			memcpy(hp + offset*MD_SECTOR_SIZE, p, MD_SECTOR_SIZE);
160b411b363SPhilipp Reisner 		}
161b411b363SPhilipp Reisner 	}
162b411b363SPhilipp Reisner 
163b411b363SPhilipp Reisner 	if (sector < drbd_md_first_sector(bdev) ||
164b411b363SPhilipp Reisner 	    sector > drbd_md_last_sector(bdev))
165b411b363SPhilipp Reisner 		dev_alert(DEV, "%s [%d]:%s(,%llus,%s) out of range md access!\n",
166b411b363SPhilipp Reisner 		     current->comm, current->pid, __func__,
167b411b363SPhilipp Reisner 		     (unsigned long long)sector, (rw & WRITE) ? "WRITE" : "READ");
168b411b363SPhilipp Reisner 
169b411b363SPhilipp Reisner 	ok = _drbd_md_sync_page_io(mdev, bdev, iop, sector, rw, logical_block_size);
170b411b363SPhilipp Reisner 	if (unlikely(!ok)) {
171b411b363SPhilipp Reisner 		dev_err(DEV, "drbd_md_sync_page_io(,%llus,%s) failed!\n",
172b411b363SPhilipp Reisner 		    (unsigned long long)sector, (rw & WRITE) ? "WRITE" : "READ");
173b411b363SPhilipp Reisner 		return 0;
174b411b363SPhilipp Reisner 	}
175b411b363SPhilipp Reisner 
176b411b363SPhilipp Reisner 	if (logical_block_size != MD_SECTOR_SIZE && !(rw & WRITE)) {
177b411b363SPhilipp Reisner 		void *p = page_address(mdev->md_io_page);
178b411b363SPhilipp Reisner 		void *hp = page_address(mdev->md_io_tmpp);
179b411b363SPhilipp Reisner 
180b411b363SPhilipp Reisner 		memcpy(p, hp + offset*MD_SECTOR_SIZE, MD_SECTOR_SIZE);
181b411b363SPhilipp Reisner 	}
182b411b363SPhilipp Reisner 
183b411b363SPhilipp Reisner 	return ok;
184b411b363SPhilipp Reisner }
185b411b363SPhilipp Reisner 
186b411b363SPhilipp Reisner static struct lc_element *_al_get(struct drbd_conf *mdev, unsigned int enr)
187b411b363SPhilipp Reisner {
188b411b363SPhilipp Reisner 	struct lc_element *al_ext;
189b411b363SPhilipp Reisner 	struct lc_element *tmp;
190b411b363SPhilipp Reisner 	unsigned long     al_flags = 0;
191b411b363SPhilipp Reisner 
192b411b363SPhilipp Reisner 	spin_lock_irq(&mdev->al_lock);
193b411b363SPhilipp Reisner 	tmp = lc_find(mdev->resync, enr/AL_EXT_PER_BM_SECT);
194b411b363SPhilipp Reisner 	if (unlikely(tmp != NULL)) {
195b411b363SPhilipp Reisner 		struct bm_extent  *bm_ext = lc_entry(tmp, struct bm_extent, lce);
196b411b363SPhilipp Reisner 		if (test_bit(BME_NO_WRITES, &bm_ext->flags)) {
197b411b363SPhilipp Reisner 			spin_unlock_irq(&mdev->al_lock);
198b411b363SPhilipp Reisner 			return NULL;
199b411b363SPhilipp Reisner 		}
200b411b363SPhilipp Reisner 	}
201b411b363SPhilipp Reisner 	al_ext   = lc_get(mdev->act_log, enr);
202b411b363SPhilipp Reisner 	al_flags = mdev->act_log->flags;
203b411b363SPhilipp Reisner 	spin_unlock_irq(&mdev->al_lock);
204b411b363SPhilipp Reisner 
205b411b363SPhilipp Reisner 	/*
206b411b363SPhilipp Reisner 	if (!al_ext) {
207b411b363SPhilipp Reisner 		if (al_flags & LC_STARVING)
208b411b363SPhilipp Reisner 			dev_warn(DEV, "Have to wait for LRU element (AL too small?)\n");
209b411b363SPhilipp Reisner 		if (al_flags & LC_DIRTY)
210b411b363SPhilipp Reisner 			dev_warn(DEV, "Ongoing AL update (AL device too slow?)\n");
211b411b363SPhilipp Reisner 	}
212b411b363SPhilipp Reisner 	*/
213b411b363SPhilipp Reisner 
214b411b363SPhilipp Reisner 	return al_ext;
215b411b363SPhilipp Reisner }
216b411b363SPhilipp Reisner 
217b411b363SPhilipp Reisner void drbd_al_begin_io(struct drbd_conf *mdev, sector_t sector)
218b411b363SPhilipp Reisner {
219b411b363SPhilipp Reisner 	unsigned int enr = (sector >> (AL_EXTENT_SHIFT-9));
220b411b363SPhilipp Reisner 	struct lc_element *al_ext;
221b411b363SPhilipp Reisner 	struct update_al_work al_work;
222b411b363SPhilipp Reisner 
223b411b363SPhilipp Reisner 	D_ASSERT(atomic_read(&mdev->local_cnt) > 0);
224b411b363SPhilipp Reisner 
225b411b363SPhilipp Reisner 	wait_event(mdev->al_wait, (al_ext = _al_get(mdev, enr)));
226b411b363SPhilipp Reisner 
227b411b363SPhilipp Reisner 	if (al_ext->lc_number != enr) {
228b411b363SPhilipp Reisner 		/* drbd_al_write_transaction(mdev,al_ext,enr);
229b411b363SPhilipp Reisner 		 * recurses into generic_make_request(), which
230b411b363SPhilipp Reisner 		 * disallows recursion, bios being serialized on the
231b411b363SPhilipp Reisner 		 * current->bio_tail list now.
232b411b363SPhilipp Reisner 		 * we have to delegate updates to the activity log
233b411b363SPhilipp Reisner 		 * to the worker thread. */
234b411b363SPhilipp Reisner 		init_completion(&al_work.event);
235b411b363SPhilipp Reisner 		al_work.al_ext = al_ext;
236b411b363SPhilipp Reisner 		al_work.enr = enr;
237b411b363SPhilipp Reisner 		al_work.old_enr = al_ext->lc_number;
238b411b363SPhilipp Reisner 		al_work.w.cb = w_al_write_transaction;
239b411b363SPhilipp Reisner 		drbd_queue_work_front(&mdev->data.work, &al_work.w);
240b411b363SPhilipp Reisner 		wait_for_completion(&al_work.event);
241b411b363SPhilipp Reisner 
242b411b363SPhilipp Reisner 		mdev->al_writ_cnt++;
243b411b363SPhilipp Reisner 
244b411b363SPhilipp Reisner 		spin_lock_irq(&mdev->al_lock);
245b411b363SPhilipp Reisner 		lc_changed(mdev->act_log, al_ext);
246b411b363SPhilipp Reisner 		spin_unlock_irq(&mdev->al_lock);
247b411b363SPhilipp Reisner 		wake_up(&mdev->al_wait);
248b411b363SPhilipp Reisner 	}
249b411b363SPhilipp Reisner }
250b411b363SPhilipp Reisner 
251b411b363SPhilipp Reisner void drbd_al_complete_io(struct drbd_conf *mdev, sector_t sector)
252b411b363SPhilipp Reisner {
253b411b363SPhilipp Reisner 	unsigned int enr = (sector >> (AL_EXTENT_SHIFT-9));
254b411b363SPhilipp Reisner 	struct lc_element *extent;
255b411b363SPhilipp Reisner 	unsigned long flags;
256b411b363SPhilipp Reisner 
257b411b363SPhilipp Reisner 	spin_lock_irqsave(&mdev->al_lock, flags);
258b411b363SPhilipp Reisner 
259b411b363SPhilipp Reisner 	extent = lc_find(mdev->act_log, enr);
260b411b363SPhilipp Reisner 
261b411b363SPhilipp Reisner 	if (!extent) {
262b411b363SPhilipp Reisner 		spin_unlock_irqrestore(&mdev->al_lock, flags);
263b411b363SPhilipp Reisner 		dev_err(DEV, "al_complete_io() called on inactive extent %u\n", enr);
264b411b363SPhilipp Reisner 		return;
265b411b363SPhilipp Reisner 	}
266b411b363SPhilipp Reisner 
267b411b363SPhilipp Reisner 	if (lc_put(mdev->act_log, extent) == 0)
268b411b363SPhilipp Reisner 		wake_up(&mdev->al_wait);
269b411b363SPhilipp Reisner 
270b411b363SPhilipp Reisner 	spin_unlock_irqrestore(&mdev->al_lock, flags);
271b411b363SPhilipp Reisner }
272b411b363SPhilipp Reisner 
273b411b363SPhilipp Reisner int
274b411b363SPhilipp Reisner w_al_write_transaction(struct drbd_conf *mdev, struct drbd_work *w, int unused)
275b411b363SPhilipp Reisner {
276b411b363SPhilipp Reisner 	struct update_al_work *aw = container_of(w, struct update_al_work, w);
277b411b363SPhilipp Reisner 	struct lc_element *updated = aw->al_ext;
278b411b363SPhilipp Reisner 	const unsigned int new_enr = aw->enr;
279b411b363SPhilipp Reisner 	const unsigned int evicted = aw->old_enr;
280b411b363SPhilipp Reisner 	struct al_transaction *buffer;
281b411b363SPhilipp Reisner 	sector_t sector;
282b411b363SPhilipp Reisner 	int i, n, mx;
283b411b363SPhilipp Reisner 	unsigned int extent_nr;
284b411b363SPhilipp Reisner 	u32 xor_sum = 0;
285b411b363SPhilipp Reisner 
286b411b363SPhilipp Reisner 	if (!get_ldev(mdev)) {
287*6719fb03SLars Ellenberg 		dev_err(DEV,
288*6719fb03SLars Ellenberg 			"disk is %s, cannot start al transaction (-%d +%d)\n",
289*6719fb03SLars Ellenberg 			drbd_disk_str(mdev->state.disk), evicted, new_enr);
290b411b363SPhilipp Reisner 		complete(&((struct update_al_work *)w)->event);
291b411b363SPhilipp Reisner 		return 1;
292b411b363SPhilipp Reisner 	}
293b411b363SPhilipp Reisner 	/* do we have to do a bitmap write, first?
294b411b363SPhilipp Reisner 	 * TODO reduce maximum latency:
295b411b363SPhilipp Reisner 	 * submit both bios, then wait for both,
296*6719fb03SLars Ellenberg 	 * instead of doing two synchronous sector writes.
297*6719fb03SLars Ellenberg 	 * For now, we must not write the transaction,
298*6719fb03SLars Ellenberg 	 * if we cannot write out the bitmap of the evicted extent. */
299b411b363SPhilipp Reisner 	if (mdev->state.conn < C_CONNECTED && evicted != LC_FREE)
300b411b363SPhilipp Reisner 		drbd_bm_write_sect(mdev, evicted/AL_EXT_PER_BM_SECT);
301b411b363SPhilipp Reisner 
302*6719fb03SLars Ellenberg 	/* The bitmap write may have failed, causing a state change. */
303*6719fb03SLars Ellenberg 	if (mdev->state.disk < D_INCONSISTENT) {
304*6719fb03SLars Ellenberg 		dev_err(DEV,
305*6719fb03SLars Ellenberg 			"disk is %s, cannot write al transaction (-%d +%d)\n",
306*6719fb03SLars Ellenberg 			drbd_disk_str(mdev->state.disk), evicted, new_enr);
307*6719fb03SLars Ellenberg 		complete(&((struct update_al_work *)w)->event);
308*6719fb03SLars Ellenberg 		put_ldev(mdev);
309*6719fb03SLars Ellenberg 		return 1;
310*6719fb03SLars Ellenberg 	}
311*6719fb03SLars Ellenberg 
312*6719fb03SLars Ellenberg 	mutex_lock(&mdev->md_io_mutex); /* protects md_io_buffer, al_tr_cycle, ... */
313b411b363SPhilipp Reisner 	buffer = (struct al_transaction *)page_address(mdev->md_io_page);
314b411b363SPhilipp Reisner 
315b411b363SPhilipp Reisner 	buffer->magic = __constant_cpu_to_be32(DRBD_MAGIC);
316b411b363SPhilipp Reisner 	buffer->tr_number = cpu_to_be32(mdev->al_tr_number);
317b411b363SPhilipp Reisner 
318b411b363SPhilipp Reisner 	n = lc_index_of(mdev->act_log, updated);
319b411b363SPhilipp Reisner 
320b411b363SPhilipp Reisner 	buffer->updates[0].pos = cpu_to_be32(n);
321b411b363SPhilipp Reisner 	buffer->updates[0].extent = cpu_to_be32(new_enr);
322b411b363SPhilipp Reisner 
323b411b363SPhilipp Reisner 	xor_sum ^= new_enr;
324b411b363SPhilipp Reisner 
325b411b363SPhilipp Reisner 	mx = min_t(int, AL_EXTENTS_PT,
326b411b363SPhilipp Reisner 		   mdev->act_log->nr_elements - mdev->al_tr_cycle);
327b411b363SPhilipp Reisner 	for (i = 0; i < mx; i++) {
328b411b363SPhilipp Reisner 		unsigned idx = mdev->al_tr_cycle + i;
329b411b363SPhilipp Reisner 		extent_nr = lc_element_by_index(mdev->act_log, idx)->lc_number;
330b411b363SPhilipp Reisner 		buffer->updates[i+1].pos = cpu_to_be32(idx);
331b411b363SPhilipp Reisner 		buffer->updates[i+1].extent = cpu_to_be32(extent_nr);
332b411b363SPhilipp Reisner 		xor_sum ^= extent_nr;
333b411b363SPhilipp Reisner 	}
334b411b363SPhilipp Reisner 	for (; i < AL_EXTENTS_PT; i++) {
335b411b363SPhilipp Reisner 		buffer->updates[i+1].pos = __constant_cpu_to_be32(-1);
336b411b363SPhilipp Reisner 		buffer->updates[i+1].extent = __constant_cpu_to_be32(LC_FREE);
337b411b363SPhilipp Reisner 		xor_sum ^= LC_FREE;
338b411b363SPhilipp Reisner 	}
339b411b363SPhilipp Reisner 	mdev->al_tr_cycle += AL_EXTENTS_PT;
340b411b363SPhilipp Reisner 	if (mdev->al_tr_cycle >= mdev->act_log->nr_elements)
341b411b363SPhilipp Reisner 		mdev->al_tr_cycle = 0;
342b411b363SPhilipp Reisner 
343b411b363SPhilipp Reisner 	buffer->xor_sum = cpu_to_be32(xor_sum);
344b411b363SPhilipp Reisner 
345b411b363SPhilipp Reisner 	sector =  mdev->ldev->md.md_offset
346b411b363SPhilipp Reisner 		+ mdev->ldev->md.al_offset + mdev->al_tr_pos;
347b411b363SPhilipp Reisner 
348b411b363SPhilipp Reisner 	if (!drbd_md_sync_page_io(mdev, mdev->ldev, sector, WRITE))
349b411b363SPhilipp Reisner 		drbd_chk_io_error(mdev, 1, TRUE);
350b411b363SPhilipp Reisner 
351b411b363SPhilipp Reisner 	if (++mdev->al_tr_pos >
352b411b363SPhilipp Reisner 	    div_ceil(mdev->act_log->nr_elements, AL_EXTENTS_PT))
353b411b363SPhilipp Reisner 		mdev->al_tr_pos = 0;
354b411b363SPhilipp Reisner 
355b411b363SPhilipp Reisner 	D_ASSERT(mdev->al_tr_pos < MD_AL_MAX_SIZE);
356b411b363SPhilipp Reisner 	mdev->al_tr_number++;
357b411b363SPhilipp Reisner 
358b411b363SPhilipp Reisner 	mutex_unlock(&mdev->md_io_mutex);
359b411b363SPhilipp Reisner 
360b411b363SPhilipp Reisner 	complete(&((struct update_al_work *)w)->event);
361b411b363SPhilipp Reisner 	put_ldev(mdev);
362b411b363SPhilipp Reisner 
363b411b363SPhilipp Reisner 	return 1;
364b411b363SPhilipp Reisner }
365b411b363SPhilipp Reisner 
366b411b363SPhilipp Reisner /**
367b411b363SPhilipp Reisner  * drbd_al_read_tr() - Read a single transaction from the on disk activity log
368b411b363SPhilipp Reisner  * @mdev:	DRBD device.
369b411b363SPhilipp Reisner  * @bdev:	Block device to read form.
370b411b363SPhilipp Reisner  * @b:		pointer to an al_transaction.
371b411b363SPhilipp Reisner  * @index:	On disk slot of the transaction to read.
372b411b363SPhilipp Reisner  *
373b411b363SPhilipp Reisner  * Returns -1 on IO error, 0 on checksum error and 1 upon success.
374b411b363SPhilipp Reisner  */
375b411b363SPhilipp Reisner static int drbd_al_read_tr(struct drbd_conf *mdev,
376b411b363SPhilipp Reisner 			   struct drbd_backing_dev *bdev,
377b411b363SPhilipp Reisner 			   struct al_transaction *b,
378b411b363SPhilipp Reisner 			   int index)
379b411b363SPhilipp Reisner {
380b411b363SPhilipp Reisner 	sector_t sector;
381b411b363SPhilipp Reisner 	int rv, i;
382b411b363SPhilipp Reisner 	u32 xor_sum = 0;
383b411b363SPhilipp Reisner 
384b411b363SPhilipp Reisner 	sector = bdev->md.md_offset + bdev->md.al_offset + index;
385b411b363SPhilipp Reisner 
386b411b363SPhilipp Reisner 	/* Dont process error normally,
387b411b363SPhilipp Reisner 	 * as this is done before disk is attached! */
388b411b363SPhilipp Reisner 	if (!drbd_md_sync_page_io(mdev, bdev, sector, READ))
389b411b363SPhilipp Reisner 		return -1;
390b411b363SPhilipp Reisner 
391b411b363SPhilipp Reisner 	rv = (be32_to_cpu(b->magic) == DRBD_MAGIC);
392b411b363SPhilipp Reisner 
393b411b363SPhilipp Reisner 	for (i = 0; i < AL_EXTENTS_PT + 1; i++)
394b411b363SPhilipp Reisner 		xor_sum ^= be32_to_cpu(b->updates[i].extent);
395b411b363SPhilipp Reisner 	rv &= (xor_sum == be32_to_cpu(b->xor_sum));
396b411b363SPhilipp Reisner 
397b411b363SPhilipp Reisner 	return rv;
398b411b363SPhilipp Reisner }
399b411b363SPhilipp Reisner 
400b411b363SPhilipp Reisner /**
401b411b363SPhilipp Reisner  * drbd_al_read_log() - Restores the activity log from its on disk representation.
402b411b363SPhilipp Reisner  * @mdev:	DRBD device.
403b411b363SPhilipp Reisner  * @bdev:	Block device to read form.
404b411b363SPhilipp Reisner  *
405b411b363SPhilipp Reisner  * Returns 1 on success, returns 0 when reading the log failed due to IO errors.
406b411b363SPhilipp Reisner  */
407b411b363SPhilipp Reisner int drbd_al_read_log(struct drbd_conf *mdev, struct drbd_backing_dev *bdev)
408b411b363SPhilipp Reisner {
409b411b363SPhilipp Reisner 	struct al_transaction *buffer;
410b411b363SPhilipp Reisner 	int i;
411b411b363SPhilipp Reisner 	int rv;
412b411b363SPhilipp Reisner 	int mx;
413b411b363SPhilipp Reisner 	int active_extents = 0;
414b411b363SPhilipp Reisner 	int transactions = 0;
415b411b363SPhilipp Reisner 	int found_valid = 0;
416b411b363SPhilipp Reisner 	int from = 0;
417b411b363SPhilipp Reisner 	int to = 0;
418b411b363SPhilipp Reisner 	u32 from_tnr = 0;
419b411b363SPhilipp Reisner 	u32 to_tnr = 0;
420b411b363SPhilipp Reisner 	u32 cnr;
421b411b363SPhilipp Reisner 
422b411b363SPhilipp Reisner 	mx = div_ceil(mdev->act_log->nr_elements, AL_EXTENTS_PT);
423b411b363SPhilipp Reisner 
424b411b363SPhilipp Reisner 	/* lock out all other meta data io for now,
425b411b363SPhilipp Reisner 	 * and make sure the page is mapped.
426b411b363SPhilipp Reisner 	 */
427b411b363SPhilipp Reisner 	mutex_lock(&mdev->md_io_mutex);
428b411b363SPhilipp Reisner 	buffer = page_address(mdev->md_io_page);
429b411b363SPhilipp Reisner 
430b411b363SPhilipp Reisner 	/* Find the valid transaction in the log */
431b411b363SPhilipp Reisner 	for (i = 0; i <= mx; i++) {
432b411b363SPhilipp Reisner 		rv = drbd_al_read_tr(mdev, bdev, buffer, i);
433b411b363SPhilipp Reisner 		if (rv == 0)
434b411b363SPhilipp Reisner 			continue;
435b411b363SPhilipp Reisner 		if (rv == -1) {
436b411b363SPhilipp Reisner 			mutex_unlock(&mdev->md_io_mutex);
437b411b363SPhilipp Reisner 			return 0;
438b411b363SPhilipp Reisner 		}
439b411b363SPhilipp Reisner 		cnr = be32_to_cpu(buffer->tr_number);
440b411b363SPhilipp Reisner 
441b411b363SPhilipp Reisner 		if (++found_valid == 1) {
442b411b363SPhilipp Reisner 			from = i;
443b411b363SPhilipp Reisner 			to = i;
444b411b363SPhilipp Reisner 			from_tnr = cnr;
445b411b363SPhilipp Reisner 			to_tnr = cnr;
446b411b363SPhilipp Reisner 			continue;
447b411b363SPhilipp Reisner 		}
448b411b363SPhilipp Reisner 		if ((int)cnr - (int)from_tnr < 0) {
449b411b363SPhilipp Reisner 			D_ASSERT(from_tnr - cnr + i - from == mx+1);
450b411b363SPhilipp Reisner 			from = i;
451b411b363SPhilipp Reisner 			from_tnr = cnr;
452b411b363SPhilipp Reisner 		}
453b411b363SPhilipp Reisner 		if ((int)cnr - (int)to_tnr > 0) {
454b411b363SPhilipp Reisner 			D_ASSERT(cnr - to_tnr == i - to);
455b411b363SPhilipp Reisner 			to = i;
456b411b363SPhilipp Reisner 			to_tnr = cnr;
457b411b363SPhilipp Reisner 		}
458b411b363SPhilipp Reisner 	}
459b411b363SPhilipp Reisner 
460b411b363SPhilipp Reisner 	if (!found_valid) {
461b411b363SPhilipp Reisner 		dev_warn(DEV, "No usable activity log found.\n");
462b411b363SPhilipp Reisner 		mutex_unlock(&mdev->md_io_mutex);
463b411b363SPhilipp Reisner 		return 1;
464b411b363SPhilipp Reisner 	}
465b411b363SPhilipp Reisner 
466b411b363SPhilipp Reisner 	/* Read the valid transactions.
467b411b363SPhilipp Reisner 	 * dev_info(DEV, "Reading from %d to %d.\n",from,to); */
468b411b363SPhilipp Reisner 	i = from;
469b411b363SPhilipp Reisner 	while (1) {
470b411b363SPhilipp Reisner 		int j, pos;
471b411b363SPhilipp Reisner 		unsigned int extent_nr;
472b411b363SPhilipp Reisner 		unsigned int trn;
473b411b363SPhilipp Reisner 
474b411b363SPhilipp Reisner 		rv = drbd_al_read_tr(mdev, bdev, buffer, i);
475b411b363SPhilipp Reisner 		ERR_IF(rv == 0) goto cancel;
476b411b363SPhilipp Reisner 		if (rv == -1) {
477b411b363SPhilipp Reisner 			mutex_unlock(&mdev->md_io_mutex);
478b411b363SPhilipp Reisner 			return 0;
479b411b363SPhilipp Reisner 		}
480b411b363SPhilipp Reisner 
481b411b363SPhilipp Reisner 		trn = be32_to_cpu(buffer->tr_number);
482b411b363SPhilipp Reisner 
483b411b363SPhilipp Reisner 		spin_lock_irq(&mdev->al_lock);
484b411b363SPhilipp Reisner 
485b411b363SPhilipp Reisner 		/* This loop runs backwards because in the cyclic
486b411b363SPhilipp Reisner 		   elements there might be an old version of the
487b411b363SPhilipp Reisner 		   updated element (in slot 0). So the element in slot 0
488b411b363SPhilipp Reisner 		   can overwrite old versions. */
489b411b363SPhilipp Reisner 		for (j = AL_EXTENTS_PT; j >= 0; j--) {
490b411b363SPhilipp Reisner 			pos = be32_to_cpu(buffer->updates[j].pos);
491b411b363SPhilipp Reisner 			extent_nr = be32_to_cpu(buffer->updates[j].extent);
492b411b363SPhilipp Reisner 
493b411b363SPhilipp Reisner 			if (extent_nr == LC_FREE)
494b411b363SPhilipp Reisner 				continue;
495b411b363SPhilipp Reisner 
496b411b363SPhilipp Reisner 			lc_set(mdev->act_log, extent_nr, pos);
497b411b363SPhilipp Reisner 			active_extents++;
498b411b363SPhilipp Reisner 		}
499b411b363SPhilipp Reisner 		spin_unlock_irq(&mdev->al_lock);
500b411b363SPhilipp Reisner 
501b411b363SPhilipp Reisner 		transactions++;
502b411b363SPhilipp Reisner 
503b411b363SPhilipp Reisner cancel:
504b411b363SPhilipp Reisner 		if (i == to)
505b411b363SPhilipp Reisner 			break;
506b411b363SPhilipp Reisner 		i++;
507b411b363SPhilipp Reisner 		if (i > mx)
508b411b363SPhilipp Reisner 			i = 0;
509b411b363SPhilipp Reisner 	}
510b411b363SPhilipp Reisner 
511b411b363SPhilipp Reisner 	mdev->al_tr_number = to_tnr+1;
512b411b363SPhilipp Reisner 	mdev->al_tr_pos = to;
513b411b363SPhilipp Reisner 	if (++mdev->al_tr_pos >
514b411b363SPhilipp Reisner 	    div_ceil(mdev->act_log->nr_elements, AL_EXTENTS_PT))
515b411b363SPhilipp Reisner 		mdev->al_tr_pos = 0;
516b411b363SPhilipp Reisner 
517b411b363SPhilipp Reisner 	/* ok, we are done with it */
518b411b363SPhilipp Reisner 	mutex_unlock(&mdev->md_io_mutex);
519b411b363SPhilipp Reisner 
520b411b363SPhilipp Reisner 	dev_info(DEV, "Found %d transactions (%d active extents) in activity log.\n",
521b411b363SPhilipp Reisner 	     transactions, active_extents);
522b411b363SPhilipp Reisner 
523b411b363SPhilipp Reisner 	return 1;
524b411b363SPhilipp Reisner }
525b411b363SPhilipp Reisner 
526b411b363SPhilipp Reisner static void atodb_endio(struct bio *bio, int error)
527b411b363SPhilipp Reisner {
528b411b363SPhilipp Reisner 	struct drbd_atodb_wait *wc = bio->bi_private;
529b411b363SPhilipp Reisner 	struct drbd_conf *mdev = wc->mdev;
530b411b363SPhilipp Reisner 	struct page *page;
531b411b363SPhilipp Reisner 	int uptodate = bio_flagged(bio, BIO_UPTODATE);
532b411b363SPhilipp Reisner 
533b411b363SPhilipp Reisner 	/* strange behavior of some lower level drivers...
534b411b363SPhilipp Reisner 	 * fail the request by clearing the uptodate flag,
535b411b363SPhilipp Reisner 	 * but do not return any error?! */
536b411b363SPhilipp Reisner 	if (!error && !uptodate)
537b411b363SPhilipp Reisner 		error = -EIO;
538b411b363SPhilipp Reisner 
539b411b363SPhilipp Reisner 	drbd_chk_io_error(mdev, error, TRUE);
540b411b363SPhilipp Reisner 	if (error && wc->error == 0)
541b411b363SPhilipp Reisner 		wc->error = error;
542b411b363SPhilipp Reisner 
543b411b363SPhilipp Reisner 	if (atomic_dec_and_test(&wc->count))
544b411b363SPhilipp Reisner 		complete(&wc->io_done);
545b411b363SPhilipp Reisner 
546b411b363SPhilipp Reisner 	page = bio->bi_io_vec[0].bv_page;
547b411b363SPhilipp Reisner 	put_page(page);
548b411b363SPhilipp Reisner 	bio_put(bio);
549b411b363SPhilipp Reisner 	mdev->bm_writ_cnt++;
550b411b363SPhilipp Reisner 	put_ldev(mdev);
551b411b363SPhilipp Reisner }
552b411b363SPhilipp Reisner 
55339ad2bbbSLars Ellenberg /* sector to word */
554b411b363SPhilipp Reisner #define S2W(s)	((s)<<(BM_EXT_SHIFT-BM_BLOCK_SHIFT-LN2_BPL))
55539ad2bbbSLars Ellenberg 
556b411b363SPhilipp Reisner /* activity log to on disk bitmap -- prepare bio unless that sector
557b411b363SPhilipp Reisner  * is already covered by previously prepared bios */
558b411b363SPhilipp Reisner static int atodb_prepare_unless_covered(struct drbd_conf *mdev,
559b411b363SPhilipp Reisner 					struct bio **bios,
560b411b363SPhilipp Reisner 					unsigned int enr,
561b411b363SPhilipp Reisner 					struct drbd_atodb_wait *wc) __must_hold(local)
562b411b363SPhilipp Reisner {
563b411b363SPhilipp Reisner 	struct bio *bio;
564b411b363SPhilipp Reisner 	struct page *page;
56539ad2bbbSLars Ellenberg 	sector_t on_disk_sector;
566b411b363SPhilipp Reisner 	unsigned int page_offset = PAGE_SIZE;
567b411b363SPhilipp Reisner 	int offset;
568b411b363SPhilipp Reisner 	int i = 0;
569b411b363SPhilipp Reisner 	int err = -ENOMEM;
570b411b363SPhilipp Reisner 
57139ad2bbbSLars Ellenberg 	/* We always write aligned, full 4k blocks,
57239ad2bbbSLars Ellenberg 	 * so we can ignore the logical_block_size (for now) */
57339ad2bbbSLars Ellenberg 	enr &= ~7U;
57439ad2bbbSLars Ellenberg 	on_disk_sector = enr + mdev->ldev->md.md_offset
57539ad2bbbSLars Ellenberg 			     + mdev->ldev->md.bm_offset;
57639ad2bbbSLars Ellenberg 
57739ad2bbbSLars Ellenberg 	D_ASSERT(!(on_disk_sector & 7U));
57839ad2bbbSLars Ellenberg 
579b411b363SPhilipp Reisner 	/* Check if that enr is already covered by an already created bio.
580b411b363SPhilipp Reisner 	 * Caution, bios[] is not NULL terminated,
581b411b363SPhilipp Reisner 	 * but only initialized to all NULL.
582b411b363SPhilipp Reisner 	 * For completely scattered activity log,
583b411b363SPhilipp Reisner 	 * the last invocation iterates over all bios,
584b411b363SPhilipp Reisner 	 * and finds the last NULL entry.
585b411b363SPhilipp Reisner 	 */
586b411b363SPhilipp Reisner 	while ((bio = bios[i])) {
587b411b363SPhilipp Reisner 		if (bio->bi_sector == on_disk_sector)
588b411b363SPhilipp Reisner 			return 0;
589b411b363SPhilipp Reisner 		i++;
590b411b363SPhilipp Reisner 	}
591b411b363SPhilipp Reisner 	/* bios[i] == NULL, the next not yet used slot */
592b411b363SPhilipp Reisner 
593b411b363SPhilipp Reisner 	/* GFP_KERNEL, we are not in the write-out path */
594b411b363SPhilipp Reisner 	bio = bio_alloc(GFP_KERNEL, 1);
595b411b363SPhilipp Reisner 	if (bio == NULL)
596b411b363SPhilipp Reisner 		return -ENOMEM;
597b411b363SPhilipp Reisner 
598b411b363SPhilipp Reisner 	if (i > 0) {
599b411b363SPhilipp Reisner 		const struct bio_vec *prev_bv = bios[i-1]->bi_io_vec;
600b411b363SPhilipp Reisner 		page_offset = prev_bv->bv_offset + prev_bv->bv_len;
601b411b363SPhilipp Reisner 		page = prev_bv->bv_page;
602b411b363SPhilipp Reisner 	}
603b411b363SPhilipp Reisner 	if (page_offset == PAGE_SIZE) {
604b411b363SPhilipp Reisner 		page = alloc_page(__GFP_HIGHMEM);
605b411b363SPhilipp Reisner 		if (page == NULL)
606b411b363SPhilipp Reisner 			goto out_bio_put;
607b411b363SPhilipp Reisner 		page_offset = 0;
608b411b363SPhilipp Reisner 	} else {
609b411b363SPhilipp Reisner 		get_page(page);
610b411b363SPhilipp Reisner 	}
611b411b363SPhilipp Reisner 
612b411b363SPhilipp Reisner 	offset = S2W(enr);
613b411b363SPhilipp Reisner 	drbd_bm_get_lel(mdev, offset,
61439ad2bbbSLars Ellenberg 			min_t(size_t, S2W(8), drbd_bm_words(mdev) - offset),
615b411b363SPhilipp Reisner 			kmap(page) + page_offset);
616b411b363SPhilipp Reisner 	kunmap(page);
617b411b363SPhilipp Reisner 
618b411b363SPhilipp Reisner 	bio->bi_private = wc;
619b411b363SPhilipp Reisner 	bio->bi_end_io = atodb_endio;
620b411b363SPhilipp Reisner 	bio->bi_bdev = mdev->ldev->md_bdev;
621b411b363SPhilipp Reisner 	bio->bi_sector = on_disk_sector;
622b411b363SPhilipp Reisner 
62339ad2bbbSLars Ellenberg 	if (bio_add_page(bio, page, 4096, page_offset) != 4096)
624b411b363SPhilipp Reisner 		goto out_put_page;
625b411b363SPhilipp Reisner 
626b411b363SPhilipp Reisner 	atomic_inc(&wc->count);
627b411b363SPhilipp Reisner 	/* we already know that we may do this...
628b411b363SPhilipp Reisner 	 * get_ldev_if_state(mdev,D_ATTACHING);
629b411b363SPhilipp Reisner 	 * just get the extra reference, so that the local_cnt reflects
630b411b363SPhilipp Reisner 	 * the number of pending IO requests DRBD at its backing device.
631b411b363SPhilipp Reisner 	 */
632b411b363SPhilipp Reisner 	atomic_inc(&mdev->local_cnt);
633b411b363SPhilipp Reisner 
634b411b363SPhilipp Reisner 	bios[i] = bio;
635b411b363SPhilipp Reisner 
636b411b363SPhilipp Reisner 	return 0;
637b411b363SPhilipp Reisner 
638b411b363SPhilipp Reisner out_put_page:
639b411b363SPhilipp Reisner 	err = -EINVAL;
640b411b363SPhilipp Reisner 	put_page(page);
641b411b363SPhilipp Reisner out_bio_put:
642b411b363SPhilipp Reisner 	bio_put(bio);
643b411b363SPhilipp Reisner 	return err;
644b411b363SPhilipp Reisner }
645b411b363SPhilipp Reisner 
646b411b363SPhilipp Reisner /**
647b411b363SPhilipp Reisner  * drbd_al_to_on_disk_bm() -  * Writes bitmap parts covered by active AL extents
648b411b363SPhilipp Reisner  * @mdev:	DRBD device.
649b411b363SPhilipp Reisner  *
650b411b363SPhilipp Reisner  * Called when we detach (unconfigure) local storage,
651b411b363SPhilipp Reisner  * or when we go from R_PRIMARY to R_SECONDARY role.
652b411b363SPhilipp Reisner  */
653b411b363SPhilipp Reisner void drbd_al_to_on_disk_bm(struct drbd_conf *mdev)
654b411b363SPhilipp Reisner {
655b411b363SPhilipp Reisner 	int i, nr_elements;
656b411b363SPhilipp Reisner 	unsigned int enr;
657b411b363SPhilipp Reisner 	struct bio **bios;
658b411b363SPhilipp Reisner 	struct drbd_atodb_wait wc;
659b411b363SPhilipp Reisner 
660b411b363SPhilipp Reisner 	ERR_IF (!get_ldev_if_state(mdev, D_ATTACHING))
661b411b363SPhilipp Reisner 		return; /* sorry, I don't have any act_log etc... */
662b411b363SPhilipp Reisner 
663b411b363SPhilipp Reisner 	wait_event(mdev->al_wait, lc_try_lock(mdev->act_log));
664b411b363SPhilipp Reisner 
665b411b363SPhilipp Reisner 	nr_elements = mdev->act_log->nr_elements;
666b411b363SPhilipp Reisner 
667b411b363SPhilipp Reisner 	/* GFP_KERNEL, we are not in anyone's write-out path */
668b411b363SPhilipp Reisner 	bios = kzalloc(sizeof(struct bio *) * nr_elements, GFP_KERNEL);
669b411b363SPhilipp Reisner 	if (!bios)
670b411b363SPhilipp Reisner 		goto submit_one_by_one;
671b411b363SPhilipp Reisner 
672b411b363SPhilipp Reisner 	atomic_set(&wc.count, 0);
673b411b363SPhilipp Reisner 	init_completion(&wc.io_done);
674b411b363SPhilipp Reisner 	wc.mdev = mdev;
675b411b363SPhilipp Reisner 	wc.error = 0;
676b411b363SPhilipp Reisner 
677b411b363SPhilipp Reisner 	for (i = 0; i < nr_elements; i++) {
678b411b363SPhilipp Reisner 		enr = lc_element_by_index(mdev->act_log, i)->lc_number;
679b411b363SPhilipp Reisner 		if (enr == LC_FREE)
680b411b363SPhilipp Reisner 			continue;
681b411b363SPhilipp Reisner 		/* next statement also does atomic_inc wc.count and local_cnt */
682b411b363SPhilipp Reisner 		if (atodb_prepare_unless_covered(mdev, bios,
683b411b363SPhilipp Reisner 						enr/AL_EXT_PER_BM_SECT,
684b411b363SPhilipp Reisner 						&wc))
685b411b363SPhilipp Reisner 			goto free_bios_submit_one_by_one;
686b411b363SPhilipp Reisner 	}
687b411b363SPhilipp Reisner 
688b411b363SPhilipp Reisner 	/* unnecessary optimization? */
689b411b363SPhilipp Reisner 	lc_unlock(mdev->act_log);
690b411b363SPhilipp Reisner 	wake_up(&mdev->al_wait);
691b411b363SPhilipp Reisner 
692b411b363SPhilipp Reisner 	/* all prepared, submit them */
693b411b363SPhilipp Reisner 	for (i = 0; i < nr_elements; i++) {
694b411b363SPhilipp Reisner 		if (bios[i] == NULL)
695b411b363SPhilipp Reisner 			break;
696b411b363SPhilipp Reisner 		if (FAULT_ACTIVE(mdev, DRBD_FAULT_MD_WR)) {
697b411b363SPhilipp Reisner 			bios[i]->bi_rw = WRITE;
698b411b363SPhilipp Reisner 			bio_endio(bios[i], -EIO);
699b411b363SPhilipp Reisner 		} else {
700b411b363SPhilipp Reisner 			submit_bio(WRITE, bios[i]);
701b411b363SPhilipp Reisner 		}
702b411b363SPhilipp Reisner 	}
703b411b363SPhilipp Reisner 
704b411b363SPhilipp Reisner 	drbd_blk_run_queue(bdev_get_queue(mdev->ldev->md_bdev));
705b411b363SPhilipp Reisner 
706b411b363SPhilipp Reisner 	/* always (try to) flush bitmap to stable storage */
707b411b363SPhilipp Reisner 	drbd_md_flush(mdev);
708b411b363SPhilipp Reisner 
709b411b363SPhilipp Reisner 	/* In case we did not submit a single IO do not wait for
710b411b363SPhilipp Reisner 	 * them to complete. ( Because we would wait forever here. )
711b411b363SPhilipp Reisner 	 *
712b411b363SPhilipp Reisner 	 * In case we had IOs and they are already complete, there
713b411b363SPhilipp Reisner 	 * is not point in waiting anyways.
714b411b363SPhilipp Reisner 	 * Therefore this if () ... */
715b411b363SPhilipp Reisner 	if (atomic_read(&wc.count))
716b411b363SPhilipp Reisner 		wait_for_completion(&wc.io_done);
717b411b363SPhilipp Reisner 
718b411b363SPhilipp Reisner 	put_ldev(mdev);
719b411b363SPhilipp Reisner 
720b411b363SPhilipp Reisner 	kfree(bios);
721b411b363SPhilipp Reisner 	return;
722b411b363SPhilipp Reisner 
723b411b363SPhilipp Reisner  free_bios_submit_one_by_one:
724b411b363SPhilipp Reisner 	/* free everything by calling the endio callback directly. */
725b411b363SPhilipp Reisner 	for (i = 0; i < nr_elements && bios[i]; i++)
726b411b363SPhilipp Reisner 		bio_endio(bios[i], 0);
727b411b363SPhilipp Reisner 
728b411b363SPhilipp Reisner 	kfree(bios);
729b411b363SPhilipp Reisner 
730b411b363SPhilipp Reisner  submit_one_by_one:
731b411b363SPhilipp Reisner 	dev_warn(DEV, "Using the slow drbd_al_to_on_disk_bm()\n");
732b411b363SPhilipp Reisner 
733b411b363SPhilipp Reisner 	for (i = 0; i < mdev->act_log->nr_elements; i++) {
734b411b363SPhilipp Reisner 		enr = lc_element_by_index(mdev->act_log, i)->lc_number;
735b411b363SPhilipp Reisner 		if (enr == LC_FREE)
736b411b363SPhilipp Reisner 			continue;
737b411b363SPhilipp Reisner 		/* Really slow: if we have al-extents 16..19 active,
738b411b363SPhilipp Reisner 		 * sector 4 will be written four times! Synchronous! */
739b411b363SPhilipp Reisner 		drbd_bm_write_sect(mdev, enr/AL_EXT_PER_BM_SECT);
740b411b363SPhilipp Reisner 	}
741b411b363SPhilipp Reisner 
742b411b363SPhilipp Reisner 	lc_unlock(mdev->act_log);
743b411b363SPhilipp Reisner 	wake_up(&mdev->al_wait);
744b411b363SPhilipp Reisner 	put_ldev(mdev);
745b411b363SPhilipp Reisner }
746b411b363SPhilipp Reisner 
747b411b363SPhilipp Reisner /**
748b411b363SPhilipp Reisner  * drbd_al_apply_to_bm() - Sets the bitmap to diry(1) where covered ba active AL extents
749b411b363SPhilipp Reisner  * @mdev:	DRBD device.
750b411b363SPhilipp Reisner  */
751b411b363SPhilipp Reisner void drbd_al_apply_to_bm(struct drbd_conf *mdev)
752b411b363SPhilipp Reisner {
753b411b363SPhilipp Reisner 	unsigned int enr;
754b411b363SPhilipp Reisner 	unsigned long add = 0;
755b411b363SPhilipp Reisner 	char ppb[10];
756*6719fb03SLars Ellenberg 	int i, tmp;
757b411b363SPhilipp Reisner 
758b411b363SPhilipp Reisner 	wait_event(mdev->al_wait, lc_try_lock(mdev->act_log));
759b411b363SPhilipp Reisner 
760b411b363SPhilipp Reisner 	for (i = 0; i < mdev->act_log->nr_elements; i++) {
761b411b363SPhilipp Reisner 		enr = lc_element_by_index(mdev->act_log, i)->lc_number;
762b411b363SPhilipp Reisner 		if (enr == LC_FREE)
763b411b363SPhilipp Reisner 			continue;
764*6719fb03SLars Ellenberg 		tmp = drbd_bm_ALe_set_all(mdev, enr);
765*6719fb03SLars Ellenberg 		dynamic_dev_dbg(DEV, "AL: set %d bits in extent %u\n", tmp, enr);
766*6719fb03SLars Ellenberg 		add += tmp;
767b411b363SPhilipp Reisner 	}
768b411b363SPhilipp Reisner 
769b411b363SPhilipp Reisner 	lc_unlock(mdev->act_log);
770b411b363SPhilipp Reisner 	wake_up(&mdev->al_wait);
771b411b363SPhilipp Reisner 
772b411b363SPhilipp Reisner 	dev_info(DEV, "Marked additional %s as out-of-sync based on AL.\n",
773b411b363SPhilipp Reisner 	     ppsize(ppb, Bit2KB(add)));
774b411b363SPhilipp Reisner }
775b411b363SPhilipp Reisner 
776b411b363SPhilipp Reisner static int _try_lc_del(struct drbd_conf *mdev, struct lc_element *al_ext)
777b411b363SPhilipp Reisner {
778b411b363SPhilipp Reisner 	int rv;
779b411b363SPhilipp Reisner 
780b411b363SPhilipp Reisner 	spin_lock_irq(&mdev->al_lock);
781b411b363SPhilipp Reisner 	rv = (al_ext->refcnt == 0);
782b411b363SPhilipp Reisner 	if (likely(rv))
783b411b363SPhilipp Reisner 		lc_del(mdev->act_log, al_ext);
784b411b363SPhilipp Reisner 	spin_unlock_irq(&mdev->al_lock);
785b411b363SPhilipp Reisner 
786b411b363SPhilipp Reisner 	return rv;
787b411b363SPhilipp Reisner }
788b411b363SPhilipp Reisner 
789b411b363SPhilipp Reisner /**
790b411b363SPhilipp Reisner  * drbd_al_shrink() - Removes all active extents form the activity log
791b411b363SPhilipp Reisner  * @mdev:	DRBD device.
792b411b363SPhilipp Reisner  *
793b411b363SPhilipp Reisner  * Removes all active extents form the activity log, waiting until
794b411b363SPhilipp Reisner  * the reference count of each entry dropped to 0 first, of course.
795b411b363SPhilipp Reisner  *
796b411b363SPhilipp Reisner  * You need to lock mdev->act_log with lc_try_lock() / lc_unlock()
797b411b363SPhilipp Reisner  */
798b411b363SPhilipp Reisner void drbd_al_shrink(struct drbd_conf *mdev)
799b411b363SPhilipp Reisner {
800b411b363SPhilipp Reisner 	struct lc_element *al_ext;
801b411b363SPhilipp Reisner 	int i;
802b411b363SPhilipp Reisner 
803b411b363SPhilipp Reisner 	D_ASSERT(test_bit(__LC_DIRTY, &mdev->act_log->flags));
804b411b363SPhilipp Reisner 
805b411b363SPhilipp Reisner 	for (i = 0; i < mdev->act_log->nr_elements; i++) {
806b411b363SPhilipp Reisner 		al_ext = lc_element_by_index(mdev->act_log, i);
807b411b363SPhilipp Reisner 		if (al_ext->lc_number == LC_FREE)
808b411b363SPhilipp Reisner 			continue;
809b411b363SPhilipp Reisner 		wait_event(mdev->al_wait, _try_lc_del(mdev, al_ext));
810b411b363SPhilipp Reisner 	}
811b411b363SPhilipp Reisner 
812b411b363SPhilipp Reisner 	wake_up(&mdev->al_wait);
813b411b363SPhilipp Reisner }
814b411b363SPhilipp Reisner 
815b411b363SPhilipp Reisner static int w_update_odbm(struct drbd_conf *mdev, struct drbd_work *w, int unused)
816b411b363SPhilipp Reisner {
817b411b363SPhilipp Reisner 	struct update_odbm_work *udw = container_of(w, struct update_odbm_work, w);
818b411b363SPhilipp Reisner 
819b411b363SPhilipp Reisner 	if (!get_ldev(mdev)) {
820b411b363SPhilipp Reisner 		if (__ratelimit(&drbd_ratelimit_state))
821b411b363SPhilipp Reisner 			dev_warn(DEV, "Can not update on disk bitmap, local IO disabled.\n");
822b411b363SPhilipp Reisner 		kfree(udw);
823b411b363SPhilipp Reisner 		return 1;
824b411b363SPhilipp Reisner 	}
825b411b363SPhilipp Reisner 
826b411b363SPhilipp Reisner 	drbd_bm_write_sect(mdev, udw->enr);
827b411b363SPhilipp Reisner 	put_ldev(mdev);
828b411b363SPhilipp Reisner 
829b411b363SPhilipp Reisner 	kfree(udw);
830b411b363SPhilipp Reisner 
831b411b363SPhilipp Reisner 	if (drbd_bm_total_weight(mdev) <= mdev->rs_failed) {
832b411b363SPhilipp Reisner 		switch (mdev->state.conn) {
833b411b363SPhilipp Reisner 		case C_SYNC_SOURCE:  case C_SYNC_TARGET:
834b411b363SPhilipp Reisner 		case C_PAUSED_SYNC_S: case C_PAUSED_SYNC_T:
835b411b363SPhilipp Reisner 			drbd_resync_finished(mdev);
836b411b363SPhilipp Reisner 		default:
837b411b363SPhilipp Reisner 			/* nothing to do */
838b411b363SPhilipp Reisner 			break;
839b411b363SPhilipp Reisner 		}
840b411b363SPhilipp Reisner 	}
841b411b363SPhilipp Reisner 	drbd_bcast_sync_progress(mdev);
842b411b363SPhilipp Reisner 
843b411b363SPhilipp Reisner 	return 1;
844b411b363SPhilipp Reisner }
845b411b363SPhilipp Reisner 
846b411b363SPhilipp Reisner 
847b411b363SPhilipp Reisner /* ATTENTION. The AL's extents are 4MB each, while the extents in the
848b411b363SPhilipp Reisner  * resync LRU-cache are 16MB each.
849b411b363SPhilipp Reisner  * The caller of this function has to hold an get_ldev() reference.
850b411b363SPhilipp Reisner  *
851b411b363SPhilipp Reisner  * TODO will be obsoleted once we have a caching lru of the on disk bitmap
852b411b363SPhilipp Reisner  */
853b411b363SPhilipp Reisner static void drbd_try_clear_on_disk_bm(struct drbd_conf *mdev, sector_t sector,
854b411b363SPhilipp Reisner 				      int count, int success)
855b411b363SPhilipp Reisner {
856b411b363SPhilipp Reisner 	struct lc_element *e;
857b411b363SPhilipp Reisner 	struct update_odbm_work *udw;
858b411b363SPhilipp Reisner 
859b411b363SPhilipp Reisner 	unsigned int enr;
860b411b363SPhilipp Reisner 
861b411b363SPhilipp Reisner 	D_ASSERT(atomic_read(&mdev->local_cnt));
862b411b363SPhilipp Reisner 
863b411b363SPhilipp Reisner 	/* I simply assume that a sector/size pair never crosses
864b411b363SPhilipp Reisner 	 * a 16 MB extent border. (Currently this is true...) */
865b411b363SPhilipp Reisner 	enr = BM_SECT_TO_EXT(sector);
866b411b363SPhilipp Reisner 
867b411b363SPhilipp Reisner 	e = lc_get(mdev->resync, enr);
868b411b363SPhilipp Reisner 	if (e) {
869b411b363SPhilipp Reisner 		struct bm_extent *ext = lc_entry(e, struct bm_extent, lce);
870b411b363SPhilipp Reisner 		if (ext->lce.lc_number == enr) {
871b411b363SPhilipp Reisner 			if (success)
872b411b363SPhilipp Reisner 				ext->rs_left -= count;
873b411b363SPhilipp Reisner 			else
874b411b363SPhilipp Reisner 				ext->rs_failed += count;
875b411b363SPhilipp Reisner 			if (ext->rs_left < ext->rs_failed) {
876b411b363SPhilipp Reisner 				dev_err(DEV, "BAD! sector=%llus enr=%u rs_left=%d "
877b411b363SPhilipp Reisner 				    "rs_failed=%d count=%d\n",
878b411b363SPhilipp Reisner 				     (unsigned long long)sector,
879b411b363SPhilipp Reisner 				     ext->lce.lc_number, ext->rs_left,
880b411b363SPhilipp Reisner 				     ext->rs_failed, count);
881b411b363SPhilipp Reisner 				dump_stack();
882b411b363SPhilipp Reisner 
883b411b363SPhilipp Reisner 				lc_put(mdev->resync, &ext->lce);
884b411b363SPhilipp Reisner 				drbd_force_state(mdev, NS(conn, C_DISCONNECTING));
885b411b363SPhilipp Reisner 				return;
886b411b363SPhilipp Reisner 			}
887b411b363SPhilipp Reisner 		} else {
888b411b363SPhilipp Reisner 			/* Normally this element should be in the cache,
889b411b363SPhilipp Reisner 			 * since drbd_rs_begin_io() pulled it already in.
890b411b363SPhilipp Reisner 			 *
891b411b363SPhilipp Reisner 			 * But maybe an application write finished, and we set
892b411b363SPhilipp Reisner 			 * something outside the resync lru_cache in sync.
893b411b363SPhilipp Reisner 			 */
894b411b363SPhilipp Reisner 			int rs_left = drbd_bm_e_weight(mdev, enr);
895b411b363SPhilipp Reisner 			if (ext->flags != 0) {
896b411b363SPhilipp Reisner 				dev_warn(DEV, "changing resync lce: %d[%u;%02lx]"
897b411b363SPhilipp Reisner 				     " -> %d[%u;00]\n",
898b411b363SPhilipp Reisner 				     ext->lce.lc_number, ext->rs_left,
899b411b363SPhilipp Reisner 				     ext->flags, enr, rs_left);
900b411b363SPhilipp Reisner 				ext->flags = 0;
901b411b363SPhilipp Reisner 			}
902b411b363SPhilipp Reisner 			if (ext->rs_failed) {
903b411b363SPhilipp Reisner 				dev_warn(DEV, "Kicking resync_lru element enr=%u "
904b411b363SPhilipp Reisner 				     "out with rs_failed=%d\n",
905b411b363SPhilipp Reisner 				     ext->lce.lc_number, ext->rs_failed);
906b411b363SPhilipp Reisner 				set_bit(WRITE_BM_AFTER_RESYNC, &mdev->flags);
907b411b363SPhilipp Reisner 			}
908b411b363SPhilipp Reisner 			ext->rs_left = rs_left;
909b411b363SPhilipp Reisner 			ext->rs_failed = success ? 0 : count;
910b411b363SPhilipp Reisner 			lc_changed(mdev->resync, &ext->lce);
911b411b363SPhilipp Reisner 		}
912b411b363SPhilipp Reisner 		lc_put(mdev->resync, &ext->lce);
913b411b363SPhilipp Reisner 		/* no race, we are within the al_lock! */
914b411b363SPhilipp Reisner 
915b411b363SPhilipp Reisner 		if (ext->rs_left == ext->rs_failed) {
916b411b363SPhilipp Reisner 			ext->rs_failed = 0;
917b411b363SPhilipp Reisner 
918b411b363SPhilipp Reisner 			udw = kmalloc(sizeof(*udw), GFP_ATOMIC);
919b411b363SPhilipp Reisner 			if (udw) {
920b411b363SPhilipp Reisner 				udw->enr = ext->lce.lc_number;
921b411b363SPhilipp Reisner 				udw->w.cb = w_update_odbm;
922b411b363SPhilipp Reisner 				drbd_queue_work_front(&mdev->data.work, &udw->w);
923b411b363SPhilipp Reisner 			} else {
924b411b363SPhilipp Reisner 				dev_warn(DEV, "Could not kmalloc an udw\n");
925b411b363SPhilipp Reisner 				set_bit(WRITE_BM_AFTER_RESYNC, &mdev->flags);
926b411b363SPhilipp Reisner 			}
927b411b363SPhilipp Reisner 		}
928b411b363SPhilipp Reisner 	} else {
929b411b363SPhilipp Reisner 		dev_err(DEV, "lc_get() failed! locked=%d/%d flags=%lu\n",
930b411b363SPhilipp Reisner 		    mdev->resync_locked,
931b411b363SPhilipp Reisner 		    mdev->resync->nr_elements,
932b411b363SPhilipp Reisner 		    mdev->resync->flags);
933b411b363SPhilipp Reisner 	}
934b411b363SPhilipp Reisner }
935b411b363SPhilipp Reisner 
936b411b363SPhilipp Reisner /* clear the bit corresponding to the piece of storage in question:
937b411b363SPhilipp Reisner  * size byte of data starting from sector.  Only clear a bits of the affected
938b411b363SPhilipp Reisner  * one ore more _aligned_ BM_BLOCK_SIZE blocks.
939b411b363SPhilipp Reisner  *
940b411b363SPhilipp Reisner  * called by worker on C_SYNC_TARGET and receiver on SyncSource.
941b411b363SPhilipp Reisner  *
942b411b363SPhilipp Reisner  */
943b411b363SPhilipp Reisner void __drbd_set_in_sync(struct drbd_conf *mdev, sector_t sector, int size,
944b411b363SPhilipp Reisner 		       const char *file, const unsigned int line)
945b411b363SPhilipp Reisner {
946b411b363SPhilipp Reisner 	/* Is called from worker and receiver context _only_ */
947b411b363SPhilipp Reisner 	unsigned long sbnr, ebnr, lbnr;
948b411b363SPhilipp Reisner 	unsigned long count = 0;
949b411b363SPhilipp Reisner 	sector_t esector, nr_sectors;
950b411b363SPhilipp Reisner 	int wake_up = 0;
951b411b363SPhilipp Reisner 	unsigned long flags;
952b411b363SPhilipp Reisner 
953b411b363SPhilipp Reisner 	if (size <= 0 || (size & 0x1ff) != 0 || size > DRBD_MAX_SEGMENT_SIZE) {
954b411b363SPhilipp Reisner 		dev_err(DEV, "drbd_set_in_sync: sector=%llus size=%d nonsense!\n",
955b411b363SPhilipp Reisner 				(unsigned long long)sector, size);
956b411b363SPhilipp Reisner 		return;
957b411b363SPhilipp Reisner 	}
958b411b363SPhilipp Reisner 	nr_sectors = drbd_get_capacity(mdev->this_bdev);
959b411b363SPhilipp Reisner 	esector = sector + (size >> 9) - 1;
960b411b363SPhilipp Reisner 
961b411b363SPhilipp Reisner 	ERR_IF(sector >= nr_sectors) return;
962b411b363SPhilipp Reisner 	ERR_IF(esector >= nr_sectors) esector = (nr_sectors-1);
963b411b363SPhilipp Reisner 
964b411b363SPhilipp Reisner 	lbnr = BM_SECT_TO_BIT(nr_sectors-1);
965b411b363SPhilipp Reisner 
966b411b363SPhilipp Reisner 	/* we clear it (in sync).
967b411b363SPhilipp Reisner 	 * round up start sector, round down end sector.  we make sure we only
968b411b363SPhilipp Reisner 	 * clear full, aligned, BM_BLOCK_SIZE (4K) blocks */
969b411b363SPhilipp Reisner 	if (unlikely(esector < BM_SECT_PER_BIT-1))
970b411b363SPhilipp Reisner 		return;
971b411b363SPhilipp Reisner 	if (unlikely(esector == (nr_sectors-1)))
972b411b363SPhilipp Reisner 		ebnr = lbnr;
973b411b363SPhilipp Reisner 	else
974b411b363SPhilipp Reisner 		ebnr = BM_SECT_TO_BIT(esector - (BM_SECT_PER_BIT-1));
975b411b363SPhilipp Reisner 	sbnr = BM_SECT_TO_BIT(sector + BM_SECT_PER_BIT-1);
976b411b363SPhilipp Reisner 
977b411b363SPhilipp Reisner 	if (sbnr > ebnr)
978b411b363SPhilipp Reisner 		return;
979b411b363SPhilipp Reisner 
980b411b363SPhilipp Reisner 	/*
981b411b363SPhilipp Reisner 	 * ok, (capacity & 7) != 0 sometimes, but who cares...
982b411b363SPhilipp Reisner 	 * we count rs_{total,left} in bits, not sectors.
983b411b363SPhilipp Reisner 	 */
984b411b363SPhilipp Reisner 	count = drbd_bm_clear_bits(mdev, sbnr, ebnr);
9851d7734a0SLars Ellenberg 	if (count && get_ldev(mdev)) {
9861d7734a0SLars Ellenberg 		unsigned long now = jiffies;
9871d7734a0SLars Ellenberg 		unsigned long last = mdev->rs_mark_time[mdev->rs_last_mark];
9881d7734a0SLars Ellenberg 		int next = (mdev->rs_last_mark + 1) % DRBD_SYNC_MARKS;
9891d7734a0SLars Ellenberg 		if (time_after_eq(now, last + DRBD_SYNC_MARK_STEP)) {
9901d7734a0SLars Ellenberg 			unsigned long tw = drbd_bm_total_weight(mdev);
9911d7734a0SLars Ellenberg 			if (mdev->rs_mark_left[mdev->rs_last_mark] != tw &&
992b411b363SPhilipp Reisner 			    mdev->state.conn != C_PAUSED_SYNC_T &&
993b411b363SPhilipp Reisner 			    mdev->state.conn != C_PAUSED_SYNC_S) {
9941d7734a0SLars Ellenberg 				mdev->rs_mark_time[next] = now;
9951d7734a0SLars Ellenberg 				mdev->rs_mark_left[next] = tw;
9961d7734a0SLars Ellenberg 				mdev->rs_last_mark = next;
997b411b363SPhilipp Reisner 			}
998b411b363SPhilipp Reisner 		}
9991d7734a0SLars Ellenberg 		spin_lock_irqsave(&mdev->al_lock, flags);
1000b411b363SPhilipp Reisner 		drbd_try_clear_on_disk_bm(mdev, sector, count, TRUE);
10011d7734a0SLars Ellenberg 		spin_unlock_irqrestore(&mdev->al_lock, flags);
10021d7734a0SLars Ellenberg 
1003b411b363SPhilipp Reisner 		/* just wake_up unconditional now, various lc_chaged(),
1004b411b363SPhilipp Reisner 		 * lc_put() in drbd_try_clear_on_disk_bm(). */
1005b411b363SPhilipp Reisner 		wake_up = 1;
10061d7734a0SLars Ellenberg 		put_ldev(mdev);
1007b411b363SPhilipp Reisner 	}
1008b411b363SPhilipp Reisner 	if (wake_up)
1009b411b363SPhilipp Reisner 		wake_up(&mdev->al_wait);
1010b411b363SPhilipp Reisner }
1011b411b363SPhilipp Reisner 
1012b411b363SPhilipp Reisner /*
1013b411b363SPhilipp Reisner  * this is intended to set one request worth of data out of sync.
1014b411b363SPhilipp Reisner  * affects at least 1 bit,
1015b411b363SPhilipp Reisner  * and at most 1+DRBD_MAX_SEGMENT_SIZE/BM_BLOCK_SIZE bits.
1016b411b363SPhilipp Reisner  *
1017b411b363SPhilipp Reisner  * called by tl_clear and drbd_send_dblock (==drbd_make_request).
1018b411b363SPhilipp Reisner  * so this can be _any_ process.
1019b411b363SPhilipp Reisner  */
1020b411b363SPhilipp Reisner void __drbd_set_out_of_sync(struct drbd_conf *mdev, sector_t sector, int size,
1021b411b363SPhilipp Reisner 			    const char *file, const unsigned int line)
1022b411b363SPhilipp Reisner {
1023b411b363SPhilipp Reisner 	unsigned long sbnr, ebnr, lbnr, flags;
1024b411b363SPhilipp Reisner 	sector_t esector, nr_sectors;
1025b411b363SPhilipp Reisner 	unsigned int enr, count;
1026b411b363SPhilipp Reisner 	struct lc_element *e;
1027b411b363SPhilipp Reisner 
1028b411b363SPhilipp Reisner 	if (size <= 0 || (size & 0x1ff) != 0 || size > DRBD_MAX_SEGMENT_SIZE) {
1029b411b363SPhilipp Reisner 		dev_err(DEV, "sector: %llus, size: %d\n",
1030b411b363SPhilipp Reisner 			(unsigned long long)sector, size);
1031b411b363SPhilipp Reisner 		return;
1032b411b363SPhilipp Reisner 	}
1033b411b363SPhilipp Reisner 
1034b411b363SPhilipp Reisner 	if (!get_ldev(mdev))
1035b411b363SPhilipp Reisner 		return; /* no disk, no metadata, no bitmap to set bits in */
1036b411b363SPhilipp Reisner 
1037b411b363SPhilipp Reisner 	nr_sectors = drbd_get_capacity(mdev->this_bdev);
1038b411b363SPhilipp Reisner 	esector = sector + (size >> 9) - 1;
1039b411b363SPhilipp Reisner 
1040b411b363SPhilipp Reisner 	ERR_IF(sector >= nr_sectors)
1041b411b363SPhilipp Reisner 		goto out;
1042b411b363SPhilipp Reisner 	ERR_IF(esector >= nr_sectors)
1043b411b363SPhilipp Reisner 		esector = (nr_sectors-1);
1044b411b363SPhilipp Reisner 
1045b411b363SPhilipp Reisner 	lbnr = BM_SECT_TO_BIT(nr_sectors-1);
1046b411b363SPhilipp Reisner 
1047b411b363SPhilipp Reisner 	/* we set it out of sync,
1048b411b363SPhilipp Reisner 	 * we do not need to round anything here */
1049b411b363SPhilipp Reisner 	sbnr = BM_SECT_TO_BIT(sector);
1050b411b363SPhilipp Reisner 	ebnr = BM_SECT_TO_BIT(esector);
1051b411b363SPhilipp Reisner 
1052b411b363SPhilipp Reisner 	/* ok, (capacity & 7) != 0 sometimes, but who cares...
1053b411b363SPhilipp Reisner 	 * we count rs_{total,left} in bits, not sectors.  */
1054b411b363SPhilipp Reisner 	spin_lock_irqsave(&mdev->al_lock, flags);
1055b411b363SPhilipp Reisner 	count = drbd_bm_set_bits(mdev, sbnr, ebnr);
1056b411b363SPhilipp Reisner 
1057b411b363SPhilipp Reisner 	enr = BM_SECT_TO_EXT(sector);
1058b411b363SPhilipp Reisner 	e = lc_find(mdev->resync, enr);
1059b411b363SPhilipp Reisner 	if (e)
1060b411b363SPhilipp Reisner 		lc_entry(e, struct bm_extent, lce)->rs_left += count;
1061b411b363SPhilipp Reisner 	spin_unlock_irqrestore(&mdev->al_lock, flags);
1062b411b363SPhilipp Reisner 
1063b411b363SPhilipp Reisner out:
1064b411b363SPhilipp Reisner 	put_ldev(mdev);
1065b411b363SPhilipp Reisner }
1066b411b363SPhilipp Reisner 
1067b411b363SPhilipp Reisner static
1068b411b363SPhilipp Reisner struct bm_extent *_bme_get(struct drbd_conf *mdev, unsigned int enr)
1069b411b363SPhilipp Reisner {
1070b411b363SPhilipp Reisner 	struct lc_element *e;
1071b411b363SPhilipp Reisner 	struct bm_extent *bm_ext;
1072b411b363SPhilipp Reisner 	int wakeup = 0;
1073b411b363SPhilipp Reisner 	unsigned long rs_flags;
1074b411b363SPhilipp Reisner 
1075b411b363SPhilipp Reisner 	spin_lock_irq(&mdev->al_lock);
1076b411b363SPhilipp Reisner 	if (mdev->resync_locked > mdev->resync->nr_elements/2) {
1077b411b363SPhilipp Reisner 		spin_unlock_irq(&mdev->al_lock);
1078b411b363SPhilipp Reisner 		return NULL;
1079b411b363SPhilipp Reisner 	}
1080b411b363SPhilipp Reisner 	e = lc_get(mdev->resync, enr);
1081b411b363SPhilipp Reisner 	bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL;
1082b411b363SPhilipp Reisner 	if (bm_ext) {
1083b411b363SPhilipp Reisner 		if (bm_ext->lce.lc_number != enr) {
1084b411b363SPhilipp Reisner 			bm_ext->rs_left = drbd_bm_e_weight(mdev, enr);
1085b411b363SPhilipp Reisner 			bm_ext->rs_failed = 0;
1086b411b363SPhilipp Reisner 			lc_changed(mdev->resync, &bm_ext->lce);
1087b411b363SPhilipp Reisner 			wakeup = 1;
1088b411b363SPhilipp Reisner 		}
1089b411b363SPhilipp Reisner 		if (bm_ext->lce.refcnt == 1)
1090b411b363SPhilipp Reisner 			mdev->resync_locked++;
1091b411b363SPhilipp Reisner 		set_bit(BME_NO_WRITES, &bm_ext->flags);
1092b411b363SPhilipp Reisner 	}
1093b411b363SPhilipp Reisner 	rs_flags = mdev->resync->flags;
1094b411b363SPhilipp Reisner 	spin_unlock_irq(&mdev->al_lock);
1095b411b363SPhilipp Reisner 	if (wakeup)
1096b411b363SPhilipp Reisner 		wake_up(&mdev->al_wait);
1097b411b363SPhilipp Reisner 
1098b411b363SPhilipp Reisner 	if (!bm_ext) {
1099b411b363SPhilipp Reisner 		if (rs_flags & LC_STARVING)
1100b411b363SPhilipp Reisner 			dev_warn(DEV, "Have to wait for element"
1101b411b363SPhilipp Reisner 			     " (resync LRU too small?)\n");
1102b411b363SPhilipp Reisner 		BUG_ON(rs_flags & LC_DIRTY);
1103b411b363SPhilipp Reisner 	}
1104b411b363SPhilipp Reisner 
1105b411b363SPhilipp Reisner 	return bm_ext;
1106b411b363SPhilipp Reisner }
1107b411b363SPhilipp Reisner 
1108b411b363SPhilipp Reisner static int _is_in_al(struct drbd_conf *mdev, unsigned int enr)
1109b411b363SPhilipp Reisner {
1110b411b363SPhilipp Reisner 	struct lc_element *al_ext;
1111b411b363SPhilipp Reisner 	int rv = 0;
1112b411b363SPhilipp Reisner 
1113b411b363SPhilipp Reisner 	spin_lock_irq(&mdev->al_lock);
1114b411b363SPhilipp Reisner 	if (unlikely(enr == mdev->act_log->new_number))
1115b411b363SPhilipp Reisner 		rv = 1;
1116b411b363SPhilipp Reisner 	else {
1117b411b363SPhilipp Reisner 		al_ext = lc_find(mdev->act_log, enr);
1118b411b363SPhilipp Reisner 		if (al_ext) {
1119b411b363SPhilipp Reisner 			if (al_ext->refcnt)
1120b411b363SPhilipp Reisner 				rv = 1;
1121b411b363SPhilipp Reisner 		}
1122b411b363SPhilipp Reisner 	}
1123b411b363SPhilipp Reisner 	spin_unlock_irq(&mdev->al_lock);
1124b411b363SPhilipp Reisner 
1125b411b363SPhilipp Reisner 	/*
1126b411b363SPhilipp Reisner 	if (unlikely(rv)) {
1127b411b363SPhilipp Reisner 		dev_info(DEV, "Delaying sync read until app's write is done\n");
1128b411b363SPhilipp Reisner 	}
1129b411b363SPhilipp Reisner 	*/
1130b411b363SPhilipp Reisner 	return rv;
1131b411b363SPhilipp Reisner }
1132b411b363SPhilipp Reisner 
1133b411b363SPhilipp Reisner /**
1134b411b363SPhilipp Reisner  * drbd_rs_begin_io() - Gets an extent in the resync LRU cache and sets it to BME_LOCKED
1135b411b363SPhilipp Reisner  * @mdev:	DRBD device.
1136b411b363SPhilipp Reisner  * @sector:	The sector number.
1137b411b363SPhilipp Reisner  *
113880a40e43SLars Ellenberg  * This functions sleeps on al_wait. Returns 0 on success, -EINTR if interrupted.
1139b411b363SPhilipp Reisner  */
1140b411b363SPhilipp Reisner int drbd_rs_begin_io(struct drbd_conf *mdev, sector_t sector)
1141b411b363SPhilipp Reisner {
1142b411b363SPhilipp Reisner 	unsigned int enr = BM_SECT_TO_EXT(sector);
1143b411b363SPhilipp Reisner 	struct bm_extent *bm_ext;
1144b411b363SPhilipp Reisner 	int i, sig;
1145b411b363SPhilipp Reisner 
1146b411b363SPhilipp Reisner 	sig = wait_event_interruptible(mdev->al_wait,
1147b411b363SPhilipp Reisner 			(bm_ext = _bme_get(mdev, enr)));
1148b411b363SPhilipp Reisner 	if (sig)
114980a40e43SLars Ellenberg 		return -EINTR;
1150b411b363SPhilipp Reisner 
1151b411b363SPhilipp Reisner 	if (test_bit(BME_LOCKED, &bm_ext->flags))
115280a40e43SLars Ellenberg 		return 0;
1153b411b363SPhilipp Reisner 
1154b411b363SPhilipp Reisner 	for (i = 0; i < AL_EXT_PER_BM_SECT; i++) {
1155b411b363SPhilipp Reisner 		sig = wait_event_interruptible(mdev->al_wait,
1156b411b363SPhilipp Reisner 				!_is_in_al(mdev, enr * AL_EXT_PER_BM_SECT + i));
1157b411b363SPhilipp Reisner 		if (sig) {
1158b411b363SPhilipp Reisner 			spin_lock_irq(&mdev->al_lock);
1159b411b363SPhilipp Reisner 			if (lc_put(mdev->resync, &bm_ext->lce) == 0) {
1160b411b363SPhilipp Reisner 				clear_bit(BME_NO_WRITES, &bm_ext->flags);
1161b411b363SPhilipp Reisner 				mdev->resync_locked--;
1162b411b363SPhilipp Reisner 				wake_up(&mdev->al_wait);
1163b411b363SPhilipp Reisner 			}
1164b411b363SPhilipp Reisner 			spin_unlock_irq(&mdev->al_lock);
116580a40e43SLars Ellenberg 			return -EINTR;
1166b411b363SPhilipp Reisner 		}
1167b411b363SPhilipp Reisner 	}
1168b411b363SPhilipp Reisner 	set_bit(BME_LOCKED, &bm_ext->flags);
116980a40e43SLars Ellenberg 	return 0;
1170b411b363SPhilipp Reisner }
1171b411b363SPhilipp Reisner 
1172b411b363SPhilipp Reisner /**
1173b411b363SPhilipp Reisner  * drbd_try_rs_begin_io() - Gets an extent in the resync LRU cache, does not sleep
1174b411b363SPhilipp Reisner  * @mdev:	DRBD device.
1175b411b363SPhilipp Reisner  * @sector:	The sector number.
1176b411b363SPhilipp Reisner  *
1177b411b363SPhilipp Reisner  * Gets an extent in the resync LRU cache, sets it to BME_NO_WRITES, then
1178b411b363SPhilipp Reisner  * tries to set it to BME_LOCKED. Returns 0 upon success, and -EAGAIN
1179b411b363SPhilipp Reisner  * if there is still application IO going on in this area.
1180b411b363SPhilipp Reisner  */
1181b411b363SPhilipp Reisner int drbd_try_rs_begin_io(struct drbd_conf *mdev, sector_t sector)
1182b411b363SPhilipp Reisner {
1183b411b363SPhilipp Reisner 	unsigned int enr = BM_SECT_TO_EXT(sector);
1184b411b363SPhilipp Reisner 	const unsigned int al_enr = enr*AL_EXT_PER_BM_SECT;
1185b411b363SPhilipp Reisner 	struct lc_element *e;
1186b411b363SPhilipp Reisner 	struct bm_extent *bm_ext;
1187b411b363SPhilipp Reisner 	int i;
1188b411b363SPhilipp Reisner 
1189b411b363SPhilipp Reisner 	spin_lock_irq(&mdev->al_lock);
1190b411b363SPhilipp Reisner 	if (mdev->resync_wenr != LC_FREE && mdev->resync_wenr != enr) {
1191b411b363SPhilipp Reisner 		/* in case you have very heavy scattered io, it may
1192b411b363SPhilipp Reisner 		 * stall the syncer undefined if we give up the ref count
1193b411b363SPhilipp Reisner 		 * when we try again and requeue.
1194b411b363SPhilipp Reisner 		 *
1195b411b363SPhilipp Reisner 		 * if we don't give up the refcount, but the next time
1196b411b363SPhilipp Reisner 		 * we are scheduled this extent has been "synced" by new
1197b411b363SPhilipp Reisner 		 * application writes, we'd miss the lc_put on the
1198b411b363SPhilipp Reisner 		 * extent we keep the refcount on.
1199b411b363SPhilipp Reisner 		 * so we remembered which extent we had to try again, and
1200b411b363SPhilipp Reisner 		 * if the next requested one is something else, we do
1201b411b363SPhilipp Reisner 		 * the lc_put here...
1202b411b363SPhilipp Reisner 		 * we also have to wake_up
1203b411b363SPhilipp Reisner 		 */
1204b411b363SPhilipp Reisner 		e = lc_find(mdev->resync, mdev->resync_wenr);
1205b411b363SPhilipp Reisner 		bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL;
1206b411b363SPhilipp Reisner 		if (bm_ext) {
1207b411b363SPhilipp Reisner 			D_ASSERT(!test_bit(BME_LOCKED, &bm_ext->flags));
1208b411b363SPhilipp Reisner 			D_ASSERT(test_bit(BME_NO_WRITES, &bm_ext->flags));
1209b411b363SPhilipp Reisner 			clear_bit(BME_NO_WRITES, &bm_ext->flags);
1210b411b363SPhilipp Reisner 			mdev->resync_wenr = LC_FREE;
1211b411b363SPhilipp Reisner 			if (lc_put(mdev->resync, &bm_ext->lce) == 0)
1212b411b363SPhilipp Reisner 				mdev->resync_locked--;
1213b411b363SPhilipp Reisner 			wake_up(&mdev->al_wait);
1214b411b363SPhilipp Reisner 		} else {
1215b411b363SPhilipp Reisner 			dev_alert(DEV, "LOGIC BUG\n");
1216b411b363SPhilipp Reisner 		}
1217b411b363SPhilipp Reisner 	}
1218b411b363SPhilipp Reisner 	/* TRY. */
1219b411b363SPhilipp Reisner 	e = lc_try_get(mdev->resync, enr);
1220b411b363SPhilipp Reisner 	bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL;
1221b411b363SPhilipp Reisner 	if (bm_ext) {
1222b411b363SPhilipp Reisner 		if (test_bit(BME_LOCKED, &bm_ext->flags))
1223b411b363SPhilipp Reisner 			goto proceed;
1224b411b363SPhilipp Reisner 		if (!test_and_set_bit(BME_NO_WRITES, &bm_ext->flags)) {
1225b411b363SPhilipp Reisner 			mdev->resync_locked++;
1226b411b363SPhilipp Reisner 		} else {
1227b411b363SPhilipp Reisner 			/* we did set the BME_NO_WRITES,
1228b411b363SPhilipp Reisner 			 * but then could not set BME_LOCKED,
1229b411b363SPhilipp Reisner 			 * so we tried again.
1230b411b363SPhilipp Reisner 			 * drop the extra reference. */
1231b411b363SPhilipp Reisner 			bm_ext->lce.refcnt--;
1232b411b363SPhilipp Reisner 			D_ASSERT(bm_ext->lce.refcnt > 0);
1233b411b363SPhilipp Reisner 		}
1234b411b363SPhilipp Reisner 		goto check_al;
1235b411b363SPhilipp Reisner 	} else {
1236b411b363SPhilipp Reisner 		/* do we rather want to try later? */
12376a0afdf5SJens Axboe 		if (mdev->resync_locked > mdev->resync->nr_elements-3)
1238b411b363SPhilipp Reisner 			goto try_again;
1239b411b363SPhilipp Reisner 		/* Do or do not. There is no try. -- Yoda */
1240b411b363SPhilipp Reisner 		e = lc_get(mdev->resync, enr);
1241b411b363SPhilipp Reisner 		bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL;
1242b411b363SPhilipp Reisner 		if (!bm_ext) {
1243b411b363SPhilipp Reisner 			const unsigned long rs_flags = mdev->resync->flags;
1244b411b363SPhilipp Reisner 			if (rs_flags & LC_STARVING)
1245b411b363SPhilipp Reisner 				dev_warn(DEV, "Have to wait for element"
1246b411b363SPhilipp Reisner 				     " (resync LRU too small?)\n");
1247b411b363SPhilipp Reisner 			BUG_ON(rs_flags & LC_DIRTY);
1248b411b363SPhilipp Reisner 			goto try_again;
1249b411b363SPhilipp Reisner 		}
1250b411b363SPhilipp Reisner 		if (bm_ext->lce.lc_number != enr) {
1251b411b363SPhilipp Reisner 			bm_ext->rs_left = drbd_bm_e_weight(mdev, enr);
1252b411b363SPhilipp Reisner 			bm_ext->rs_failed = 0;
1253b411b363SPhilipp Reisner 			lc_changed(mdev->resync, &bm_ext->lce);
1254b411b363SPhilipp Reisner 			wake_up(&mdev->al_wait);
1255b411b363SPhilipp Reisner 			D_ASSERT(test_bit(BME_LOCKED, &bm_ext->flags) == 0);
1256b411b363SPhilipp Reisner 		}
1257b411b363SPhilipp Reisner 		set_bit(BME_NO_WRITES, &bm_ext->flags);
1258b411b363SPhilipp Reisner 		D_ASSERT(bm_ext->lce.refcnt == 1);
1259b411b363SPhilipp Reisner 		mdev->resync_locked++;
1260b411b363SPhilipp Reisner 		goto check_al;
1261b411b363SPhilipp Reisner 	}
1262b411b363SPhilipp Reisner check_al:
1263b411b363SPhilipp Reisner 	for (i = 0; i < AL_EXT_PER_BM_SECT; i++) {
1264b411b363SPhilipp Reisner 		if (unlikely(al_enr+i == mdev->act_log->new_number))
1265b411b363SPhilipp Reisner 			goto try_again;
1266b411b363SPhilipp Reisner 		if (lc_is_used(mdev->act_log, al_enr+i))
1267b411b363SPhilipp Reisner 			goto try_again;
1268b411b363SPhilipp Reisner 	}
1269b411b363SPhilipp Reisner 	set_bit(BME_LOCKED, &bm_ext->flags);
1270b411b363SPhilipp Reisner proceed:
1271b411b363SPhilipp Reisner 	mdev->resync_wenr = LC_FREE;
1272b411b363SPhilipp Reisner 	spin_unlock_irq(&mdev->al_lock);
1273b411b363SPhilipp Reisner 	return 0;
1274b411b363SPhilipp Reisner 
1275b411b363SPhilipp Reisner try_again:
1276b411b363SPhilipp Reisner 	if (bm_ext)
1277b411b363SPhilipp Reisner 		mdev->resync_wenr = enr;
1278b411b363SPhilipp Reisner 	spin_unlock_irq(&mdev->al_lock);
1279b411b363SPhilipp Reisner 	return -EAGAIN;
1280b411b363SPhilipp Reisner }
1281b411b363SPhilipp Reisner 
1282b411b363SPhilipp Reisner void drbd_rs_complete_io(struct drbd_conf *mdev, sector_t sector)
1283b411b363SPhilipp Reisner {
1284b411b363SPhilipp Reisner 	unsigned int enr = BM_SECT_TO_EXT(sector);
1285b411b363SPhilipp Reisner 	struct lc_element *e;
1286b411b363SPhilipp Reisner 	struct bm_extent *bm_ext;
1287b411b363SPhilipp Reisner 	unsigned long flags;
1288b411b363SPhilipp Reisner 
1289b411b363SPhilipp Reisner 	spin_lock_irqsave(&mdev->al_lock, flags);
1290b411b363SPhilipp Reisner 	e = lc_find(mdev->resync, enr);
1291b411b363SPhilipp Reisner 	bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL;
1292b411b363SPhilipp Reisner 	if (!bm_ext) {
1293b411b363SPhilipp Reisner 		spin_unlock_irqrestore(&mdev->al_lock, flags);
1294b411b363SPhilipp Reisner 		if (__ratelimit(&drbd_ratelimit_state))
1295b411b363SPhilipp Reisner 			dev_err(DEV, "drbd_rs_complete_io() called, but extent not found\n");
1296b411b363SPhilipp Reisner 		return;
1297b411b363SPhilipp Reisner 	}
1298b411b363SPhilipp Reisner 
1299b411b363SPhilipp Reisner 	if (bm_ext->lce.refcnt == 0) {
1300b411b363SPhilipp Reisner 		spin_unlock_irqrestore(&mdev->al_lock, flags);
1301b411b363SPhilipp Reisner 		dev_err(DEV, "drbd_rs_complete_io(,%llu [=%u]) called, "
1302b411b363SPhilipp Reisner 		    "but refcnt is 0!?\n",
1303b411b363SPhilipp Reisner 		    (unsigned long long)sector, enr);
1304b411b363SPhilipp Reisner 		return;
1305b411b363SPhilipp Reisner 	}
1306b411b363SPhilipp Reisner 
1307b411b363SPhilipp Reisner 	if (lc_put(mdev->resync, &bm_ext->lce) == 0) {
1308b411b363SPhilipp Reisner 		clear_bit(BME_LOCKED, &bm_ext->flags);
1309b411b363SPhilipp Reisner 		clear_bit(BME_NO_WRITES, &bm_ext->flags);
1310b411b363SPhilipp Reisner 		mdev->resync_locked--;
1311b411b363SPhilipp Reisner 		wake_up(&mdev->al_wait);
1312b411b363SPhilipp Reisner 	}
1313b411b363SPhilipp Reisner 
1314b411b363SPhilipp Reisner 	spin_unlock_irqrestore(&mdev->al_lock, flags);
1315b411b363SPhilipp Reisner }
1316b411b363SPhilipp Reisner 
1317b411b363SPhilipp Reisner /**
1318b411b363SPhilipp Reisner  * drbd_rs_cancel_all() - Removes all extents from the resync LRU (even BME_LOCKED)
1319b411b363SPhilipp Reisner  * @mdev:	DRBD device.
1320b411b363SPhilipp Reisner  */
1321b411b363SPhilipp Reisner void drbd_rs_cancel_all(struct drbd_conf *mdev)
1322b411b363SPhilipp Reisner {
1323b411b363SPhilipp Reisner 	spin_lock_irq(&mdev->al_lock);
1324b411b363SPhilipp Reisner 
1325b411b363SPhilipp Reisner 	if (get_ldev_if_state(mdev, D_FAILED)) { /* Makes sure ->resync is there. */
1326b411b363SPhilipp Reisner 		lc_reset(mdev->resync);
1327b411b363SPhilipp Reisner 		put_ldev(mdev);
1328b411b363SPhilipp Reisner 	}
1329b411b363SPhilipp Reisner 	mdev->resync_locked = 0;
1330b411b363SPhilipp Reisner 	mdev->resync_wenr = LC_FREE;
1331b411b363SPhilipp Reisner 	spin_unlock_irq(&mdev->al_lock);
1332b411b363SPhilipp Reisner 	wake_up(&mdev->al_wait);
1333b411b363SPhilipp Reisner }
1334b411b363SPhilipp Reisner 
1335b411b363SPhilipp Reisner /**
1336b411b363SPhilipp Reisner  * drbd_rs_del_all() - Gracefully remove all extents from the resync LRU
1337b411b363SPhilipp Reisner  * @mdev:	DRBD device.
1338b411b363SPhilipp Reisner  *
1339b411b363SPhilipp Reisner  * Returns 0 upon success, -EAGAIN if at least one reference count was
1340b411b363SPhilipp Reisner  * not zero.
1341b411b363SPhilipp Reisner  */
1342b411b363SPhilipp Reisner int drbd_rs_del_all(struct drbd_conf *mdev)
1343b411b363SPhilipp Reisner {
1344b411b363SPhilipp Reisner 	struct lc_element *e;
1345b411b363SPhilipp Reisner 	struct bm_extent *bm_ext;
1346b411b363SPhilipp Reisner 	int i;
1347b411b363SPhilipp Reisner 
1348b411b363SPhilipp Reisner 	spin_lock_irq(&mdev->al_lock);
1349b411b363SPhilipp Reisner 
1350b411b363SPhilipp Reisner 	if (get_ldev_if_state(mdev, D_FAILED)) {
1351b411b363SPhilipp Reisner 		/* ok, ->resync is there. */
1352b411b363SPhilipp Reisner 		for (i = 0; i < mdev->resync->nr_elements; i++) {
1353b411b363SPhilipp Reisner 			e = lc_element_by_index(mdev->resync, i);
1354b2b163ddSPhilipp Reisner 			bm_ext = lc_entry(e, struct bm_extent, lce);
1355b411b363SPhilipp Reisner 			if (bm_ext->lce.lc_number == LC_FREE)
1356b411b363SPhilipp Reisner 				continue;
1357b411b363SPhilipp Reisner 			if (bm_ext->lce.lc_number == mdev->resync_wenr) {
1358b411b363SPhilipp Reisner 				dev_info(DEV, "dropping %u in drbd_rs_del_all, apparently"
1359b411b363SPhilipp Reisner 				     " got 'synced' by application io\n",
1360b411b363SPhilipp Reisner 				     mdev->resync_wenr);
1361b411b363SPhilipp Reisner 				D_ASSERT(!test_bit(BME_LOCKED, &bm_ext->flags));
1362b411b363SPhilipp Reisner 				D_ASSERT(test_bit(BME_NO_WRITES, &bm_ext->flags));
1363b411b363SPhilipp Reisner 				clear_bit(BME_NO_WRITES, &bm_ext->flags);
1364b411b363SPhilipp Reisner 				mdev->resync_wenr = LC_FREE;
1365b411b363SPhilipp Reisner 				lc_put(mdev->resync, &bm_ext->lce);
1366b411b363SPhilipp Reisner 			}
1367b411b363SPhilipp Reisner 			if (bm_ext->lce.refcnt != 0) {
1368b411b363SPhilipp Reisner 				dev_info(DEV, "Retrying drbd_rs_del_all() later. "
1369b411b363SPhilipp Reisner 				     "refcnt=%d\n", bm_ext->lce.refcnt);
1370b411b363SPhilipp Reisner 				put_ldev(mdev);
1371b411b363SPhilipp Reisner 				spin_unlock_irq(&mdev->al_lock);
1372b411b363SPhilipp Reisner 				return -EAGAIN;
1373b411b363SPhilipp Reisner 			}
1374b411b363SPhilipp Reisner 			D_ASSERT(!test_bit(BME_LOCKED, &bm_ext->flags));
1375b411b363SPhilipp Reisner 			D_ASSERT(!test_bit(BME_NO_WRITES, &bm_ext->flags));
1376b411b363SPhilipp Reisner 			lc_del(mdev->resync, &bm_ext->lce);
1377b411b363SPhilipp Reisner 		}
1378b411b363SPhilipp Reisner 		D_ASSERT(mdev->resync->used == 0);
1379b411b363SPhilipp Reisner 		put_ldev(mdev);
1380b411b363SPhilipp Reisner 	}
1381b411b363SPhilipp Reisner 	spin_unlock_irq(&mdev->al_lock);
1382b411b363SPhilipp Reisner 
1383b411b363SPhilipp Reisner 	return 0;
1384b411b363SPhilipp Reisner }
1385b411b363SPhilipp Reisner 
1386b411b363SPhilipp Reisner /**
1387b411b363SPhilipp Reisner  * drbd_rs_failed_io() - Record information on a failure to resync the specified blocks
1388b411b363SPhilipp Reisner  * @mdev:	DRBD device.
1389b411b363SPhilipp Reisner  * @sector:	The sector number.
1390b411b363SPhilipp Reisner  * @size:	Size of failed IO operation, in byte.
1391b411b363SPhilipp Reisner  */
1392b411b363SPhilipp Reisner void drbd_rs_failed_io(struct drbd_conf *mdev, sector_t sector, int size)
1393b411b363SPhilipp Reisner {
1394b411b363SPhilipp Reisner 	/* Is called from worker and receiver context _only_ */
1395b411b363SPhilipp Reisner 	unsigned long sbnr, ebnr, lbnr;
1396b411b363SPhilipp Reisner 	unsigned long count;
1397b411b363SPhilipp Reisner 	sector_t esector, nr_sectors;
1398b411b363SPhilipp Reisner 	int wake_up = 0;
1399b411b363SPhilipp Reisner 
1400b411b363SPhilipp Reisner 	if (size <= 0 || (size & 0x1ff) != 0 || size > DRBD_MAX_SEGMENT_SIZE) {
1401b411b363SPhilipp Reisner 		dev_err(DEV, "drbd_rs_failed_io: sector=%llus size=%d nonsense!\n",
1402b411b363SPhilipp Reisner 				(unsigned long long)sector, size);
1403b411b363SPhilipp Reisner 		return;
1404b411b363SPhilipp Reisner 	}
1405b411b363SPhilipp Reisner 	nr_sectors = drbd_get_capacity(mdev->this_bdev);
1406b411b363SPhilipp Reisner 	esector = sector + (size >> 9) - 1;
1407b411b363SPhilipp Reisner 
1408b411b363SPhilipp Reisner 	ERR_IF(sector >= nr_sectors) return;
1409b411b363SPhilipp Reisner 	ERR_IF(esector >= nr_sectors) esector = (nr_sectors-1);
1410b411b363SPhilipp Reisner 
1411b411b363SPhilipp Reisner 	lbnr = BM_SECT_TO_BIT(nr_sectors-1);
1412b411b363SPhilipp Reisner 
1413b411b363SPhilipp Reisner 	/*
1414b411b363SPhilipp Reisner 	 * round up start sector, round down end sector.  we make sure we only
1415b411b363SPhilipp Reisner 	 * handle full, aligned, BM_BLOCK_SIZE (4K) blocks */
1416b411b363SPhilipp Reisner 	if (unlikely(esector < BM_SECT_PER_BIT-1))
1417b411b363SPhilipp Reisner 		return;
1418b411b363SPhilipp Reisner 	if (unlikely(esector == (nr_sectors-1)))
1419b411b363SPhilipp Reisner 		ebnr = lbnr;
1420b411b363SPhilipp Reisner 	else
1421b411b363SPhilipp Reisner 		ebnr = BM_SECT_TO_BIT(esector - (BM_SECT_PER_BIT-1));
1422b411b363SPhilipp Reisner 	sbnr = BM_SECT_TO_BIT(sector + BM_SECT_PER_BIT-1);
1423b411b363SPhilipp Reisner 
1424b411b363SPhilipp Reisner 	if (sbnr > ebnr)
1425b411b363SPhilipp Reisner 		return;
1426b411b363SPhilipp Reisner 
1427b411b363SPhilipp Reisner 	/*
1428b411b363SPhilipp Reisner 	 * ok, (capacity & 7) != 0 sometimes, but who cares...
1429b411b363SPhilipp Reisner 	 * we count rs_{total,left} in bits, not sectors.
1430b411b363SPhilipp Reisner 	 */
1431b411b363SPhilipp Reisner 	spin_lock_irq(&mdev->al_lock);
1432b411b363SPhilipp Reisner 	count = drbd_bm_count_bits(mdev, sbnr, ebnr);
1433b411b363SPhilipp Reisner 	if (count) {
1434b411b363SPhilipp Reisner 		mdev->rs_failed += count;
1435b411b363SPhilipp Reisner 
1436b411b363SPhilipp Reisner 		if (get_ldev(mdev)) {
1437b411b363SPhilipp Reisner 			drbd_try_clear_on_disk_bm(mdev, sector, count, FALSE);
1438b411b363SPhilipp Reisner 			put_ldev(mdev);
1439b411b363SPhilipp Reisner 		}
1440b411b363SPhilipp Reisner 
1441b411b363SPhilipp Reisner 		/* just wake_up unconditional now, various lc_chaged(),
1442b411b363SPhilipp Reisner 		 * lc_put() in drbd_try_clear_on_disk_bm(). */
1443b411b363SPhilipp Reisner 		wake_up = 1;
1444b411b363SPhilipp Reisner 	}
1445b411b363SPhilipp Reisner 	spin_unlock_irq(&mdev->al_lock);
1446b411b363SPhilipp Reisner 	if (wake_up)
1447b411b363SPhilipp Reisner 		wake_up(&mdev->al_wait);
1448b411b363SPhilipp Reisner }
1449