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