xref: /openbmc/linux/drivers/mtd/mtdswap.c (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
12b27bdccSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2a3215902SJarkko Lavinen /*
3a3215902SJarkko Lavinen  * Swap block device support for MTDs
4a3215902SJarkko Lavinen  * Turns an MTD device into a swap device with block wear leveling
5a3215902SJarkko Lavinen  *
6a3215902SJarkko Lavinen  * Copyright © 2007,2011 Nokia Corporation. All rights reserved.
7a3215902SJarkko Lavinen  *
8a3215902SJarkko Lavinen  * Authors: Jarkko Lavinen <jarkko.lavinen@nokia.com>
9a3215902SJarkko Lavinen  *
10a3215902SJarkko Lavinen  * Based on Richard Purdie's earlier implementation in 2007. Background
11a3215902SJarkko Lavinen  * support and lock-less operation written by Adrian Hunter.
12a3215902SJarkko Lavinen  */
13a3215902SJarkko Lavinen 
14a3215902SJarkko Lavinen #include <linux/kernel.h>
15a3215902SJarkko Lavinen #include <linux/module.h>
16a3215902SJarkko Lavinen #include <linux/mtd/mtd.h>
17a3215902SJarkko Lavinen #include <linux/mtd/blktrans.h>
18a3215902SJarkko Lavinen #include <linux/rbtree.h>
19a3215902SJarkko Lavinen #include <linux/sched.h>
20a3215902SJarkko Lavinen #include <linux/slab.h>
21a3215902SJarkko Lavinen #include <linux/vmalloc.h>
22322cbb50SChristoph Hellwig #include <linux/blkdev.h>
23a3215902SJarkko Lavinen #include <linux/swap.h>
24a3215902SJarkko Lavinen #include <linux/debugfs.h>
25a3215902SJarkko Lavinen #include <linux/seq_file.h>
26a3215902SJarkko Lavinen #include <linux/device.h>
27a3215902SJarkko Lavinen #include <linux/math64.h>
28a3215902SJarkko Lavinen 
29a3215902SJarkko Lavinen #define MTDSWAP_PREFIX "mtdswap"
30a3215902SJarkko Lavinen 
31a3215902SJarkko Lavinen /*
32a3215902SJarkko Lavinen  * The number of free eraseblocks when GC should stop
33a3215902SJarkko Lavinen  */
34a3215902SJarkko Lavinen #define CLEAN_BLOCK_THRESHOLD	20
35a3215902SJarkko Lavinen 
36a3215902SJarkko Lavinen /*
37a3215902SJarkko Lavinen  * Number of free eraseblocks below which GC can also collect low frag
38a3215902SJarkko Lavinen  * blocks.
39a3215902SJarkko Lavinen  */
40a5929b64SArvind Yadav #define LOW_FRAG_GC_THRESHOLD	5
41a3215902SJarkko Lavinen 
42a3215902SJarkko Lavinen /*
43a3215902SJarkko Lavinen  * Wear level cost amortization. We want to do wear leveling on the background
44a3215902SJarkko Lavinen  * without disturbing gc too much. This is made by defining max GC frequency.
45a3215902SJarkko Lavinen  * Frequency value 6 means 1/6 of the GC passes will pick an erase block based
46a3215902SJarkko Lavinen  * on the biggest wear difference rather than the biggest dirtiness.
47a3215902SJarkko Lavinen  *
48a3215902SJarkko Lavinen  * The lower freq2 should be chosen so that it makes sure the maximum erase
49a3215902SJarkko Lavinen  * difference will decrease even if a malicious application is deliberately
50a3215902SJarkko Lavinen  * trying to make erase differences large.
51a3215902SJarkko Lavinen  */
52a3215902SJarkko Lavinen #define MAX_ERASE_DIFF		4000
53a3215902SJarkko Lavinen #define COLLECT_NONDIRTY_BASE	MAX_ERASE_DIFF
54a3215902SJarkko Lavinen #define COLLECT_NONDIRTY_FREQ1	6
55a3215902SJarkko Lavinen #define COLLECT_NONDIRTY_FREQ2	4
56a3215902SJarkko Lavinen 
57a3215902SJarkko Lavinen #define PAGE_UNDEF		UINT_MAX
58a3215902SJarkko Lavinen #define BLOCK_UNDEF		UINT_MAX
59a3215902SJarkko Lavinen #define BLOCK_ERROR		(UINT_MAX - 1)
60a3215902SJarkko Lavinen #define BLOCK_MAX		(UINT_MAX - 2)
61a3215902SJarkko Lavinen 
62a3215902SJarkko Lavinen #define EBLOCK_BAD		(1 << 0)
63a3215902SJarkko Lavinen #define EBLOCK_NOMAGIC		(1 << 1)
64a3215902SJarkko Lavinen #define EBLOCK_BITFLIP		(1 << 2)
65a3215902SJarkko Lavinen #define EBLOCK_FAILED		(1 << 3)
66a3215902SJarkko Lavinen #define EBLOCK_READERR		(1 << 4)
67a3215902SJarkko Lavinen #define EBLOCK_IDX_SHIFT	5
68a3215902SJarkko Lavinen 
69a3215902SJarkko Lavinen struct swap_eb {
70a3215902SJarkko Lavinen 	struct rb_node rb;
71a3215902SJarkko Lavinen 	struct rb_root *root;
72a3215902SJarkko Lavinen 
73a3215902SJarkko Lavinen 	unsigned int flags;
74a3215902SJarkko Lavinen 	unsigned int active_count;
75a3215902SJarkko Lavinen 	unsigned int erase_count;
7692394b5cSBrian Norris 	unsigned int pad;		/* speeds up pointer decrement */
77a3215902SJarkko Lavinen };
78a3215902SJarkko Lavinen 
79a3215902SJarkko Lavinen #define MTDSWAP_ECNT_MIN(rbroot) (rb_entry(rb_first(rbroot), struct swap_eb, \
80a3215902SJarkko Lavinen 				rb)->erase_count)
81a3215902SJarkko Lavinen #define MTDSWAP_ECNT_MAX(rbroot) (rb_entry(rb_last(rbroot), struct swap_eb, \
82a3215902SJarkko Lavinen 				rb)->erase_count)
83a3215902SJarkko Lavinen 
84a3215902SJarkko Lavinen struct mtdswap_tree {
85a3215902SJarkko Lavinen 	struct rb_root root;
86a3215902SJarkko Lavinen 	unsigned int count;
87a3215902SJarkko Lavinen };
88a3215902SJarkko Lavinen 
89a3215902SJarkko Lavinen enum {
90a3215902SJarkko Lavinen 	MTDSWAP_CLEAN,
91a3215902SJarkko Lavinen 	MTDSWAP_USED,
92a3215902SJarkko Lavinen 	MTDSWAP_LOWFRAG,
93a3215902SJarkko Lavinen 	MTDSWAP_HIFRAG,
94a3215902SJarkko Lavinen 	MTDSWAP_DIRTY,
95a3215902SJarkko Lavinen 	MTDSWAP_BITFLIP,
96a3215902SJarkko Lavinen 	MTDSWAP_FAILING,
97a3215902SJarkko Lavinen 	MTDSWAP_TREE_CNT,
98a3215902SJarkko Lavinen };
99a3215902SJarkko Lavinen 
100a3215902SJarkko Lavinen struct mtdswap_dev {
101a3215902SJarkko Lavinen 	struct mtd_blktrans_dev *mbd_dev;
102a3215902SJarkko Lavinen 	struct mtd_info *mtd;
103a3215902SJarkko Lavinen 	struct device *dev;
104a3215902SJarkko Lavinen 
105a3215902SJarkko Lavinen 	unsigned int *page_data;
106a3215902SJarkko Lavinen 	unsigned int *revmap;
107a3215902SJarkko Lavinen 
108a3215902SJarkko Lavinen 	unsigned int eblks;
109a3215902SJarkko Lavinen 	unsigned int spare_eblks;
110a3215902SJarkko Lavinen 	unsigned int pages_per_eblk;
111a3215902SJarkko Lavinen 	unsigned int max_erase_count;
112a3215902SJarkko Lavinen 	struct swap_eb *eb_data;
113a3215902SJarkko Lavinen 
114a3215902SJarkko Lavinen 	struct mtdswap_tree trees[MTDSWAP_TREE_CNT];
115a3215902SJarkko Lavinen 
116a3215902SJarkko Lavinen 	unsigned long long sect_read_count;
117a3215902SJarkko Lavinen 	unsigned long long sect_write_count;
118a3215902SJarkko Lavinen 	unsigned long long mtd_write_count;
119a3215902SJarkko Lavinen 	unsigned long long mtd_read_count;
120a3215902SJarkko Lavinen 	unsigned long long discard_count;
121a3215902SJarkko Lavinen 	unsigned long long discard_page_count;
122a3215902SJarkko Lavinen 
123a3215902SJarkko Lavinen 	unsigned int curr_write_pos;
124a3215902SJarkko Lavinen 	struct swap_eb *curr_write;
125a3215902SJarkko Lavinen 
126a3215902SJarkko Lavinen 	char *page_buf;
127a3215902SJarkko Lavinen 	char *oob_buf;
128a3215902SJarkko Lavinen };
129a3215902SJarkko Lavinen 
130a3215902SJarkko Lavinen struct mtdswap_oobdata {
131a3215902SJarkko Lavinen 	__le16 magic;
132a3215902SJarkko Lavinen 	__le32 count;
13331f75462SBrian Norris } __packed;
134a3215902SJarkko Lavinen 
135a3215902SJarkko Lavinen #define MTDSWAP_MAGIC_CLEAN	0x2095
136a3215902SJarkko Lavinen #define MTDSWAP_MAGIC_DIRTY	(MTDSWAP_MAGIC_CLEAN + 1)
137a3215902SJarkko Lavinen #define MTDSWAP_TYPE_CLEAN	0
138a3215902SJarkko Lavinen #define MTDSWAP_TYPE_DIRTY	1
139a3215902SJarkko Lavinen #define MTDSWAP_OOBSIZE		sizeof(struct mtdswap_oobdata)
140a3215902SJarkko Lavinen 
141a3215902SJarkko Lavinen #define MTDSWAP_ERASE_RETRIES	3 /* Before marking erase block bad */
142a3215902SJarkko Lavinen #define MTDSWAP_IO_RETRIES	3
143a3215902SJarkko Lavinen 
144a3215902SJarkko Lavinen enum {
145a3215902SJarkko Lavinen 	MTDSWAP_SCANNED_CLEAN,
146a3215902SJarkko Lavinen 	MTDSWAP_SCANNED_DIRTY,
147a3215902SJarkko Lavinen 	MTDSWAP_SCANNED_BITFLIP,
148a3215902SJarkko Lavinen 	MTDSWAP_SCANNED_BAD,
149a3215902SJarkko Lavinen };
150a3215902SJarkko Lavinen 
151a3215902SJarkko Lavinen /*
152a3215902SJarkko Lavinen  * In the worst case mtdswap_writesect() has allocated the last clean
153a3215902SJarkko Lavinen  * page from the current block and is then pre-empted by the GC
154a3215902SJarkko Lavinen  * thread. The thread can consume a full erase block when moving a
155a3215902SJarkko Lavinen  * block.
156a3215902SJarkko Lavinen  */
157a3215902SJarkko Lavinen #define MIN_SPARE_EBLOCKS	2
158a3215902SJarkko Lavinen #define MIN_ERASE_BLOCKS	(MIN_SPARE_EBLOCKS + 1)
159a3215902SJarkko Lavinen 
160a3215902SJarkko Lavinen #define TREE_ROOT(d, name) (&d->trees[MTDSWAP_ ## name].root)
161a3215902SJarkko Lavinen #define TREE_EMPTY(d, name) (TREE_ROOT(d, name)->rb_node == NULL)
162a3215902SJarkko Lavinen #define TREE_NONEMPTY(d, name) (!TREE_EMPTY(d, name))
163a3215902SJarkko Lavinen #define TREE_COUNT(d, name) (d->trees[MTDSWAP_ ## name].count)
164a3215902SJarkko Lavinen 
165a3215902SJarkko Lavinen #define MTDSWAP_MBD_TO_MTDSWAP(dev) ((struct mtdswap_dev *)dev->priv)
166a3215902SJarkko Lavinen 
167a3215902SJarkko Lavinen static char partitions[128] = "";
168a3215902SJarkko Lavinen module_param_string(partitions, partitions, sizeof(partitions), 0444);
169a3215902SJarkko Lavinen MODULE_PARM_DESC(partitions, "MTD partition numbers to use as swap "
170a3215902SJarkko Lavinen 		"partitions=\"1,3,5\"");
171a3215902SJarkko Lavinen 
172a3215902SJarkko Lavinen static unsigned int spare_eblocks = 10;
173a3215902SJarkko Lavinen module_param(spare_eblocks, uint, 0444);
174a3215902SJarkko Lavinen MODULE_PARM_DESC(spare_eblocks, "Percentage of spare erase blocks for "
175a3215902SJarkko Lavinen 		"garbage collection (default 10%)");
176a3215902SJarkko Lavinen 
177a3215902SJarkko Lavinen static bool header; /* false */
178a3215902SJarkko Lavinen module_param(header, bool, 0444);
179a3215902SJarkko Lavinen MODULE_PARM_DESC(header,
180a3215902SJarkko Lavinen 		"Include builtin swap header (default 0, without header)");
181a3215902SJarkko Lavinen 
182a3215902SJarkko Lavinen static int mtdswap_gc(struct mtdswap_dev *d, unsigned int background);
183a3215902SJarkko Lavinen 
mtdswap_eb_offset(struct mtdswap_dev * d,struct swap_eb * eb)184a3215902SJarkko Lavinen static loff_t mtdswap_eb_offset(struct mtdswap_dev *d, struct swap_eb *eb)
185a3215902SJarkko Lavinen {
186a3215902SJarkko Lavinen 	return (loff_t)(eb - d->eb_data) * d->mtd->erasesize;
187a3215902SJarkko Lavinen }
188a3215902SJarkko Lavinen 
mtdswap_eb_detach(struct mtdswap_dev * d,struct swap_eb * eb)189a3215902SJarkko Lavinen static void mtdswap_eb_detach(struct mtdswap_dev *d, struct swap_eb *eb)
190a3215902SJarkko Lavinen {
191a3215902SJarkko Lavinen 	unsigned int oldidx;
192a3215902SJarkko Lavinen 	struct mtdswap_tree *tp;
193a3215902SJarkko Lavinen 
194a3215902SJarkko Lavinen 	if (eb->root) {
195a3215902SJarkko Lavinen 		tp = container_of(eb->root, struct mtdswap_tree, root);
196a3215902SJarkko Lavinen 		oldidx = tp - &d->trees[0];
197a3215902SJarkko Lavinen 
198a3215902SJarkko Lavinen 		d->trees[oldidx].count--;
199a3215902SJarkko Lavinen 		rb_erase(&eb->rb, eb->root);
200a3215902SJarkko Lavinen 	}
201a3215902SJarkko Lavinen }
202a3215902SJarkko Lavinen 
__mtdswap_rb_add(struct rb_root * root,struct swap_eb * eb)203a3215902SJarkko Lavinen static void __mtdswap_rb_add(struct rb_root *root, struct swap_eb *eb)
204a3215902SJarkko Lavinen {
205a3215902SJarkko Lavinen 	struct rb_node **p, *parent = NULL;
206a3215902SJarkko Lavinen 	struct swap_eb *cur;
207a3215902SJarkko Lavinen 
208a3215902SJarkko Lavinen 	p = &root->rb_node;
209a3215902SJarkko Lavinen 	while (*p) {
210a3215902SJarkko Lavinen 		parent = *p;
211a3215902SJarkko Lavinen 		cur = rb_entry(parent, struct swap_eb, rb);
212a3215902SJarkko Lavinen 		if (eb->erase_count > cur->erase_count)
213a3215902SJarkko Lavinen 			p = &(*p)->rb_right;
214a3215902SJarkko Lavinen 		else
215a3215902SJarkko Lavinen 			p = &(*p)->rb_left;
216a3215902SJarkko Lavinen 	}
217a3215902SJarkko Lavinen 
218a3215902SJarkko Lavinen 	rb_link_node(&eb->rb, parent, p);
219a3215902SJarkko Lavinen 	rb_insert_color(&eb->rb, root);
220a3215902SJarkko Lavinen }
221a3215902SJarkko Lavinen 
mtdswap_rb_add(struct mtdswap_dev * d,struct swap_eb * eb,int idx)222a3215902SJarkko Lavinen static void mtdswap_rb_add(struct mtdswap_dev *d, struct swap_eb *eb, int idx)
223a3215902SJarkko Lavinen {
224a3215902SJarkko Lavinen 	struct rb_root *root;
225a3215902SJarkko Lavinen 
226a3215902SJarkko Lavinen 	if (eb->root == &d->trees[idx].root)
227a3215902SJarkko Lavinen 		return;
228a3215902SJarkko Lavinen 
229a3215902SJarkko Lavinen 	mtdswap_eb_detach(d, eb);
230a3215902SJarkko Lavinen 	root = &d->trees[idx].root;
231a3215902SJarkko Lavinen 	__mtdswap_rb_add(root, eb);
232a3215902SJarkko Lavinen 	eb->root = root;
233a3215902SJarkko Lavinen 	d->trees[idx].count++;
234a3215902SJarkko Lavinen }
235a3215902SJarkko Lavinen 
mtdswap_rb_index(struct rb_root * root,unsigned int idx)236a3215902SJarkko Lavinen static struct rb_node *mtdswap_rb_index(struct rb_root *root, unsigned int idx)
237a3215902SJarkko Lavinen {
238a3215902SJarkko Lavinen 	struct rb_node *p;
239a3215902SJarkko Lavinen 	unsigned int i;
240a3215902SJarkko Lavinen 
241a3215902SJarkko Lavinen 	p = rb_first(root);
242a3215902SJarkko Lavinen 	i = 0;
243a3215902SJarkko Lavinen 	while (i < idx && p) {
244a3215902SJarkko Lavinen 		p = rb_next(p);
245a3215902SJarkko Lavinen 		i++;
246a3215902SJarkko Lavinen 	}
247a3215902SJarkko Lavinen 
248a3215902SJarkko Lavinen 	return p;
249a3215902SJarkko Lavinen }
250a3215902SJarkko Lavinen 
mtdswap_handle_badblock(struct mtdswap_dev * d,struct swap_eb * eb)251a3215902SJarkko Lavinen static int mtdswap_handle_badblock(struct mtdswap_dev *d, struct swap_eb *eb)
252a3215902SJarkko Lavinen {
253a3215902SJarkko Lavinen 	int ret;
254a3215902SJarkko Lavinen 	loff_t offset;
255a3215902SJarkko Lavinen 
256a3215902SJarkko Lavinen 	d->spare_eblks--;
257a3215902SJarkko Lavinen 	eb->flags |= EBLOCK_BAD;
258a3215902SJarkko Lavinen 	mtdswap_eb_detach(d, eb);
259a3215902SJarkko Lavinen 	eb->root = NULL;
260a3215902SJarkko Lavinen 
261a3215902SJarkko Lavinen 	/* badblocks not supported */
262800ffd34SArtem Bityutskiy 	if (!mtd_can_have_bb(d->mtd))
263a3215902SJarkko Lavinen 		return 1;
264a3215902SJarkko Lavinen 
265a3215902SJarkko Lavinen 	offset = mtdswap_eb_offset(d, eb);
266a3215902SJarkko Lavinen 	dev_warn(d->dev, "Marking bad block at %08llx\n", offset);
2675942ddbcSArtem Bityutskiy 	ret = mtd_block_markbad(d->mtd, offset);
268a3215902SJarkko Lavinen 
269a3215902SJarkko Lavinen 	if (ret) {
270a3215902SJarkko Lavinen 		dev_warn(d->dev, "Mark block bad failed for block at %08llx "
271a3215902SJarkko Lavinen 			"error %d\n", offset, ret);
272a3215902SJarkko Lavinen 		return ret;
273a3215902SJarkko Lavinen 	}
274a3215902SJarkko Lavinen 
275a3215902SJarkko Lavinen 	return 1;
276a3215902SJarkko Lavinen 
277a3215902SJarkko Lavinen }
278a3215902SJarkko Lavinen 
mtdswap_handle_write_error(struct mtdswap_dev * d,struct swap_eb * eb)279a3215902SJarkko Lavinen static int mtdswap_handle_write_error(struct mtdswap_dev *d, struct swap_eb *eb)
280a3215902SJarkko Lavinen {
281a3215902SJarkko Lavinen 	unsigned int marked = eb->flags & EBLOCK_FAILED;
282a3215902SJarkko Lavinen 	struct swap_eb *curr_write = d->curr_write;
283a3215902SJarkko Lavinen 
284a3215902SJarkko Lavinen 	eb->flags |= EBLOCK_FAILED;
285a3215902SJarkko Lavinen 	if (curr_write == eb) {
286a3215902SJarkko Lavinen 		d->curr_write = NULL;
287a3215902SJarkko Lavinen 
288a3215902SJarkko Lavinen 		if (!marked && d->curr_write_pos != 0) {
289a3215902SJarkko Lavinen 			mtdswap_rb_add(d, eb, MTDSWAP_FAILING);
290a3215902SJarkko Lavinen 			return 0;
291a3215902SJarkko Lavinen 		}
292a3215902SJarkko Lavinen 	}
293a3215902SJarkko Lavinen 
294a3215902SJarkko Lavinen 	return mtdswap_handle_badblock(d, eb);
295a3215902SJarkko Lavinen }
296a3215902SJarkko Lavinen 
mtdswap_read_oob(struct mtdswap_dev * d,loff_t from,struct mtd_oob_ops * ops)297a3215902SJarkko Lavinen static int mtdswap_read_oob(struct mtdswap_dev *d, loff_t from,
298a3215902SJarkko Lavinen 			struct mtd_oob_ops *ops)
299a3215902SJarkko Lavinen {
300fd2819bbSArtem Bityutskiy 	int ret = mtd_read_oob(d->mtd, from, ops);
301a3215902SJarkko Lavinen 
302d57f4054SBrian Norris 	if (mtd_is_bitflip(ret))
303a3215902SJarkko Lavinen 		return ret;
304a3215902SJarkko Lavinen 
305a3215902SJarkko Lavinen 	if (ret) {
306a3215902SJarkko Lavinen 		dev_warn(d->dev, "Read OOB failed %d for block at %08llx\n",
307a3215902SJarkko Lavinen 			ret, from);
308a3215902SJarkko Lavinen 		return ret;
309a3215902SJarkko Lavinen 	}
310a3215902SJarkko Lavinen 
311a3215902SJarkko Lavinen 	if (ops->oobretlen < ops->ooblen) {
312a3215902SJarkko Lavinen 		dev_warn(d->dev, "Read OOB return short read (%zd bytes not "
31389d8d320SDavid Woodhouse 			"%zd) for block at %08llx\n",
314a3215902SJarkko Lavinen 			ops->oobretlen, ops->ooblen, from);
315a3215902SJarkko Lavinen 		return -EIO;
316a3215902SJarkko Lavinen 	}
317a3215902SJarkko Lavinen 
318a3215902SJarkko Lavinen 	return 0;
319a3215902SJarkko Lavinen }
320a3215902SJarkko Lavinen 
mtdswap_read_markers(struct mtdswap_dev * d,struct swap_eb * eb)321a3215902SJarkko Lavinen static int mtdswap_read_markers(struct mtdswap_dev *d, struct swap_eb *eb)
322a3215902SJarkko Lavinen {
323a3215902SJarkko Lavinen 	struct mtdswap_oobdata *data, *data2;
324a3215902SJarkko Lavinen 	int ret;
325a3215902SJarkko Lavinen 	loff_t offset;
326*745df179SMichał Kępień 	struct mtd_oob_ops ops = { };
327a3215902SJarkko Lavinen 
328a3215902SJarkko Lavinen 	offset = mtdswap_eb_offset(d, eb);
329a3215902SJarkko Lavinen 
330a3215902SJarkko Lavinen 	/* Check first if the block is bad. */
3318f461a73SArtem Bityutskiy 	if (mtd_can_have_bb(d->mtd) && mtd_block_isbad(d->mtd, offset))
332a3215902SJarkko Lavinen 		return MTDSWAP_SCANNED_BAD;
333a3215902SJarkko Lavinen 
334f5b8aa78SBoris BREZILLON 	ops.ooblen = 2 * d->mtd->oobavail;
335a3215902SJarkko Lavinen 	ops.oobbuf = d->oob_buf;
336a3215902SJarkko Lavinen 	ops.ooboffs = 0;
337a3215902SJarkko Lavinen 	ops.datbuf = NULL;
3380612b9ddSBrian Norris 	ops.mode = MTD_OPS_AUTO_OOB;
339a3215902SJarkko Lavinen 
340a3215902SJarkko Lavinen 	ret = mtdswap_read_oob(d, offset, &ops);
341a3215902SJarkko Lavinen 
342d57f4054SBrian Norris 	if (ret && !mtd_is_bitflip(ret))
343a3215902SJarkko Lavinen 		return ret;
344a3215902SJarkko Lavinen 
345a3215902SJarkko Lavinen 	data = (struct mtdswap_oobdata *)d->oob_buf;
346a3215902SJarkko Lavinen 	data2 = (struct mtdswap_oobdata *)
347f5b8aa78SBoris BREZILLON 		(d->oob_buf + d->mtd->oobavail);
348a3215902SJarkko Lavinen 
349a3215902SJarkko Lavinen 	if (le16_to_cpu(data->magic) == MTDSWAP_MAGIC_CLEAN) {
350a3215902SJarkko Lavinen 		eb->erase_count = le32_to_cpu(data->count);
351d57f4054SBrian Norris 		if (mtd_is_bitflip(ret))
352a3215902SJarkko Lavinen 			ret = MTDSWAP_SCANNED_BITFLIP;
353a3215902SJarkko Lavinen 		else {
354a3215902SJarkko Lavinen 			if (le16_to_cpu(data2->magic) == MTDSWAP_MAGIC_DIRTY)
355a3215902SJarkko Lavinen 				ret = MTDSWAP_SCANNED_DIRTY;
356a3215902SJarkko Lavinen 			else
357a3215902SJarkko Lavinen 				ret = MTDSWAP_SCANNED_CLEAN;
358a3215902SJarkko Lavinen 		}
359a3215902SJarkko Lavinen 	} else {
360a3215902SJarkko Lavinen 		eb->flags |= EBLOCK_NOMAGIC;
361a3215902SJarkko Lavinen 		ret = MTDSWAP_SCANNED_DIRTY;
362a3215902SJarkko Lavinen 	}
363a3215902SJarkko Lavinen 
364a3215902SJarkko Lavinen 	return ret;
365a3215902SJarkko Lavinen }
366a3215902SJarkko Lavinen 
mtdswap_write_marker(struct mtdswap_dev * d,struct swap_eb * eb,u16 marker)367a3215902SJarkko Lavinen static int mtdswap_write_marker(struct mtdswap_dev *d, struct swap_eb *eb,
368a3215902SJarkko Lavinen 				u16 marker)
369a3215902SJarkko Lavinen {
370a3215902SJarkko Lavinen 	struct mtdswap_oobdata n;
371a3215902SJarkko Lavinen 	int ret;
372a3215902SJarkko Lavinen 	loff_t offset;
373*745df179SMichał Kępień 	struct mtd_oob_ops ops = { };
374a3215902SJarkko Lavinen 
375a3215902SJarkko Lavinen 	ops.ooboffs = 0;
376a3215902SJarkko Lavinen 	ops.oobbuf = (uint8_t *)&n;
3770612b9ddSBrian Norris 	ops.mode = MTD_OPS_AUTO_OOB;
378a3215902SJarkko Lavinen 	ops.datbuf = NULL;
379a3215902SJarkko Lavinen 
380a3215902SJarkko Lavinen 	if (marker == MTDSWAP_TYPE_CLEAN) {
381a3215902SJarkko Lavinen 		n.magic = cpu_to_le16(MTDSWAP_MAGIC_CLEAN);
382a3215902SJarkko Lavinen 		n.count = cpu_to_le32(eb->erase_count);
383a3215902SJarkko Lavinen 		ops.ooblen = MTDSWAP_OOBSIZE;
384a3215902SJarkko Lavinen 		offset = mtdswap_eb_offset(d, eb);
385a3215902SJarkko Lavinen 	} else {
386a3215902SJarkko Lavinen 		n.magic = cpu_to_le16(MTDSWAP_MAGIC_DIRTY);
387a3215902SJarkko Lavinen 		ops.ooblen = sizeof(n.magic);
388a3215902SJarkko Lavinen 		offset = mtdswap_eb_offset(d, eb) + d->mtd->writesize;
389a3215902SJarkko Lavinen 	}
390a3215902SJarkko Lavinen 
391a2cc5ba0SArtem Bityutskiy 	ret = mtd_write_oob(d->mtd, offset, &ops);
392a3215902SJarkko Lavinen 
393a3215902SJarkko Lavinen 	if (ret) {
394a3215902SJarkko Lavinen 		dev_warn(d->dev, "Write OOB failed for block at %08llx "
395a3215902SJarkko Lavinen 			"error %d\n", offset, ret);
396d57f4054SBrian Norris 		if (ret == -EIO || mtd_is_eccerr(ret))
397a3215902SJarkko Lavinen 			mtdswap_handle_write_error(d, eb);
398a3215902SJarkko Lavinen 		return ret;
399a3215902SJarkko Lavinen 	}
400a3215902SJarkko Lavinen 
401a3215902SJarkko Lavinen 	if (ops.oobretlen != ops.ooblen) {
402a3215902SJarkko Lavinen 		dev_warn(d->dev, "Short OOB write for block at %08llx: "
40389d8d320SDavid Woodhouse 			"%zd not %zd\n",
404a3215902SJarkko Lavinen 			offset, ops.oobretlen, ops.ooblen);
405a3215902SJarkko Lavinen 		return ret;
406a3215902SJarkko Lavinen 	}
407a3215902SJarkko Lavinen 
408a3215902SJarkko Lavinen 	return 0;
409a3215902SJarkko Lavinen }
410a3215902SJarkko Lavinen 
411a3215902SJarkko Lavinen /*
412a3215902SJarkko Lavinen  * Are there any erase blocks without MAGIC_CLEAN header, presumably
413a3215902SJarkko Lavinen  * because power was cut off after erase but before header write? We
414a3215902SJarkko Lavinen  * need to guestimate the erase count.
415a3215902SJarkko Lavinen  */
mtdswap_check_counts(struct mtdswap_dev * d)416a3215902SJarkko Lavinen static void mtdswap_check_counts(struct mtdswap_dev *d)
417a3215902SJarkko Lavinen {
418a3215902SJarkko Lavinen 	struct rb_root hist_root = RB_ROOT;
419a3215902SJarkko Lavinen 	struct rb_node *medrb;
420a3215902SJarkko Lavinen 	struct swap_eb *eb;
421a3215902SJarkko Lavinen 	unsigned int i, cnt, median;
422a3215902SJarkko Lavinen 
423a3215902SJarkko Lavinen 	cnt = 0;
424a3215902SJarkko Lavinen 	for (i = 0; i < d->eblks; i++) {
425a3215902SJarkko Lavinen 		eb = d->eb_data + i;
426a3215902SJarkko Lavinen 
427a3215902SJarkko Lavinen 		if (eb->flags & (EBLOCK_NOMAGIC | EBLOCK_BAD | EBLOCK_READERR))
428a3215902SJarkko Lavinen 			continue;
429a3215902SJarkko Lavinen 
430a3215902SJarkko Lavinen 		__mtdswap_rb_add(&hist_root, eb);
431a3215902SJarkko Lavinen 		cnt++;
432a3215902SJarkko Lavinen 	}
433a3215902SJarkko Lavinen 
434a3215902SJarkko Lavinen 	if (cnt == 0)
435a3215902SJarkko Lavinen 		return;
436a3215902SJarkko Lavinen 
437a3215902SJarkko Lavinen 	medrb = mtdswap_rb_index(&hist_root, cnt / 2);
438a3215902SJarkko Lavinen 	median = rb_entry(medrb, struct swap_eb, rb)->erase_count;
439a3215902SJarkko Lavinen 
440a3215902SJarkko Lavinen 	d->max_erase_count = MTDSWAP_ECNT_MAX(&hist_root);
441a3215902SJarkko Lavinen 
442a3215902SJarkko Lavinen 	for (i = 0; i < d->eblks; i++) {
443a3215902SJarkko Lavinen 		eb = d->eb_data + i;
444a3215902SJarkko Lavinen 
445a3215902SJarkko Lavinen 		if (eb->flags & (EBLOCK_NOMAGIC | EBLOCK_READERR))
446a3215902SJarkko Lavinen 			eb->erase_count = median;
447a3215902SJarkko Lavinen 
448a3215902SJarkko Lavinen 		if (eb->flags & (EBLOCK_NOMAGIC | EBLOCK_BAD | EBLOCK_READERR))
449a3215902SJarkko Lavinen 			continue;
450a3215902SJarkko Lavinen 
451a3215902SJarkko Lavinen 		rb_erase(&eb->rb, &hist_root);
452a3215902SJarkko Lavinen 	}
453a3215902SJarkko Lavinen }
454a3215902SJarkko Lavinen 
mtdswap_scan_eblks(struct mtdswap_dev * d)455a3215902SJarkko Lavinen static void mtdswap_scan_eblks(struct mtdswap_dev *d)
456a3215902SJarkko Lavinen {
457a3215902SJarkko Lavinen 	int status;
458a3215902SJarkko Lavinen 	unsigned int i, idx;
459a3215902SJarkko Lavinen 	struct swap_eb *eb;
460a3215902SJarkko Lavinen 
461a3215902SJarkko Lavinen 	for (i = 0; i < d->eblks; i++) {
462a3215902SJarkko Lavinen 		eb = d->eb_data + i;
463a3215902SJarkko Lavinen 
464a3215902SJarkko Lavinen 		status = mtdswap_read_markers(d, eb);
465a3215902SJarkko Lavinen 		if (status < 0)
466a3215902SJarkko Lavinen 			eb->flags |= EBLOCK_READERR;
467a3215902SJarkko Lavinen 		else if (status == MTDSWAP_SCANNED_BAD) {
468a3215902SJarkko Lavinen 			eb->flags |= EBLOCK_BAD;
469a3215902SJarkko Lavinen 			continue;
470a3215902SJarkko Lavinen 		}
471a3215902SJarkko Lavinen 
472a3215902SJarkko Lavinen 		switch (status) {
473a3215902SJarkko Lavinen 		case MTDSWAP_SCANNED_CLEAN:
474a3215902SJarkko Lavinen 			idx = MTDSWAP_CLEAN;
475a3215902SJarkko Lavinen 			break;
476a3215902SJarkko Lavinen 		case MTDSWAP_SCANNED_DIRTY:
477a3215902SJarkko Lavinen 		case MTDSWAP_SCANNED_BITFLIP:
478a3215902SJarkko Lavinen 			idx = MTDSWAP_DIRTY;
479a3215902SJarkko Lavinen 			break;
480a3215902SJarkko Lavinen 		default:
481a3215902SJarkko Lavinen 			idx = MTDSWAP_FAILING;
482a3215902SJarkko Lavinen 		}
483a3215902SJarkko Lavinen 
484a3215902SJarkko Lavinen 		eb->flags |= (idx << EBLOCK_IDX_SHIFT);
485a3215902SJarkko Lavinen 	}
486a3215902SJarkko Lavinen 
487a3215902SJarkko Lavinen 	mtdswap_check_counts(d);
488a3215902SJarkko Lavinen 
489a3215902SJarkko Lavinen 	for (i = 0; i < d->eblks; i++) {
490a3215902SJarkko Lavinen 		eb = d->eb_data + i;
491a3215902SJarkko Lavinen 
492a3215902SJarkko Lavinen 		if (eb->flags & EBLOCK_BAD)
493a3215902SJarkko Lavinen 			continue;
494a3215902SJarkko Lavinen 
495a3215902SJarkko Lavinen 		idx = eb->flags >> EBLOCK_IDX_SHIFT;
496a3215902SJarkko Lavinen 		mtdswap_rb_add(d, eb, idx);
497a3215902SJarkko Lavinen 	}
498a3215902SJarkko Lavinen }
499a3215902SJarkko Lavinen 
500a3215902SJarkko Lavinen /*
501a3215902SJarkko Lavinen  * Place eblk into a tree corresponding to its number of active blocks
502a3215902SJarkko Lavinen  * it contains.
503a3215902SJarkko Lavinen  */
mtdswap_store_eb(struct mtdswap_dev * d,struct swap_eb * eb)504a3215902SJarkko Lavinen static void mtdswap_store_eb(struct mtdswap_dev *d, struct swap_eb *eb)
505a3215902SJarkko Lavinen {
506a3215902SJarkko Lavinen 	unsigned int weight = eb->active_count;
507a3215902SJarkko Lavinen 	unsigned int maxweight = d->pages_per_eblk;
508a3215902SJarkko Lavinen 
509a3215902SJarkko Lavinen 	if (eb == d->curr_write)
510a3215902SJarkko Lavinen 		return;
511a3215902SJarkko Lavinen 
512a3215902SJarkko Lavinen 	if (eb->flags & EBLOCK_BITFLIP)
513a3215902SJarkko Lavinen 		mtdswap_rb_add(d, eb, MTDSWAP_BITFLIP);
514a3215902SJarkko Lavinen 	else if (eb->flags & (EBLOCK_READERR | EBLOCK_FAILED))
515a3215902SJarkko Lavinen 		mtdswap_rb_add(d, eb, MTDSWAP_FAILING);
516a3215902SJarkko Lavinen 	if (weight == maxweight)
517a3215902SJarkko Lavinen 		mtdswap_rb_add(d, eb, MTDSWAP_USED);
518a3215902SJarkko Lavinen 	else if (weight == 0)
519a3215902SJarkko Lavinen 		mtdswap_rb_add(d, eb, MTDSWAP_DIRTY);
520a3215902SJarkko Lavinen 	else if (weight > (maxweight/2))
521a3215902SJarkko Lavinen 		mtdswap_rb_add(d, eb, MTDSWAP_LOWFRAG);
522a3215902SJarkko Lavinen 	else
523a3215902SJarkko Lavinen 		mtdswap_rb_add(d, eb, MTDSWAP_HIFRAG);
524a3215902SJarkko Lavinen }
525a3215902SJarkko Lavinen 
mtdswap_erase_block(struct mtdswap_dev * d,struct swap_eb * eb)526a3215902SJarkko Lavinen static int mtdswap_erase_block(struct mtdswap_dev *d, struct swap_eb *eb)
527a3215902SJarkko Lavinen {
528a3215902SJarkko Lavinen 	struct mtd_info *mtd = d->mtd;
529a3215902SJarkko Lavinen 	struct erase_info erase;
530a3215902SJarkko Lavinen 	unsigned int retries = 0;
531a3215902SJarkko Lavinen 	int ret;
532a3215902SJarkko Lavinen 
533a3215902SJarkko Lavinen 	eb->erase_count++;
534a3215902SJarkko Lavinen 	if (eb->erase_count > d->max_erase_count)
535a3215902SJarkko Lavinen 		d->max_erase_count = eb->erase_count;
536a3215902SJarkko Lavinen 
537a3215902SJarkko Lavinen retry:
538a3215902SJarkko Lavinen 	memset(&erase, 0, sizeof(struct erase_info));
539a3215902SJarkko Lavinen 	erase.addr	= mtdswap_eb_offset(d, eb);
540a3215902SJarkko Lavinen 	erase.len	= mtd->erasesize;
541a3215902SJarkko Lavinen 
5427e1f0dc0SArtem Bityutskiy 	ret = mtd_erase(mtd, &erase);
543a3215902SJarkko Lavinen 	if (ret) {
544e21fa86aSYang Ruirui 		if (retries++ < MTDSWAP_ERASE_RETRIES) {
545a3215902SJarkko Lavinen 			dev_warn(d->dev,
546a3215902SJarkko Lavinen 				"erase of erase block %#llx on %s failed",
547a3215902SJarkko Lavinen 				erase.addr, mtd->name);
548a3215902SJarkko Lavinen 			yield();
549a3215902SJarkko Lavinen 			goto retry;
550a3215902SJarkko Lavinen 		}
551a3215902SJarkko Lavinen 
552a3215902SJarkko Lavinen 		dev_err(d->dev, "Cannot erase erase block %#llx on %s\n",
553a3215902SJarkko Lavinen 			erase.addr, mtd->name);
554a3215902SJarkko Lavinen 
555a3215902SJarkko Lavinen 		mtdswap_handle_badblock(d, eb);
556a3215902SJarkko Lavinen 		return -EIO;
557a3215902SJarkko Lavinen 	}
558a3215902SJarkko Lavinen 
559a3215902SJarkko Lavinen 	return 0;
560a3215902SJarkko Lavinen }
561a3215902SJarkko Lavinen 
mtdswap_map_free_block(struct mtdswap_dev * d,unsigned int page,unsigned int * block)562a3215902SJarkko Lavinen static int mtdswap_map_free_block(struct mtdswap_dev *d, unsigned int page,
563a3215902SJarkko Lavinen 				unsigned int *block)
564a3215902SJarkko Lavinen {
565a3215902SJarkko Lavinen 	int ret;
566a3215902SJarkko Lavinen 	struct swap_eb *old_eb = d->curr_write;
567a3215902SJarkko Lavinen 	struct rb_root *clean_root;
568a3215902SJarkko Lavinen 	struct swap_eb *eb;
569a3215902SJarkko Lavinen 
570a3215902SJarkko Lavinen 	if (old_eb == NULL || d->curr_write_pos >= d->pages_per_eblk) {
571a3215902SJarkko Lavinen 		do {
572a3215902SJarkko Lavinen 			if (TREE_EMPTY(d, CLEAN))
573a3215902SJarkko Lavinen 				return -ENOSPC;
574a3215902SJarkko Lavinen 
575a3215902SJarkko Lavinen 			clean_root = TREE_ROOT(d, CLEAN);
576a3215902SJarkko Lavinen 			eb = rb_entry(rb_first(clean_root), struct swap_eb, rb);
577a3215902SJarkko Lavinen 			rb_erase(&eb->rb, clean_root);
578a3215902SJarkko Lavinen 			eb->root = NULL;
579a3215902SJarkko Lavinen 			TREE_COUNT(d, CLEAN)--;
580a3215902SJarkko Lavinen 
581a3215902SJarkko Lavinen 			ret = mtdswap_write_marker(d, eb, MTDSWAP_TYPE_DIRTY);
582d57f4054SBrian Norris 		} while (ret == -EIO || mtd_is_eccerr(ret));
583a3215902SJarkko Lavinen 
584a3215902SJarkko Lavinen 		if (ret)
585a3215902SJarkko Lavinen 			return ret;
586a3215902SJarkko Lavinen 
587a3215902SJarkko Lavinen 		d->curr_write_pos = 0;
588a3215902SJarkko Lavinen 		d->curr_write = eb;
589a3215902SJarkko Lavinen 		if (old_eb)
590a3215902SJarkko Lavinen 			mtdswap_store_eb(d, old_eb);
591a3215902SJarkko Lavinen 	}
592a3215902SJarkko Lavinen 
593a3215902SJarkko Lavinen 	*block = (d->curr_write - d->eb_data) * d->pages_per_eblk +
594a3215902SJarkko Lavinen 		d->curr_write_pos;
595a3215902SJarkko Lavinen 
596a3215902SJarkko Lavinen 	d->curr_write->active_count++;
597a3215902SJarkko Lavinen 	d->revmap[*block] = page;
598a3215902SJarkko Lavinen 	d->curr_write_pos++;
599a3215902SJarkko Lavinen 
600a3215902SJarkko Lavinen 	return 0;
601a3215902SJarkko Lavinen }
602a3215902SJarkko Lavinen 
mtdswap_free_page_cnt(struct mtdswap_dev * d)603a3215902SJarkko Lavinen static unsigned int mtdswap_free_page_cnt(struct mtdswap_dev *d)
604a3215902SJarkko Lavinen {
605a3215902SJarkko Lavinen 	return TREE_COUNT(d, CLEAN) * d->pages_per_eblk +
606a3215902SJarkko Lavinen 		d->pages_per_eblk - d->curr_write_pos;
607a3215902SJarkko Lavinen }
608a3215902SJarkko Lavinen 
mtdswap_enough_free_pages(struct mtdswap_dev * d)609a3215902SJarkko Lavinen static unsigned int mtdswap_enough_free_pages(struct mtdswap_dev *d)
610a3215902SJarkko Lavinen {
611a3215902SJarkko Lavinen 	return mtdswap_free_page_cnt(d) > d->pages_per_eblk;
612a3215902SJarkko Lavinen }
613a3215902SJarkko Lavinen 
mtdswap_write_block(struct mtdswap_dev * d,char * buf,unsigned int page,unsigned int * bp,int gc_context)614a3215902SJarkko Lavinen static int mtdswap_write_block(struct mtdswap_dev *d, char *buf,
615a3215902SJarkko Lavinen 			unsigned int page, unsigned int *bp, int gc_context)
616a3215902SJarkko Lavinen {
617a3215902SJarkko Lavinen 	struct mtd_info *mtd = d->mtd;
618a3215902SJarkko Lavinen 	struct swap_eb *eb;
619a3215902SJarkko Lavinen 	size_t retlen;
620a3215902SJarkko Lavinen 	loff_t writepos;
621a3215902SJarkko Lavinen 	int ret;
622a3215902SJarkko Lavinen 
623a3215902SJarkko Lavinen retry:
624a3215902SJarkko Lavinen 	if (!gc_context)
625a3215902SJarkko Lavinen 		while (!mtdswap_enough_free_pages(d))
626a3215902SJarkko Lavinen 			if (mtdswap_gc(d, 0) > 0)
627a3215902SJarkko Lavinen 				return -ENOSPC;
628a3215902SJarkko Lavinen 
629a3215902SJarkko Lavinen 	ret = mtdswap_map_free_block(d, page, bp);
630a3215902SJarkko Lavinen 	eb = d->eb_data + (*bp / d->pages_per_eblk);
631a3215902SJarkko Lavinen 
632d57f4054SBrian Norris 	if (ret == -EIO || mtd_is_eccerr(ret)) {
633a3215902SJarkko Lavinen 		d->curr_write = NULL;
634a3215902SJarkko Lavinen 		eb->active_count--;
635a3215902SJarkko Lavinen 		d->revmap[*bp] = PAGE_UNDEF;
636a3215902SJarkko Lavinen 		goto retry;
637a3215902SJarkko Lavinen 	}
638a3215902SJarkko Lavinen 
639a3215902SJarkko Lavinen 	if (ret < 0)
640a3215902SJarkko Lavinen 		return ret;
641a3215902SJarkko Lavinen 
642a3215902SJarkko Lavinen 	writepos = (loff_t)*bp << PAGE_SHIFT;
643eda95cbfSArtem Bityutskiy 	ret =  mtd_write(mtd, writepos, PAGE_SIZE, &retlen, buf);
644d57f4054SBrian Norris 	if (ret == -EIO || mtd_is_eccerr(ret)) {
645a3215902SJarkko Lavinen 		d->curr_write_pos--;
646a3215902SJarkko Lavinen 		eb->active_count--;
647a3215902SJarkko Lavinen 		d->revmap[*bp] = PAGE_UNDEF;
648a3215902SJarkko Lavinen 		mtdswap_handle_write_error(d, eb);
649a3215902SJarkko Lavinen 		goto retry;
650a3215902SJarkko Lavinen 	}
651a3215902SJarkko Lavinen 
652a3215902SJarkko Lavinen 	if (ret < 0) {
65389d8d320SDavid Woodhouse 		dev_err(d->dev, "Write to MTD device failed: %d (%zd written)",
654a3215902SJarkko Lavinen 			ret, retlen);
655a3215902SJarkko Lavinen 		goto err;
656a3215902SJarkko Lavinen 	}
657a3215902SJarkko Lavinen 
658a3215902SJarkko Lavinen 	if (retlen != PAGE_SIZE) {
65989d8d320SDavid Woodhouse 		dev_err(d->dev, "Short write to MTD device: %zd written",
660a3215902SJarkko Lavinen 			retlen);
661a3215902SJarkko Lavinen 		ret = -EIO;
662a3215902SJarkko Lavinen 		goto err;
663a3215902SJarkko Lavinen 	}
664a3215902SJarkko Lavinen 
665a3215902SJarkko Lavinen 	return ret;
666a3215902SJarkko Lavinen 
667a3215902SJarkko Lavinen err:
668a3215902SJarkko Lavinen 	d->curr_write_pos--;
669a3215902SJarkko Lavinen 	eb->active_count--;
670a3215902SJarkko Lavinen 	d->revmap[*bp] = PAGE_UNDEF;
671a3215902SJarkko Lavinen 
672a3215902SJarkko Lavinen 	return ret;
673a3215902SJarkko Lavinen }
674a3215902SJarkko Lavinen 
mtdswap_move_block(struct mtdswap_dev * d,unsigned int oldblock,unsigned int * newblock)675a3215902SJarkko Lavinen static int mtdswap_move_block(struct mtdswap_dev *d, unsigned int oldblock,
676a3215902SJarkko Lavinen 		unsigned int *newblock)
677a3215902SJarkko Lavinen {
678a3215902SJarkko Lavinen 	struct mtd_info *mtd = d->mtd;
679a3215902SJarkko Lavinen 	struct swap_eb *eb, *oldeb;
680a3215902SJarkko Lavinen 	int ret;
681a3215902SJarkko Lavinen 	size_t retlen;
682a3215902SJarkko Lavinen 	unsigned int page, retries;
683a3215902SJarkko Lavinen 	loff_t readpos;
684a3215902SJarkko Lavinen 
685a3215902SJarkko Lavinen 	page = d->revmap[oldblock];
686a3215902SJarkko Lavinen 	readpos = (loff_t) oldblock << PAGE_SHIFT;
687a3215902SJarkko Lavinen 	retries = 0;
688a3215902SJarkko Lavinen 
689a3215902SJarkko Lavinen retry:
690329ad399SArtem Bityutskiy 	ret = mtd_read(mtd, readpos, PAGE_SIZE, &retlen, d->page_buf);
691a3215902SJarkko Lavinen 
692d57f4054SBrian Norris 	if (ret < 0 && !mtd_is_bitflip(ret)) {
693a3215902SJarkko Lavinen 		oldeb = d->eb_data + oldblock / d->pages_per_eblk;
694a3215902SJarkko Lavinen 		oldeb->flags |= EBLOCK_READERR;
695a3215902SJarkko Lavinen 
696a3215902SJarkko Lavinen 		dev_err(d->dev, "Read Error: %d (block %u)\n", ret,
697a3215902SJarkko Lavinen 			oldblock);
698a3215902SJarkko Lavinen 		retries++;
699a3215902SJarkko Lavinen 		if (retries < MTDSWAP_IO_RETRIES)
700a3215902SJarkko Lavinen 			goto retry;
701a3215902SJarkko Lavinen 
702a3215902SJarkko Lavinen 		goto read_error;
703a3215902SJarkko Lavinen 	}
704a3215902SJarkko Lavinen 
705a3215902SJarkko Lavinen 	if (retlen != PAGE_SIZE) {
70689d8d320SDavid Woodhouse 		dev_err(d->dev, "Short read: %zd (block %u)\n", retlen,
707a3215902SJarkko Lavinen 		       oldblock);
708a3215902SJarkko Lavinen 		ret = -EIO;
709a3215902SJarkko Lavinen 		goto read_error;
710a3215902SJarkko Lavinen 	}
711a3215902SJarkko Lavinen 
712a3215902SJarkko Lavinen 	ret = mtdswap_write_block(d, d->page_buf, page, newblock, 1);
713a3215902SJarkko Lavinen 	if (ret < 0) {
714a3215902SJarkko Lavinen 		d->page_data[page] = BLOCK_ERROR;
715a3215902SJarkko Lavinen 		dev_err(d->dev, "Write error: %d\n", ret);
716a3215902SJarkko Lavinen 		return ret;
717a3215902SJarkko Lavinen 	}
718a3215902SJarkko Lavinen 
719a3215902SJarkko Lavinen 	d->page_data[page] = *newblock;
720a3215902SJarkko Lavinen 	d->revmap[oldblock] = PAGE_UNDEF;
721a3215902SJarkko Lavinen 	eb = d->eb_data + oldblock / d->pages_per_eblk;
722a3215902SJarkko Lavinen 	eb->active_count--;
723a3215902SJarkko Lavinen 
724a3215902SJarkko Lavinen 	return 0;
725a3215902SJarkko Lavinen 
726a3215902SJarkko Lavinen read_error:
727a3215902SJarkko Lavinen 	d->page_data[page] = BLOCK_ERROR;
728a3215902SJarkko Lavinen 	d->revmap[oldblock] = PAGE_UNDEF;
729a3215902SJarkko Lavinen 	return ret;
730a3215902SJarkko Lavinen }
731a3215902SJarkko Lavinen 
mtdswap_gc_eblock(struct mtdswap_dev * d,struct swap_eb * eb)732a3215902SJarkko Lavinen static int mtdswap_gc_eblock(struct mtdswap_dev *d, struct swap_eb *eb)
733a3215902SJarkko Lavinen {
734a3215902SJarkko Lavinen 	unsigned int i, block, eblk_base, newblock;
735a3215902SJarkko Lavinen 	int ret, errcode;
736a3215902SJarkko Lavinen 
737a3215902SJarkko Lavinen 	errcode = 0;
738a3215902SJarkko Lavinen 	eblk_base = (eb - d->eb_data) * d->pages_per_eblk;
739a3215902SJarkko Lavinen 
740a3215902SJarkko Lavinen 	for (i = 0; i < d->pages_per_eblk; i++) {
741a3215902SJarkko Lavinen 		if (d->spare_eblks < MIN_SPARE_EBLOCKS)
742a3215902SJarkko Lavinen 			return -ENOSPC;
743a3215902SJarkko Lavinen 
744a3215902SJarkko Lavinen 		block = eblk_base + i;
745a3215902SJarkko Lavinen 		if (d->revmap[block] == PAGE_UNDEF)
746a3215902SJarkko Lavinen 			continue;
747a3215902SJarkko Lavinen 
748a3215902SJarkko Lavinen 		ret = mtdswap_move_block(d, block, &newblock);
749a3215902SJarkko Lavinen 		if (ret < 0 && !errcode)
750a3215902SJarkko Lavinen 			errcode = ret;
751a3215902SJarkko Lavinen 	}
752a3215902SJarkko Lavinen 
753a3215902SJarkko Lavinen 	return errcode;
754a3215902SJarkko Lavinen }
755a3215902SJarkko Lavinen 
__mtdswap_choose_gc_tree(struct mtdswap_dev * d)756a3215902SJarkko Lavinen static int __mtdswap_choose_gc_tree(struct mtdswap_dev *d)
757a3215902SJarkko Lavinen {
758a3215902SJarkko Lavinen 	int idx, stopat;
759a3215902SJarkko Lavinen 
760a5929b64SArvind Yadav 	if (TREE_COUNT(d, CLEAN) < LOW_FRAG_GC_THRESHOLD)
761a3215902SJarkko Lavinen 		stopat = MTDSWAP_LOWFRAG;
762a3215902SJarkko Lavinen 	else
763a3215902SJarkko Lavinen 		stopat = MTDSWAP_HIFRAG;
764a3215902SJarkko Lavinen 
765a3215902SJarkko Lavinen 	for (idx = MTDSWAP_BITFLIP; idx >= stopat; idx--)
766a3215902SJarkko Lavinen 		if (d->trees[idx].root.rb_node != NULL)
767a3215902SJarkko Lavinen 			return idx;
768a3215902SJarkko Lavinen 
769a3215902SJarkko Lavinen 	return -1;
770a3215902SJarkko Lavinen }
771a3215902SJarkko Lavinen 
mtdswap_wlfreq(unsigned int maxdiff)772a3215902SJarkko Lavinen static int mtdswap_wlfreq(unsigned int maxdiff)
773a3215902SJarkko Lavinen {
774a3215902SJarkko Lavinen 	unsigned int h, x, y, dist, base;
775a3215902SJarkko Lavinen 
776a3215902SJarkko Lavinen 	/*
777a3215902SJarkko Lavinen 	 * Calculate linear ramp down from f1 to f2 when maxdiff goes from
778a3215902SJarkko Lavinen 	 * MAX_ERASE_DIFF to MAX_ERASE_DIFF + COLLECT_NONDIRTY_BASE.  Similar
779a3215902SJarkko Lavinen 	 * to triangle with height f1 - f1 and width COLLECT_NONDIRTY_BASE.
780a3215902SJarkko Lavinen 	 */
781a3215902SJarkko Lavinen 
782a3215902SJarkko Lavinen 	dist = maxdiff - MAX_ERASE_DIFF;
783a3215902SJarkko Lavinen 	if (dist > COLLECT_NONDIRTY_BASE)
784a3215902SJarkko Lavinen 		dist = COLLECT_NONDIRTY_BASE;
785a3215902SJarkko Lavinen 
786a3215902SJarkko Lavinen 	/*
787a3215902SJarkko Lavinen 	 * Modelling the slop as right angular triangle with base
788a3215902SJarkko Lavinen 	 * COLLECT_NONDIRTY_BASE and height freq1 - freq2. The ratio y/x is
789a3215902SJarkko Lavinen 	 * equal to the ratio h/base.
790a3215902SJarkko Lavinen 	 */
791a3215902SJarkko Lavinen 	h = COLLECT_NONDIRTY_FREQ1 - COLLECT_NONDIRTY_FREQ2;
792a3215902SJarkko Lavinen 	base = COLLECT_NONDIRTY_BASE;
793a3215902SJarkko Lavinen 
794a3215902SJarkko Lavinen 	x = dist - base;
795a3215902SJarkko Lavinen 	y = (x * h + base / 2) / base;
796a3215902SJarkko Lavinen 
797a3215902SJarkko Lavinen 	return COLLECT_NONDIRTY_FREQ2 + y;
798a3215902SJarkko Lavinen }
799a3215902SJarkko Lavinen 
mtdswap_choose_wl_tree(struct mtdswap_dev * d)800a3215902SJarkko Lavinen static int mtdswap_choose_wl_tree(struct mtdswap_dev *d)
801a3215902SJarkko Lavinen {
802a3215902SJarkko Lavinen 	static unsigned int pick_cnt;
80368b1a1e7SArtem Bityutskiy 	unsigned int i, idx = -1, wear, max;
804a3215902SJarkko Lavinen 	struct rb_root *root;
805a3215902SJarkko Lavinen 
806a3215902SJarkko Lavinen 	max = 0;
807a3215902SJarkko Lavinen 	for (i = 0; i <= MTDSWAP_DIRTY; i++) {
808a3215902SJarkko Lavinen 		root = &d->trees[i].root;
809a3215902SJarkko Lavinen 		if (root->rb_node == NULL)
810a3215902SJarkko Lavinen 			continue;
811a3215902SJarkko Lavinen 
812a3215902SJarkko Lavinen 		wear = d->max_erase_count - MTDSWAP_ECNT_MIN(root);
813a3215902SJarkko Lavinen 		if (wear > max) {
814a3215902SJarkko Lavinen 			max = wear;
815a3215902SJarkko Lavinen 			idx = i;
816a3215902SJarkko Lavinen 		}
817a3215902SJarkko Lavinen 	}
818a3215902SJarkko Lavinen 
819a3215902SJarkko Lavinen 	if (max > MAX_ERASE_DIFF && pick_cnt >= mtdswap_wlfreq(max) - 1) {
820a3215902SJarkko Lavinen 		pick_cnt = 0;
821a3215902SJarkko Lavinen 		return idx;
822a3215902SJarkko Lavinen 	}
823a3215902SJarkko Lavinen 
824a3215902SJarkko Lavinen 	pick_cnt++;
825a3215902SJarkko Lavinen 	return -1;
826a3215902SJarkko Lavinen }
827a3215902SJarkko Lavinen 
mtdswap_choose_gc_tree(struct mtdswap_dev * d,unsigned int background)828a3215902SJarkko Lavinen static int mtdswap_choose_gc_tree(struct mtdswap_dev *d,
829a3215902SJarkko Lavinen 				unsigned int background)
830a3215902SJarkko Lavinen {
831a3215902SJarkko Lavinen 	int idx;
832a3215902SJarkko Lavinen 
833a3215902SJarkko Lavinen 	if (TREE_NONEMPTY(d, FAILING) &&
834a3215902SJarkko Lavinen 		(background || (TREE_EMPTY(d, CLEAN) && TREE_EMPTY(d, DIRTY))))
835a3215902SJarkko Lavinen 		return MTDSWAP_FAILING;
836a3215902SJarkko Lavinen 
837a3215902SJarkko Lavinen 	idx = mtdswap_choose_wl_tree(d);
838a3215902SJarkko Lavinen 	if (idx >= MTDSWAP_CLEAN)
839a3215902SJarkko Lavinen 		return idx;
840a3215902SJarkko Lavinen 
841a3215902SJarkko Lavinen 	return __mtdswap_choose_gc_tree(d);
842a3215902SJarkko Lavinen }
843a3215902SJarkko Lavinen 
mtdswap_pick_gc_eblk(struct mtdswap_dev * d,unsigned int background)844a3215902SJarkko Lavinen static struct swap_eb *mtdswap_pick_gc_eblk(struct mtdswap_dev *d,
845a3215902SJarkko Lavinen 					unsigned int background)
846a3215902SJarkko Lavinen {
847a3215902SJarkko Lavinen 	struct rb_root *rp = NULL;
848a3215902SJarkko Lavinen 	struct swap_eb *eb = NULL;
849a3215902SJarkko Lavinen 	int idx;
850a3215902SJarkko Lavinen 
851a3215902SJarkko Lavinen 	if (background && TREE_COUNT(d, CLEAN) > CLEAN_BLOCK_THRESHOLD &&
852a3215902SJarkko Lavinen 		TREE_EMPTY(d, DIRTY) && TREE_EMPTY(d, FAILING))
853a3215902SJarkko Lavinen 		return NULL;
854a3215902SJarkko Lavinen 
855a3215902SJarkko Lavinen 	idx = mtdswap_choose_gc_tree(d, background);
856a3215902SJarkko Lavinen 	if (idx < 0)
857a3215902SJarkko Lavinen 		return NULL;
858a3215902SJarkko Lavinen 
859a3215902SJarkko Lavinen 	rp = &d->trees[idx].root;
860a3215902SJarkko Lavinen 	eb = rb_entry(rb_first(rp), struct swap_eb, rb);
861a3215902SJarkko Lavinen 
862a3215902SJarkko Lavinen 	rb_erase(&eb->rb, rp);
863a3215902SJarkko Lavinen 	eb->root = NULL;
864a3215902SJarkko Lavinen 	d->trees[idx].count--;
865a3215902SJarkko Lavinen 	return eb;
866a3215902SJarkko Lavinen }
867a3215902SJarkko Lavinen 
mtdswap_test_patt(unsigned int i)868a3215902SJarkko Lavinen static unsigned int mtdswap_test_patt(unsigned int i)
869a3215902SJarkko Lavinen {
870a3215902SJarkko Lavinen 	return i % 2 ? 0x55555555 : 0xAAAAAAAA;
871a3215902SJarkko Lavinen }
872a3215902SJarkko Lavinen 
mtdswap_eblk_passes(struct mtdswap_dev * d,struct swap_eb * eb)873a3215902SJarkko Lavinen static unsigned int mtdswap_eblk_passes(struct mtdswap_dev *d,
874a3215902SJarkko Lavinen 					struct swap_eb *eb)
875a3215902SJarkko Lavinen {
876a3215902SJarkko Lavinen 	struct mtd_info *mtd = d->mtd;
877a3215902SJarkko Lavinen 	unsigned int test, i, j, patt, mtd_pages;
878a3215902SJarkko Lavinen 	loff_t base, pos;
879a3215902SJarkko Lavinen 	unsigned int *p1 = (unsigned int *)d->page_buf;
880a3215902SJarkko Lavinen 	unsigned char *p2 = (unsigned char *)d->oob_buf;
881*745df179SMichał Kępień 	struct mtd_oob_ops ops = { };
882a3215902SJarkko Lavinen 	int ret;
883a3215902SJarkko Lavinen 
8840612b9ddSBrian Norris 	ops.mode = MTD_OPS_AUTO_OOB;
885a3215902SJarkko Lavinen 	ops.len = mtd->writesize;
886f5b8aa78SBoris BREZILLON 	ops.ooblen = mtd->oobavail;
887a3215902SJarkko Lavinen 	ops.ooboffs = 0;
888a3215902SJarkko Lavinen 	ops.datbuf = d->page_buf;
889a3215902SJarkko Lavinen 	ops.oobbuf = d->oob_buf;
890a3215902SJarkko Lavinen 	base = mtdswap_eb_offset(d, eb);
891a3215902SJarkko Lavinen 	mtd_pages = d->pages_per_eblk * PAGE_SIZE / mtd->writesize;
892a3215902SJarkko Lavinen 
893a3215902SJarkko Lavinen 	for (test = 0; test < 2; test++) {
894a3215902SJarkko Lavinen 		pos = base;
895a3215902SJarkko Lavinen 		for (i = 0; i < mtd_pages; i++) {
896a3215902SJarkko Lavinen 			patt = mtdswap_test_patt(test + i);
897a3215902SJarkko Lavinen 			memset(d->page_buf, patt, mtd->writesize);
898f5b8aa78SBoris BREZILLON 			memset(d->oob_buf, patt, mtd->oobavail);
899a2cc5ba0SArtem Bityutskiy 			ret = mtd_write_oob(mtd, pos, &ops);
900a3215902SJarkko Lavinen 			if (ret)
901a3215902SJarkko Lavinen 				goto error;
902a3215902SJarkko Lavinen 
903a3215902SJarkko Lavinen 			pos += mtd->writesize;
904a3215902SJarkko Lavinen 		}
905a3215902SJarkko Lavinen 
906a3215902SJarkko Lavinen 		pos = base;
907a3215902SJarkko Lavinen 		for (i = 0; i < mtd_pages; i++) {
908fd2819bbSArtem Bityutskiy 			ret = mtd_read_oob(mtd, pos, &ops);
909a3215902SJarkko Lavinen 			if (ret)
910a3215902SJarkko Lavinen 				goto error;
911a3215902SJarkko Lavinen 
912a3215902SJarkko Lavinen 			patt = mtdswap_test_patt(test + i);
913a3215902SJarkko Lavinen 			for (j = 0; j < mtd->writesize/sizeof(int); j++)
914a3215902SJarkko Lavinen 				if (p1[j] != patt)
915a3215902SJarkko Lavinen 					goto error;
916a3215902SJarkko Lavinen 
917f5b8aa78SBoris BREZILLON 			for (j = 0; j < mtd->oobavail; j++)
918a3215902SJarkko Lavinen 				if (p2[j] != (unsigned char)patt)
919a3215902SJarkko Lavinen 					goto error;
920a3215902SJarkko Lavinen 
921a3215902SJarkko Lavinen 			pos += mtd->writesize;
922a3215902SJarkko Lavinen 		}
923a3215902SJarkko Lavinen 
924a3215902SJarkko Lavinen 		ret = mtdswap_erase_block(d, eb);
925a3215902SJarkko Lavinen 		if (ret)
926a3215902SJarkko Lavinen 			goto error;
927a3215902SJarkko Lavinen 	}
928a3215902SJarkko Lavinen 
929a3215902SJarkko Lavinen 	eb->flags &= ~EBLOCK_READERR;
930a3215902SJarkko Lavinen 	return 1;
931a3215902SJarkko Lavinen 
932a3215902SJarkko Lavinen error:
933a3215902SJarkko Lavinen 	mtdswap_handle_badblock(d, eb);
934a3215902SJarkko Lavinen 	return 0;
935a3215902SJarkko Lavinen }
936a3215902SJarkko Lavinen 
mtdswap_gc(struct mtdswap_dev * d,unsigned int background)937a3215902SJarkko Lavinen static int mtdswap_gc(struct mtdswap_dev *d, unsigned int background)
938a3215902SJarkko Lavinen {
939a3215902SJarkko Lavinen 	struct swap_eb *eb;
940a3215902SJarkko Lavinen 	int ret;
941a3215902SJarkko Lavinen 
942a3215902SJarkko Lavinen 	if (d->spare_eblks < MIN_SPARE_EBLOCKS)
943a3215902SJarkko Lavinen 		return 1;
944a3215902SJarkko Lavinen 
945a3215902SJarkko Lavinen 	eb = mtdswap_pick_gc_eblk(d, background);
946a3215902SJarkko Lavinen 	if (!eb)
947a3215902SJarkko Lavinen 		return 1;
948a3215902SJarkko Lavinen 
949a3215902SJarkko Lavinen 	ret = mtdswap_gc_eblock(d, eb);
950a3215902SJarkko Lavinen 	if (ret == -ENOSPC)
951a3215902SJarkko Lavinen 		return 1;
952a3215902SJarkko Lavinen 
953a3215902SJarkko Lavinen 	if (eb->flags & EBLOCK_FAILED) {
954a3215902SJarkko Lavinen 		mtdswap_handle_badblock(d, eb);
955a3215902SJarkko Lavinen 		return 0;
956a3215902SJarkko Lavinen 	}
957a3215902SJarkko Lavinen 
958a3215902SJarkko Lavinen 	eb->flags &= ~EBLOCK_BITFLIP;
959a3215902SJarkko Lavinen 	ret = mtdswap_erase_block(d, eb);
960a3215902SJarkko Lavinen 	if ((eb->flags & EBLOCK_READERR) &&
961a3215902SJarkko Lavinen 		(ret || !mtdswap_eblk_passes(d, eb)))
962a3215902SJarkko Lavinen 		return 0;
963a3215902SJarkko Lavinen 
964a3215902SJarkko Lavinen 	if (ret == 0)
965a3215902SJarkko Lavinen 		ret = mtdswap_write_marker(d, eb, MTDSWAP_TYPE_CLEAN);
966a3215902SJarkko Lavinen 
967a3215902SJarkko Lavinen 	if (ret == 0)
968a3215902SJarkko Lavinen 		mtdswap_rb_add(d, eb, MTDSWAP_CLEAN);
969d57f4054SBrian Norris 	else if (ret != -EIO && !mtd_is_eccerr(ret))
970a3215902SJarkko Lavinen 		mtdswap_rb_add(d, eb, MTDSWAP_DIRTY);
971a3215902SJarkko Lavinen 
972a3215902SJarkko Lavinen 	return 0;
973a3215902SJarkko Lavinen }
974a3215902SJarkko Lavinen 
mtdswap_background(struct mtd_blktrans_dev * dev)975a3215902SJarkko Lavinen static void mtdswap_background(struct mtd_blktrans_dev *dev)
976a3215902SJarkko Lavinen {
977a3215902SJarkko Lavinen 	struct mtdswap_dev *d = MTDSWAP_MBD_TO_MTDSWAP(dev);
978a3215902SJarkko Lavinen 	int ret;
979a3215902SJarkko Lavinen 
980a3215902SJarkko Lavinen 	while (1) {
981a3215902SJarkko Lavinen 		ret = mtdswap_gc(d, 1);
982a3215902SJarkko Lavinen 		if (ret || mtd_blktrans_cease_background(dev))
983a3215902SJarkko Lavinen 			return;
984a3215902SJarkko Lavinen 	}
985a3215902SJarkko Lavinen }
986a3215902SJarkko Lavinen 
mtdswap_cleanup(struct mtdswap_dev * d)987a3215902SJarkko Lavinen static void mtdswap_cleanup(struct mtdswap_dev *d)
988a3215902SJarkko Lavinen {
989a3215902SJarkko Lavinen 	vfree(d->eb_data);
990a3215902SJarkko Lavinen 	vfree(d->revmap);
991a3215902SJarkko Lavinen 	vfree(d->page_data);
992a3215902SJarkko Lavinen 	kfree(d->oob_buf);
993a3215902SJarkko Lavinen 	kfree(d->page_buf);
994a3215902SJarkko Lavinen }
995a3215902SJarkko Lavinen 
mtdswap_flush(struct mtd_blktrans_dev * dev)996a3215902SJarkko Lavinen static int mtdswap_flush(struct mtd_blktrans_dev *dev)
997a3215902SJarkko Lavinen {
998a3215902SJarkko Lavinen 	struct mtdswap_dev *d = MTDSWAP_MBD_TO_MTDSWAP(dev);
999a3215902SJarkko Lavinen 
100085f2f2a8SArtem Bityutskiy 	mtd_sync(d->mtd);
1001a3215902SJarkko Lavinen 	return 0;
1002a3215902SJarkko Lavinen }
1003a3215902SJarkko Lavinen 
mtdswap_badblocks(struct mtd_info * mtd,uint64_t size)1004a3215902SJarkko Lavinen static unsigned int mtdswap_badblocks(struct mtd_info *mtd, uint64_t size)
1005a3215902SJarkko Lavinen {
1006a3215902SJarkko Lavinen 	loff_t offset;
1007a3215902SJarkko Lavinen 	unsigned int badcnt;
1008a3215902SJarkko Lavinen 
1009a3215902SJarkko Lavinen 	badcnt = 0;
1010a3215902SJarkko Lavinen 
10118f461a73SArtem Bityutskiy 	if (mtd_can_have_bb(mtd))
1012a3215902SJarkko Lavinen 		for (offset = 0; offset < size; offset += mtd->erasesize)
10137086c19dSArtem Bityutskiy 			if (mtd_block_isbad(mtd, offset))
1014a3215902SJarkko Lavinen 				badcnt++;
1015a3215902SJarkko Lavinen 
1016a3215902SJarkko Lavinen 	return badcnt;
1017a3215902SJarkko Lavinen }
1018a3215902SJarkko Lavinen 
mtdswap_writesect(struct mtd_blktrans_dev * dev,unsigned long page,char * buf)1019a3215902SJarkko Lavinen static int mtdswap_writesect(struct mtd_blktrans_dev *dev,
1020a3215902SJarkko Lavinen 			unsigned long page, char *buf)
1021a3215902SJarkko Lavinen {
1022a3215902SJarkko Lavinen 	struct mtdswap_dev *d = MTDSWAP_MBD_TO_MTDSWAP(dev);
1023a3215902SJarkko Lavinen 	unsigned int newblock, mapped;
1024a3215902SJarkko Lavinen 	struct swap_eb *eb;
1025a3215902SJarkko Lavinen 	int ret;
1026a3215902SJarkko Lavinen 
1027a3215902SJarkko Lavinen 	d->sect_write_count++;
1028a3215902SJarkko Lavinen 
1029a3215902SJarkko Lavinen 	if (d->spare_eblks < MIN_SPARE_EBLOCKS)
1030a3215902SJarkko Lavinen 		return -ENOSPC;
1031a3215902SJarkko Lavinen 
1032a3215902SJarkko Lavinen 	if (header) {
1033a3215902SJarkko Lavinen 		/* Ignore writes to the header page */
1034a3215902SJarkko Lavinen 		if (unlikely(page == 0))
1035a3215902SJarkko Lavinen 			return 0;
1036a3215902SJarkko Lavinen 
1037a3215902SJarkko Lavinen 		page--;
1038a3215902SJarkko Lavinen 	}
1039a3215902SJarkko Lavinen 
1040a3215902SJarkko Lavinen 	mapped = d->page_data[page];
1041a3215902SJarkko Lavinen 	if (mapped <= BLOCK_MAX) {
1042a3215902SJarkko Lavinen 		eb = d->eb_data + (mapped / d->pages_per_eblk);
1043a3215902SJarkko Lavinen 		eb->active_count--;
1044a3215902SJarkko Lavinen 		mtdswap_store_eb(d, eb);
1045a3215902SJarkko Lavinen 		d->page_data[page] = BLOCK_UNDEF;
1046a3215902SJarkko Lavinen 		d->revmap[mapped] = PAGE_UNDEF;
1047a3215902SJarkko Lavinen 	}
1048a3215902SJarkko Lavinen 
1049a3215902SJarkko Lavinen 	ret = mtdswap_write_block(d, buf, page, &newblock, 0);
1050a3215902SJarkko Lavinen 	d->mtd_write_count++;
1051a3215902SJarkko Lavinen 
1052a3215902SJarkko Lavinen 	if (ret < 0)
1053a3215902SJarkko Lavinen 		return ret;
1054a3215902SJarkko Lavinen 
1055a3215902SJarkko Lavinen 	d->page_data[page] = newblock;
1056a3215902SJarkko Lavinen 
1057a3215902SJarkko Lavinen 	return 0;
1058a3215902SJarkko Lavinen }
1059a3215902SJarkko Lavinen 
1060a3215902SJarkko Lavinen /* Provide a dummy swap header for the kernel */
mtdswap_auto_header(struct mtdswap_dev * d,char * buf)1061a3215902SJarkko Lavinen static int mtdswap_auto_header(struct mtdswap_dev *d, char *buf)
1062a3215902SJarkko Lavinen {
1063a3215902SJarkko Lavinen 	union swap_header *hd = (union swap_header *)(buf);
1064a3215902SJarkko Lavinen 
1065a3215902SJarkko Lavinen 	memset(buf, 0, PAGE_SIZE - 10);
1066a3215902SJarkko Lavinen 
1067a3215902SJarkko Lavinen 	hd->info.version = 1;
1068a3215902SJarkko Lavinen 	hd->info.last_page = d->mbd_dev->size - 1;
1069a3215902SJarkko Lavinen 	hd->info.nr_badpages = 0;
1070a3215902SJarkko Lavinen 
1071a3215902SJarkko Lavinen 	memcpy(buf + PAGE_SIZE - 10, "SWAPSPACE2", 10);
1072a3215902SJarkko Lavinen 
1073a3215902SJarkko Lavinen 	return 0;
1074a3215902SJarkko Lavinen }
1075a3215902SJarkko Lavinen 
mtdswap_readsect(struct mtd_blktrans_dev * dev,unsigned long page,char * buf)1076a3215902SJarkko Lavinen static int mtdswap_readsect(struct mtd_blktrans_dev *dev,
1077a3215902SJarkko Lavinen 			unsigned long page, char *buf)
1078a3215902SJarkko Lavinen {
1079a3215902SJarkko Lavinen 	struct mtdswap_dev *d = MTDSWAP_MBD_TO_MTDSWAP(dev);
1080a3215902SJarkko Lavinen 	struct mtd_info *mtd = d->mtd;
1081a3215902SJarkko Lavinen 	unsigned int realblock, retries;
1082a3215902SJarkko Lavinen 	loff_t readpos;
1083a3215902SJarkko Lavinen 	struct swap_eb *eb;
1084a3215902SJarkko Lavinen 	size_t retlen;
1085a3215902SJarkko Lavinen 	int ret;
1086a3215902SJarkko Lavinen 
1087a3215902SJarkko Lavinen 	d->sect_read_count++;
1088a3215902SJarkko Lavinen 
1089a3215902SJarkko Lavinen 	if (header) {
1090a3215902SJarkko Lavinen 		if (unlikely(page == 0))
1091a3215902SJarkko Lavinen 			return mtdswap_auto_header(d, buf);
1092a3215902SJarkko Lavinen 
1093a3215902SJarkko Lavinen 		page--;
1094a3215902SJarkko Lavinen 	}
1095a3215902SJarkko Lavinen 
1096a3215902SJarkko Lavinen 	realblock = d->page_data[page];
1097a3215902SJarkko Lavinen 	if (realblock > BLOCK_MAX) {
1098a3215902SJarkko Lavinen 		memset(buf, 0x0, PAGE_SIZE);
1099a3215902SJarkko Lavinen 		if (realblock == BLOCK_UNDEF)
1100a3215902SJarkko Lavinen 			return 0;
1101a3215902SJarkko Lavinen 		else
1102a3215902SJarkko Lavinen 			return -EIO;
1103a3215902SJarkko Lavinen 	}
1104a3215902SJarkko Lavinen 
1105a3215902SJarkko Lavinen 	eb = d->eb_data + (realblock / d->pages_per_eblk);
1106a3215902SJarkko Lavinen 	BUG_ON(d->revmap[realblock] == PAGE_UNDEF);
1107a3215902SJarkko Lavinen 
1108a3215902SJarkko Lavinen 	readpos = (loff_t)realblock << PAGE_SHIFT;
1109a3215902SJarkko Lavinen 	retries = 0;
1110a3215902SJarkko Lavinen 
1111a3215902SJarkko Lavinen retry:
1112329ad399SArtem Bityutskiy 	ret = mtd_read(mtd, readpos, PAGE_SIZE, &retlen, buf);
1113a3215902SJarkko Lavinen 
1114a3215902SJarkko Lavinen 	d->mtd_read_count++;
1115d57f4054SBrian Norris 	if (mtd_is_bitflip(ret)) {
1116a3215902SJarkko Lavinen 		eb->flags |= EBLOCK_BITFLIP;
1117a3215902SJarkko Lavinen 		mtdswap_rb_add(d, eb, MTDSWAP_BITFLIP);
1118a3215902SJarkko Lavinen 		ret = 0;
1119a3215902SJarkko Lavinen 	}
1120a3215902SJarkko Lavinen 
1121a3215902SJarkko Lavinen 	if (ret < 0) {
1122a3215902SJarkko Lavinen 		dev_err(d->dev, "Read error %d\n", ret);
1123a3215902SJarkko Lavinen 		eb->flags |= EBLOCK_READERR;
1124a3215902SJarkko Lavinen 		mtdswap_rb_add(d, eb, MTDSWAP_FAILING);
1125a3215902SJarkko Lavinen 		retries++;
1126a3215902SJarkko Lavinen 		if (retries < MTDSWAP_IO_RETRIES)
1127a3215902SJarkko Lavinen 			goto retry;
1128a3215902SJarkko Lavinen 
1129a3215902SJarkko Lavinen 		return ret;
1130a3215902SJarkko Lavinen 	}
1131a3215902SJarkko Lavinen 
1132a3215902SJarkko Lavinen 	if (retlen != PAGE_SIZE) {
113389d8d320SDavid Woodhouse 		dev_err(d->dev, "Short read %zd\n", retlen);
1134a3215902SJarkko Lavinen 		return -EIO;
1135a3215902SJarkko Lavinen 	}
1136a3215902SJarkko Lavinen 
1137a3215902SJarkko Lavinen 	return 0;
1138a3215902SJarkko Lavinen }
1139a3215902SJarkko Lavinen 
mtdswap_discard(struct mtd_blktrans_dev * dev,unsigned long first,unsigned nr_pages)1140a3215902SJarkko Lavinen static int mtdswap_discard(struct mtd_blktrans_dev *dev, unsigned long first,
1141a3215902SJarkko Lavinen 			unsigned nr_pages)
1142a3215902SJarkko Lavinen {
1143a3215902SJarkko Lavinen 	struct mtdswap_dev *d = MTDSWAP_MBD_TO_MTDSWAP(dev);
1144a3215902SJarkko Lavinen 	unsigned long page;
1145a3215902SJarkko Lavinen 	struct swap_eb *eb;
1146a3215902SJarkko Lavinen 	unsigned int mapped;
1147a3215902SJarkko Lavinen 
1148a3215902SJarkko Lavinen 	d->discard_count++;
1149a3215902SJarkko Lavinen 
1150a3215902SJarkko Lavinen 	for (page = first; page < first + nr_pages; page++) {
1151a3215902SJarkko Lavinen 		mapped = d->page_data[page];
1152a3215902SJarkko Lavinen 		if (mapped <= BLOCK_MAX) {
1153a3215902SJarkko Lavinen 			eb = d->eb_data + (mapped / d->pages_per_eblk);
1154a3215902SJarkko Lavinen 			eb->active_count--;
1155a3215902SJarkko Lavinen 			mtdswap_store_eb(d, eb);
1156a3215902SJarkko Lavinen 			d->page_data[page] = BLOCK_UNDEF;
1157a3215902SJarkko Lavinen 			d->revmap[mapped] = PAGE_UNDEF;
1158a3215902SJarkko Lavinen 			d->discard_page_count++;
1159a3215902SJarkko Lavinen 		} else if (mapped == BLOCK_ERROR) {
1160a3215902SJarkko Lavinen 			d->page_data[page] = BLOCK_UNDEF;
1161a3215902SJarkko Lavinen 			d->discard_page_count++;
1162a3215902SJarkko Lavinen 		}
1163a3215902SJarkko Lavinen 	}
1164a3215902SJarkko Lavinen 
1165a3215902SJarkko Lavinen 	return 0;
1166a3215902SJarkko Lavinen }
1167a3215902SJarkko Lavinen 
mtdswap_show(struct seq_file * s,void * data)1168a3215902SJarkko Lavinen static int mtdswap_show(struct seq_file *s, void *data)
1169a3215902SJarkko Lavinen {
1170a3215902SJarkko Lavinen 	struct mtdswap_dev *d = (struct mtdswap_dev *) s->private;
1171a3215902SJarkko Lavinen 	unsigned long sum;
1172a3215902SJarkko Lavinen 	unsigned int count[MTDSWAP_TREE_CNT];
1173a3215902SJarkko Lavinen 	unsigned int min[MTDSWAP_TREE_CNT];
1174a3215902SJarkko Lavinen 	unsigned int max[MTDSWAP_TREE_CNT];
1175a3215902SJarkko Lavinen 	unsigned int i, cw = 0, cwp = 0, cwecount = 0, bb_cnt, mapped, pages;
1176a3215902SJarkko Lavinen 	uint64_t use_size;
1177bf657105SColin Ian King 	static const char * const name[] = {
1178bf657105SColin Ian King 		"clean", "used", "low", "high", "dirty", "bitflip", "failing"
1179bf657105SColin Ian King 	};
1180a3215902SJarkko Lavinen 
1181a3215902SJarkko Lavinen 	mutex_lock(&d->mbd_dev->lock);
1182a3215902SJarkko Lavinen 
1183a3215902SJarkko Lavinen 	for (i = 0; i < MTDSWAP_TREE_CNT; i++) {
1184a3215902SJarkko Lavinen 		struct rb_root *root = &d->trees[i].root;
1185a3215902SJarkko Lavinen 
1186a3215902SJarkko Lavinen 		if (root->rb_node) {
1187a3215902SJarkko Lavinen 			count[i] = d->trees[i].count;
1188e4a8aad8SGeliang Tang 			min[i] = MTDSWAP_ECNT_MIN(root);
1189e4a8aad8SGeliang Tang 			max[i] = MTDSWAP_ECNT_MAX(root);
1190a3215902SJarkko Lavinen 		} else
1191a3215902SJarkko Lavinen 			count[i] = 0;
1192a3215902SJarkko Lavinen 	}
1193a3215902SJarkko Lavinen 
1194a3215902SJarkko Lavinen 	if (d->curr_write) {
1195a3215902SJarkko Lavinen 		cw = 1;
1196a3215902SJarkko Lavinen 		cwp = d->curr_write_pos;
1197a3215902SJarkko Lavinen 		cwecount = d->curr_write->erase_count;
1198a3215902SJarkko Lavinen 	}
1199a3215902SJarkko Lavinen 
1200a3215902SJarkko Lavinen 	sum = 0;
1201a3215902SJarkko Lavinen 	for (i = 0; i < d->eblks; i++)
1202a3215902SJarkko Lavinen 		sum += d->eb_data[i].erase_count;
1203a3215902SJarkko Lavinen 
1204a3215902SJarkko Lavinen 	use_size = (uint64_t)d->eblks * d->mtd->erasesize;
1205a3215902SJarkko Lavinen 	bb_cnt = mtdswap_badblocks(d->mtd, use_size);
1206a3215902SJarkko Lavinen 
1207a3215902SJarkko Lavinen 	mapped = 0;
1208a3215902SJarkko Lavinen 	pages = d->mbd_dev->size;
1209a3215902SJarkko Lavinen 	for (i = 0; i < pages; i++)
1210a3215902SJarkko Lavinen 		if (d->page_data[i] != BLOCK_UNDEF)
1211a3215902SJarkko Lavinen 			mapped++;
1212a3215902SJarkko Lavinen 
1213a3215902SJarkko Lavinen 	mutex_unlock(&d->mbd_dev->lock);
1214a3215902SJarkko Lavinen 
1215a3215902SJarkko Lavinen 	for (i = 0; i < MTDSWAP_TREE_CNT; i++) {
1216a3215902SJarkko Lavinen 		if (!count[i])
1217a3215902SJarkko Lavinen 			continue;
1218a3215902SJarkko Lavinen 
1219a3215902SJarkko Lavinen 		if (min[i] != max[i])
1220a3215902SJarkko Lavinen 			seq_printf(s, "%s:\t%5d erase blocks, erased min %d, "
1221a3215902SJarkko Lavinen 				"max %d times\n",
1222a3215902SJarkko Lavinen 				name[i], count[i], min[i], max[i]);
1223a3215902SJarkko Lavinen 		else
1224a3215902SJarkko Lavinen 			seq_printf(s, "%s:\t%5d erase blocks, all erased %d "
1225a3215902SJarkko Lavinen 				"times\n", name[i], count[i], min[i]);
1226a3215902SJarkko Lavinen 	}
1227a3215902SJarkko Lavinen 
1228a3215902SJarkko Lavinen 	if (bb_cnt)
1229a3215902SJarkko Lavinen 		seq_printf(s, "bad:\t%5u erase blocks\n", bb_cnt);
1230a3215902SJarkko Lavinen 
1231a3215902SJarkko Lavinen 	if (cw)
1232a3215902SJarkko Lavinen 		seq_printf(s, "current erase block: %u pages used, %u free, "
1233a3215902SJarkko Lavinen 			"erased %u times\n",
1234a3215902SJarkko Lavinen 			cwp, d->pages_per_eblk - cwp, cwecount);
1235a3215902SJarkko Lavinen 
1236a3215902SJarkko Lavinen 	seq_printf(s, "total erasures: %lu\n", sum);
1237a3215902SJarkko Lavinen 
12386f3c0f16SSamarth Parikh 	seq_puts(s, "\n");
1239a3215902SJarkko Lavinen 
1240a3215902SJarkko Lavinen 	seq_printf(s, "mtdswap_readsect count: %llu\n", d->sect_read_count);
1241a3215902SJarkko Lavinen 	seq_printf(s, "mtdswap_writesect count: %llu\n", d->sect_write_count);
1242a3215902SJarkko Lavinen 	seq_printf(s, "mtdswap_discard count: %llu\n", d->discard_count);
1243a3215902SJarkko Lavinen 	seq_printf(s, "mtd read count: %llu\n", d->mtd_read_count);
1244a3215902SJarkko Lavinen 	seq_printf(s, "mtd write count: %llu\n", d->mtd_write_count);
1245a3215902SJarkko Lavinen 	seq_printf(s, "discarded pages count: %llu\n", d->discard_page_count);
1246a3215902SJarkko Lavinen 
12476f3c0f16SSamarth Parikh 	seq_puts(s, "\n");
124889d8d320SDavid Woodhouse 	seq_printf(s, "total pages: %u\n", pages);
1249a3215902SJarkko Lavinen 	seq_printf(s, "pages mapped: %u\n", mapped);
1250a3215902SJarkko Lavinen 
1251a3215902SJarkko Lavinen 	return 0;
1252a3215902SJarkko Lavinen }
1253c78f59d7SYangtao Li DEFINE_SHOW_ATTRIBUTE(mtdswap);
1254a3215902SJarkko Lavinen 
mtdswap_add_debugfs(struct mtdswap_dev * d)1255a3215902SJarkko Lavinen static int mtdswap_add_debugfs(struct mtdswap_dev *d)
1256a3215902SJarkko Lavinen {
1257e8e3edb9SMario Rugiero 	struct dentry *root = d->mtd->dbg.dfs_dir;
1258a3215902SJarkko Lavinen 
1259e8e3edb9SMario Rugiero 	if (!IS_ENABLED(CONFIG_DEBUG_FS))
1260a3215902SJarkko Lavinen 		return 0;
1261a3215902SJarkko Lavinen 
1262e8e3edb9SMario Rugiero 	if (IS_ERR_OR_NULL(root))
1263a3215902SJarkko Lavinen 		return -1;
1264a3215902SJarkko Lavinen 
1265c2d73ba8SGreg Kroah-Hartman 	debugfs_create_file("mtdswap_stats", S_IRUSR, root, d, &mtdswap_fops);
1266a3215902SJarkko Lavinen 
1267a3215902SJarkko Lavinen 	return 0;
1268a3215902SJarkko Lavinen }
1269a3215902SJarkko Lavinen 
mtdswap_init(struct mtdswap_dev * d,unsigned int eblocks,unsigned int spare_cnt)1270a3215902SJarkko Lavinen static int mtdswap_init(struct mtdswap_dev *d, unsigned int eblocks,
1271a3215902SJarkko Lavinen 			unsigned int spare_cnt)
1272a3215902SJarkko Lavinen {
1273a3215902SJarkko Lavinen 	struct mtd_info *mtd = d->mbd_dev->mtd;
1274a3215902SJarkko Lavinen 	unsigned int i, eblk_bytes, pages, blocks;
1275a3215902SJarkko Lavinen 	int ret = -ENOMEM;
1276a3215902SJarkko Lavinen 
1277a3215902SJarkko Lavinen 	d->mtd = mtd;
1278a3215902SJarkko Lavinen 	d->eblks = eblocks;
1279a3215902SJarkko Lavinen 	d->spare_eblks = spare_cnt;
1280a3215902SJarkko Lavinen 	d->pages_per_eblk = mtd->erasesize >> PAGE_SHIFT;
1281a3215902SJarkko Lavinen 
1282a3215902SJarkko Lavinen 	pages = d->mbd_dev->size;
1283a3215902SJarkko Lavinen 	blocks = eblocks * d->pages_per_eblk;
1284a3215902SJarkko Lavinen 
1285a3215902SJarkko Lavinen 	for (i = 0; i < MTDSWAP_TREE_CNT; i++)
1286a3215902SJarkko Lavinen 		d->trees[i].root = RB_ROOT;
1287a3215902SJarkko Lavinen 
128842bc47b3SKees Cook 	d->page_data = vmalloc(array_size(pages, sizeof(int)));
1289a3215902SJarkko Lavinen 	if (!d->page_data)
1290a3215902SJarkko Lavinen 		goto page_data_fail;
1291a3215902SJarkko Lavinen 
129242bc47b3SKees Cook 	d->revmap = vmalloc(array_size(blocks, sizeof(int)));
1293a3215902SJarkko Lavinen 	if (!d->revmap)
1294a3215902SJarkko Lavinen 		goto revmap_fail;
1295a3215902SJarkko Lavinen 
1296a3215902SJarkko Lavinen 	eblk_bytes = sizeof(struct swap_eb)*d->eblks;
12971712cde2SJoe Perches 	d->eb_data = vzalloc(eblk_bytes);
1298a3215902SJarkko Lavinen 	if (!d->eb_data)
1299a3215902SJarkko Lavinen 		goto eb_data_fail;
1300a3215902SJarkko Lavinen 
1301a3215902SJarkko Lavinen 	for (i = 0; i < pages; i++)
1302a3215902SJarkko Lavinen 		d->page_data[i] = BLOCK_UNDEF;
1303a3215902SJarkko Lavinen 
1304a3215902SJarkko Lavinen 	for (i = 0; i < blocks; i++)
1305a3215902SJarkko Lavinen 		d->revmap[i] = PAGE_UNDEF;
1306a3215902SJarkko Lavinen 
1307a3215902SJarkko Lavinen 	d->page_buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1308a3215902SJarkko Lavinen 	if (!d->page_buf)
1309a3215902SJarkko Lavinen 		goto page_buf_fail;
1310a3215902SJarkko Lavinen 
13116da2ec56SKees Cook 	d->oob_buf = kmalloc_array(2, mtd->oobavail, GFP_KERNEL);
1312a3215902SJarkko Lavinen 	if (!d->oob_buf)
1313a3215902SJarkko Lavinen 		goto oob_buf_fail;
1314a3215902SJarkko Lavinen 
1315a3215902SJarkko Lavinen 	mtdswap_scan_eblks(d);
1316a3215902SJarkko Lavinen 
1317a3215902SJarkko Lavinen 	return 0;
1318a3215902SJarkko Lavinen 
1319a3215902SJarkko Lavinen oob_buf_fail:
1320a3215902SJarkko Lavinen 	kfree(d->page_buf);
1321a3215902SJarkko Lavinen page_buf_fail:
1322a3215902SJarkko Lavinen 	vfree(d->eb_data);
1323a3215902SJarkko Lavinen eb_data_fail:
1324a3215902SJarkko Lavinen 	vfree(d->revmap);
1325a3215902SJarkko Lavinen revmap_fail:
1326a3215902SJarkko Lavinen 	vfree(d->page_data);
1327a3215902SJarkko Lavinen page_data_fail:
1328a3215902SJarkko Lavinen 	printk(KERN_ERR "%s: init failed (%d)\n", MTDSWAP_PREFIX, ret);
1329a3215902SJarkko Lavinen 	return ret;
1330a3215902SJarkko Lavinen }
1331a3215902SJarkko Lavinen 
mtdswap_add_mtd(struct mtd_blktrans_ops * tr,struct mtd_info * mtd)1332a3215902SJarkko Lavinen static void mtdswap_add_mtd(struct mtd_blktrans_ops *tr, struct mtd_info *mtd)
1333a3215902SJarkko Lavinen {
1334a3215902SJarkko Lavinen 	struct mtdswap_dev *d;
1335a3215902SJarkko Lavinen 	struct mtd_blktrans_dev *mbd_dev;
1336a3215902SJarkko Lavinen 	char *parts;
1337a3215902SJarkko Lavinen 	char *this_opt;
1338a3215902SJarkko Lavinen 	unsigned long part;
1339a3215902SJarkko Lavinen 	unsigned int eblocks, eavailable, bad_blocks, spare_cnt;
1340a3215902SJarkko Lavinen 	uint64_t swap_size, use_size, size_limit;
1341a3215902SJarkko Lavinen 	int ret;
1342a3215902SJarkko Lavinen 
1343a3215902SJarkko Lavinen 	parts = &partitions[0];
1344a3215902SJarkko Lavinen 	if (!*parts)
1345a3215902SJarkko Lavinen 		return;
1346a3215902SJarkko Lavinen 
1347a3215902SJarkko Lavinen 	while ((this_opt = strsep(&parts, ",")) != NULL) {
13483156231fSJingoo Han 		if (kstrtoul(this_opt, 0, &part) < 0)
1349a3215902SJarkko Lavinen 			return;
1350a3215902SJarkko Lavinen 
1351a3215902SJarkko Lavinen 		if (mtd->index == part)
1352a3215902SJarkko Lavinen 			break;
1353a3215902SJarkko Lavinen 	}
1354a3215902SJarkko Lavinen 
1355a3215902SJarkko Lavinen 	if (mtd->index != part)
1356a3215902SJarkko Lavinen 		return;
1357a3215902SJarkko Lavinen 
1358a3215902SJarkko Lavinen 	if (mtd->erasesize < PAGE_SIZE || mtd->erasesize % PAGE_SIZE) {
1359a3215902SJarkko Lavinen 		printk(KERN_ERR "%s: Erase size %u not multiple of PAGE_SIZE "
1360a3215902SJarkko Lavinen 			"%lu\n", MTDSWAP_PREFIX, mtd->erasesize, PAGE_SIZE);
1361a3215902SJarkko Lavinen 		return;
1362a3215902SJarkko Lavinen 	}
1363a3215902SJarkko Lavinen 
1364a3215902SJarkko Lavinen 	if (PAGE_SIZE % mtd->writesize || mtd->writesize > PAGE_SIZE) {
1365a3215902SJarkko Lavinen 		printk(KERN_ERR "%s: PAGE_SIZE %lu not multiple of write size"
1366a3215902SJarkko Lavinen 			" %u\n", MTDSWAP_PREFIX, PAGE_SIZE, mtd->writesize);
1367a3215902SJarkko Lavinen 		return;
1368a3215902SJarkko Lavinen 	}
1369a3215902SJarkko Lavinen 
1370f5b8aa78SBoris BREZILLON 	if (!mtd->oobsize || mtd->oobavail < MTDSWAP_OOBSIZE) {
1371a3215902SJarkko Lavinen 		printk(KERN_ERR "%s: Not enough free bytes in OOB, "
13722130ad32SRandy Dunlap 			"%d available, %zu needed.\n",
1373f5b8aa78SBoris BREZILLON 			MTDSWAP_PREFIX, mtd->oobavail, MTDSWAP_OOBSIZE);
1374a3215902SJarkko Lavinen 		return;
1375a3215902SJarkko Lavinen 	}
1376a3215902SJarkko Lavinen 
1377a3215902SJarkko Lavinen 	if (spare_eblocks > 100)
1378a3215902SJarkko Lavinen 		spare_eblocks = 100;
1379a3215902SJarkko Lavinen 
1380a3215902SJarkko Lavinen 	use_size = mtd->size;
1381a3215902SJarkko Lavinen 	size_limit = (uint64_t) BLOCK_MAX * PAGE_SIZE;
1382a3215902SJarkko Lavinen 
1383a3215902SJarkko Lavinen 	if (mtd->size > size_limit) {
1384a3215902SJarkko Lavinen 		printk(KERN_WARNING "%s: Device too large. Limiting size to "
1385a3215902SJarkko Lavinen 			"%llu bytes\n", MTDSWAP_PREFIX, size_limit);
1386a3215902SJarkko Lavinen 		use_size = size_limit;
1387a3215902SJarkko Lavinen 	}
1388a3215902SJarkko Lavinen 
1389a3215902SJarkko Lavinen 	eblocks = mtd_div_by_eb(use_size, mtd);
13908c3f3f1dSBrian Norris 	use_size = (uint64_t)eblocks * mtd->erasesize;
1391a3215902SJarkko Lavinen 	bad_blocks = mtdswap_badblocks(mtd, use_size);
1392a3215902SJarkko Lavinen 	eavailable = eblocks - bad_blocks;
1393a3215902SJarkko Lavinen 
1394a3215902SJarkko Lavinen 	if (eavailable < MIN_ERASE_BLOCKS) {
1395a3215902SJarkko Lavinen 		printk(KERN_ERR "%s: Not enough erase blocks. %u available, "
1396a3215902SJarkko Lavinen 			"%d needed\n", MTDSWAP_PREFIX, eavailable,
1397a3215902SJarkko Lavinen 			MIN_ERASE_BLOCKS);
1398a3215902SJarkko Lavinen 		return;
1399a3215902SJarkko Lavinen 	}
1400a3215902SJarkko Lavinen 
1401a3215902SJarkko Lavinen 	spare_cnt = div_u64((uint64_t)eavailable * spare_eblocks, 100);
1402a3215902SJarkko Lavinen 
1403a3215902SJarkko Lavinen 	if (spare_cnt < MIN_SPARE_EBLOCKS)
1404a3215902SJarkko Lavinen 		spare_cnt = MIN_SPARE_EBLOCKS;
1405a3215902SJarkko Lavinen 
1406a3215902SJarkko Lavinen 	if (spare_cnt > eavailable - 1)
1407a3215902SJarkko Lavinen 		spare_cnt = eavailable - 1;
1408a3215902SJarkko Lavinen 
1409a3215902SJarkko Lavinen 	swap_size = (uint64_t)(eavailable - spare_cnt) * mtd->erasesize +
1410a3215902SJarkko Lavinen 		(header ? PAGE_SIZE : 0);
1411a3215902SJarkko Lavinen 
1412a3215902SJarkko Lavinen 	printk(KERN_INFO "%s: Enabling MTD swap on device %lu, size %llu KB, "
1413a3215902SJarkko Lavinen 		"%u spare, %u bad blocks\n",
1414a3215902SJarkko Lavinen 		MTDSWAP_PREFIX, part, swap_size / 1024, spare_cnt, bad_blocks);
1415a3215902SJarkko Lavinen 
1416a3215902SJarkko Lavinen 	d = kzalloc(sizeof(struct mtdswap_dev), GFP_KERNEL);
1417a3215902SJarkko Lavinen 	if (!d)
1418a3215902SJarkko Lavinen 		return;
1419a3215902SJarkko Lavinen 
1420a3215902SJarkko Lavinen 	mbd_dev = kzalloc(sizeof(struct mtd_blktrans_dev), GFP_KERNEL);
1421a3215902SJarkko Lavinen 	if (!mbd_dev) {
1422a3215902SJarkko Lavinen 		kfree(d);
1423a3215902SJarkko Lavinen 		return;
1424a3215902SJarkko Lavinen 	}
1425a3215902SJarkko Lavinen 
1426a3215902SJarkko Lavinen 	d->mbd_dev = mbd_dev;
1427a3215902SJarkko Lavinen 	mbd_dev->priv = d;
1428a3215902SJarkko Lavinen 
1429a3215902SJarkko Lavinen 	mbd_dev->mtd = mtd;
1430a3215902SJarkko Lavinen 	mbd_dev->devnum = mtd->index;
1431a3215902SJarkko Lavinen 	mbd_dev->size = swap_size >> PAGE_SHIFT;
1432a3215902SJarkko Lavinen 	mbd_dev->tr = tr;
1433a3215902SJarkko Lavinen 
1434a3215902SJarkko Lavinen 	if (!(mtd->flags & MTD_WRITEABLE))
1435a3215902SJarkko Lavinen 		mbd_dev->readonly = 1;
1436a3215902SJarkko Lavinen 
1437a3215902SJarkko Lavinen 	if (mtdswap_init(d, eblocks, spare_cnt) < 0)
1438a3215902SJarkko Lavinen 		goto init_failed;
1439a3215902SJarkko Lavinen 
1440a3215902SJarkko Lavinen 	if (add_mtd_blktrans_dev(mbd_dev) < 0)
1441a3215902SJarkko Lavinen 		goto cleanup;
1442a3215902SJarkko Lavinen 
1443a3215902SJarkko Lavinen 	d->dev = disk_to_dev(mbd_dev->disk);
1444a3215902SJarkko Lavinen 
1445a3215902SJarkko Lavinen 	ret = mtdswap_add_debugfs(d);
1446a3215902SJarkko Lavinen 	if (ret < 0)
1447a3215902SJarkko Lavinen 		goto debugfs_failed;
1448a3215902SJarkko Lavinen 
1449a3215902SJarkko Lavinen 	return;
1450a3215902SJarkko Lavinen 
1451a3215902SJarkko Lavinen debugfs_failed:
1452a3215902SJarkko Lavinen 	del_mtd_blktrans_dev(mbd_dev);
1453a3215902SJarkko Lavinen 
1454a3215902SJarkko Lavinen cleanup:
1455a3215902SJarkko Lavinen 	mtdswap_cleanup(d);
1456a3215902SJarkko Lavinen 
1457a3215902SJarkko Lavinen init_failed:
1458a3215902SJarkko Lavinen 	kfree(mbd_dev);
1459a3215902SJarkko Lavinen 	kfree(d);
1460a3215902SJarkko Lavinen }
1461a3215902SJarkko Lavinen 
mtdswap_remove_dev(struct mtd_blktrans_dev * dev)1462a3215902SJarkko Lavinen static void mtdswap_remove_dev(struct mtd_blktrans_dev *dev)
1463a3215902SJarkko Lavinen {
1464a3215902SJarkko Lavinen 	struct mtdswap_dev *d = MTDSWAP_MBD_TO_MTDSWAP(dev);
1465a3215902SJarkko Lavinen 
1466a3215902SJarkko Lavinen 	del_mtd_blktrans_dev(dev);
1467a3215902SJarkko Lavinen 	mtdswap_cleanup(d);
1468a3215902SJarkko Lavinen 	kfree(d);
1469a3215902SJarkko Lavinen }
1470a3215902SJarkko Lavinen 
1471a3215902SJarkko Lavinen static struct mtd_blktrans_ops mtdswap_ops = {
1472a3215902SJarkko Lavinen 	.name		= "mtdswap",
1473a3215902SJarkko Lavinen 	.major		= 0,
1474a3215902SJarkko Lavinen 	.part_bits	= 0,
1475a3215902SJarkko Lavinen 	.blksize	= PAGE_SIZE,
1476a3215902SJarkko Lavinen 	.flush		= mtdswap_flush,
1477a3215902SJarkko Lavinen 	.readsect	= mtdswap_readsect,
1478a3215902SJarkko Lavinen 	.writesect	= mtdswap_writesect,
1479a3215902SJarkko Lavinen 	.discard	= mtdswap_discard,
1480a3215902SJarkko Lavinen 	.background	= mtdswap_background,
1481a3215902SJarkko Lavinen 	.add_mtd	= mtdswap_add_mtd,
1482a3215902SJarkko Lavinen 	.remove_dev	= mtdswap_remove_dev,
1483a3215902SJarkko Lavinen 	.owner		= THIS_MODULE,
1484a3215902SJarkko Lavinen };
1485a3215902SJarkko Lavinen 
14861d5b7d47SDejin Zheng module_mtd_blktrans(mtdswap_ops);
1487a3215902SJarkko Lavinen 
1488a3215902SJarkko Lavinen MODULE_LICENSE("GPL");
1489a3215902SJarkko Lavinen MODULE_AUTHOR("Jarkko Lavinen <jarkko.lavinen@nokia.com>");
1490a3215902SJarkko Lavinen MODULE_DESCRIPTION("Block device access to an MTD suitable for using as "
1491a3215902SJarkko Lavinen 		"swap space");
1492