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