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> 22a3215902SJarkko Lavinen #include <linux/genhd.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 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 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 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 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 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 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 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 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 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; 326a3215902SJarkko Lavinen 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 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; 373a3215902SJarkko Lavinen 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 */ 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 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 */ 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 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 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 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 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 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 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 eb = d->eb_data + *newblock / d->pages_per_eblk; 720a3215902SJarkko Lavinen d->page_data[page] = *newblock; 721a3215902SJarkko Lavinen d->revmap[oldblock] = PAGE_UNDEF; 722a3215902SJarkko Lavinen eb = d->eb_data + oldblock / d->pages_per_eblk; 723a3215902SJarkko Lavinen eb->active_count--; 724a3215902SJarkko Lavinen 725a3215902SJarkko Lavinen return 0; 726a3215902SJarkko Lavinen 727a3215902SJarkko Lavinen read_error: 728a3215902SJarkko Lavinen d->page_data[page] = BLOCK_ERROR; 729a3215902SJarkko Lavinen d->revmap[oldblock] = PAGE_UNDEF; 730a3215902SJarkko Lavinen return ret; 731a3215902SJarkko Lavinen } 732a3215902SJarkko Lavinen 733a3215902SJarkko Lavinen static int mtdswap_gc_eblock(struct mtdswap_dev *d, struct swap_eb *eb) 734a3215902SJarkko Lavinen { 735a3215902SJarkko Lavinen unsigned int i, block, eblk_base, newblock; 736a3215902SJarkko Lavinen int ret, errcode; 737a3215902SJarkko Lavinen 738a3215902SJarkko Lavinen errcode = 0; 739a3215902SJarkko Lavinen eblk_base = (eb - d->eb_data) * d->pages_per_eblk; 740a3215902SJarkko Lavinen 741a3215902SJarkko Lavinen for (i = 0; i < d->pages_per_eblk; i++) { 742a3215902SJarkko Lavinen if (d->spare_eblks < MIN_SPARE_EBLOCKS) 743a3215902SJarkko Lavinen return -ENOSPC; 744a3215902SJarkko Lavinen 745a3215902SJarkko Lavinen block = eblk_base + i; 746a3215902SJarkko Lavinen if (d->revmap[block] == PAGE_UNDEF) 747a3215902SJarkko Lavinen continue; 748a3215902SJarkko Lavinen 749a3215902SJarkko Lavinen ret = mtdswap_move_block(d, block, &newblock); 750a3215902SJarkko Lavinen if (ret < 0 && !errcode) 751a3215902SJarkko Lavinen errcode = ret; 752a3215902SJarkko Lavinen } 753a3215902SJarkko Lavinen 754a3215902SJarkko Lavinen return errcode; 755a3215902SJarkko Lavinen } 756a3215902SJarkko Lavinen 757a3215902SJarkko Lavinen static int __mtdswap_choose_gc_tree(struct mtdswap_dev *d) 758a3215902SJarkko Lavinen { 759a3215902SJarkko Lavinen int idx, stopat; 760a3215902SJarkko Lavinen 761a5929b64SArvind Yadav if (TREE_COUNT(d, CLEAN) < LOW_FRAG_GC_THRESHOLD) 762a3215902SJarkko Lavinen stopat = MTDSWAP_LOWFRAG; 763a3215902SJarkko Lavinen else 764a3215902SJarkko Lavinen stopat = MTDSWAP_HIFRAG; 765a3215902SJarkko Lavinen 766a3215902SJarkko Lavinen for (idx = MTDSWAP_BITFLIP; idx >= stopat; idx--) 767a3215902SJarkko Lavinen if (d->trees[idx].root.rb_node != NULL) 768a3215902SJarkko Lavinen return idx; 769a3215902SJarkko Lavinen 770a3215902SJarkko Lavinen return -1; 771a3215902SJarkko Lavinen } 772a3215902SJarkko Lavinen 773a3215902SJarkko Lavinen static int mtdswap_wlfreq(unsigned int maxdiff) 774a3215902SJarkko Lavinen { 775a3215902SJarkko Lavinen unsigned int h, x, y, dist, base; 776a3215902SJarkko Lavinen 777a3215902SJarkko Lavinen /* 778a3215902SJarkko Lavinen * Calculate linear ramp down from f1 to f2 when maxdiff goes from 779a3215902SJarkko Lavinen * MAX_ERASE_DIFF to MAX_ERASE_DIFF + COLLECT_NONDIRTY_BASE. Similar 780a3215902SJarkko Lavinen * to triangle with height f1 - f1 and width COLLECT_NONDIRTY_BASE. 781a3215902SJarkko Lavinen */ 782a3215902SJarkko Lavinen 783a3215902SJarkko Lavinen dist = maxdiff - MAX_ERASE_DIFF; 784a3215902SJarkko Lavinen if (dist > COLLECT_NONDIRTY_BASE) 785a3215902SJarkko Lavinen dist = COLLECT_NONDIRTY_BASE; 786a3215902SJarkko Lavinen 787a3215902SJarkko Lavinen /* 788a3215902SJarkko Lavinen * Modelling the slop as right angular triangle with base 789a3215902SJarkko Lavinen * COLLECT_NONDIRTY_BASE and height freq1 - freq2. The ratio y/x is 790a3215902SJarkko Lavinen * equal to the ratio h/base. 791a3215902SJarkko Lavinen */ 792a3215902SJarkko Lavinen h = COLLECT_NONDIRTY_FREQ1 - COLLECT_NONDIRTY_FREQ2; 793a3215902SJarkko Lavinen base = COLLECT_NONDIRTY_BASE; 794a3215902SJarkko Lavinen 795a3215902SJarkko Lavinen x = dist - base; 796a3215902SJarkko Lavinen y = (x * h + base / 2) / base; 797a3215902SJarkko Lavinen 798a3215902SJarkko Lavinen return COLLECT_NONDIRTY_FREQ2 + y; 799a3215902SJarkko Lavinen } 800a3215902SJarkko Lavinen 801a3215902SJarkko Lavinen static int mtdswap_choose_wl_tree(struct mtdswap_dev *d) 802a3215902SJarkko Lavinen { 803a3215902SJarkko Lavinen static unsigned int pick_cnt; 80468b1a1e7SArtem Bityutskiy unsigned int i, idx = -1, wear, max; 805a3215902SJarkko Lavinen struct rb_root *root; 806a3215902SJarkko Lavinen 807a3215902SJarkko Lavinen max = 0; 808a3215902SJarkko Lavinen for (i = 0; i <= MTDSWAP_DIRTY; i++) { 809a3215902SJarkko Lavinen root = &d->trees[i].root; 810a3215902SJarkko Lavinen if (root->rb_node == NULL) 811a3215902SJarkko Lavinen continue; 812a3215902SJarkko Lavinen 813a3215902SJarkko Lavinen wear = d->max_erase_count - MTDSWAP_ECNT_MIN(root); 814a3215902SJarkko Lavinen if (wear > max) { 815a3215902SJarkko Lavinen max = wear; 816a3215902SJarkko Lavinen idx = i; 817a3215902SJarkko Lavinen } 818a3215902SJarkko Lavinen } 819a3215902SJarkko Lavinen 820a3215902SJarkko Lavinen if (max > MAX_ERASE_DIFF && pick_cnt >= mtdswap_wlfreq(max) - 1) { 821a3215902SJarkko Lavinen pick_cnt = 0; 822a3215902SJarkko Lavinen return idx; 823a3215902SJarkko Lavinen } 824a3215902SJarkko Lavinen 825a3215902SJarkko Lavinen pick_cnt++; 826a3215902SJarkko Lavinen return -1; 827a3215902SJarkko Lavinen } 828a3215902SJarkko Lavinen 829a3215902SJarkko Lavinen static int mtdswap_choose_gc_tree(struct mtdswap_dev *d, 830a3215902SJarkko Lavinen unsigned int background) 831a3215902SJarkko Lavinen { 832a3215902SJarkko Lavinen int idx; 833a3215902SJarkko Lavinen 834a3215902SJarkko Lavinen if (TREE_NONEMPTY(d, FAILING) && 835a3215902SJarkko Lavinen (background || (TREE_EMPTY(d, CLEAN) && TREE_EMPTY(d, DIRTY)))) 836a3215902SJarkko Lavinen return MTDSWAP_FAILING; 837a3215902SJarkko Lavinen 838a3215902SJarkko Lavinen idx = mtdswap_choose_wl_tree(d); 839a3215902SJarkko Lavinen if (idx >= MTDSWAP_CLEAN) 840a3215902SJarkko Lavinen return idx; 841a3215902SJarkko Lavinen 842a3215902SJarkko Lavinen return __mtdswap_choose_gc_tree(d); 843a3215902SJarkko Lavinen } 844a3215902SJarkko Lavinen 845a3215902SJarkko Lavinen static struct swap_eb *mtdswap_pick_gc_eblk(struct mtdswap_dev *d, 846a3215902SJarkko Lavinen unsigned int background) 847a3215902SJarkko Lavinen { 848a3215902SJarkko Lavinen struct rb_root *rp = NULL; 849a3215902SJarkko Lavinen struct swap_eb *eb = NULL; 850a3215902SJarkko Lavinen int idx; 851a3215902SJarkko Lavinen 852a3215902SJarkko Lavinen if (background && TREE_COUNT(d, CLEAN) > CLEAN_BLOCK_THRESHOLD && 853a3215902SJarkko Lavinen TREE_EMPTY(d, DIRTY) && TREE_EMPTY(d, FAILING)) 854a3215902SJarkko Lavinen return NULL; 855a3215902SJarkko Lavinen 856a3215902SJarkko Lavinen idx = mtdswap_choose_gc_tree(d, background); 857a3215902SJarkko Lavinen if (idx < 0) 858a3215902SJarkko Lavinen return NULL; 859a3215902SJarkko Lavinen 860a3215902SJarkko Lavinen rp = &d->trees[idx].root; 861a3215902SJarkko Lavinen eb = rb_entry(rb_first(rp), struct swap_eb, rb); 862a3215902SJarkko Lavinen 863a3215902SJarkko Lavinen rb_erase(&eb->rb, rp); 864a3215902SJarkko Lavinen eb->root = NULL; 865a3215902SJarkko Lavinen d->trees[idx].count--; 866a3215902SJarkko Lavinen return eb; 867a3215902SJarkko Lavinen } 868a3215902SJarkko Lavinen 869a3215902SJarkko Lavinen static unsigned int mtdswap_test_patt(unsigned int i) 870a3215902SJarkko Lavinen { 871a3215902SJarkko Lavinen return i % 2 ? 0x55555555 : 0xAAAAAAAA; 872a3215902SJarkko Lavinen } 873a3215902SJarkko Lavinen 874a3215902SJarkko Lavinen static unsigned int mtdswap_eblk_passes(struct mtdswap_dev *d, 875a3215902SJarkko Lavinen struct swap_eb *eb) 876a3215902SJarkko Lavinen { 877a3215902SJarkko Lavinen struct mtd_info *mtd = d->mtd; 878a3215902SJarkko Lavinen unsigned int test, i, j, patt, mtd_pages; 879a3215902SJarkko Lavinen loff_t base, pos; 880a3215902SJarkko Lavinen unsigned int *p1 = (unsigned int *)d->page_buf; 881a3215902SJarkko Lavinen unsigned char *p2 = (unsigned char *)d->oob_buf; 882a3215902SJarkko Lavinen struct mtd_oob_ops ops; 883a3215902SJarkko Lavinen int ret; 884a3215902SJarkko Lavinen 8850612b9ddSBrian Norris ops.mode = MTD_OPS_AUTO_OOB; 886a3215902SJarkko Lavinen ops.len = mtd->writesize; 887f5b8aa78SBoris BREZILLON ops.ooblen = mtd->oobavail; 888a3215902SJarkko Lavinen ops.ooboffs = 0; 889a3215902SJarkko Lavinen ops.datbuf = d->page_buf; 890a3215902SJarkko Lavinen ops.oobbuf = d->oob_buf; 891a3215902SJarkko Lavinen base = mtdswap_eb_offset(d, eb); 892a3215902SJarkko Lavinen mtd_pages = d->pages_per_eblk * PAGE_SIZE / mtd->writesize; 893a3215902SJarkko Lavinen 894a3215902SJarkko Lavinen for (test = 0; test < 2; test++) { 895a3215902SJarkko Lavinen pos = base; 896a3215902SJarkko Lavinen for (i = 0; i < mtd_pages; i++) { 897a3215902SJarkko Lavinen patt = mtdswap_test_patt(test + i); 898a3215902SJarkko Lavinen memset(d->page_buf, patt, mtd->writesize); 899f5b8aa78SBoris BREZILLON memset(d->oob_buf, patt, mtd->oobavail); 900a2cc5ba0SArtem Bityutskiy ret = mtd_write_oob(mtd, pos, &ops); 901a3215902SJarkko Lavinen if (ret) 902a3215902SJarkko Lavinen goto error; 903a3215902SJarkko Lavinen 904a3215902SJarkko Lavinen pos += mtd->writesize; 905a3215902SJarkko Lavinen } 906a3215902SJarkko Lavinen 907a3215902SJarkko Lavinen pos = base; 908a3215902SJarkko Lavinen for (i = 0; i < mtd_pages; i++) { 909fd2819bbSArtem Bityutskiy ret = mtd_read_oob(mtd, pos, &ops); 910a3215902SJarkko Lavinen if (ret) 911a3215902SJarkko Lavinen goto error; 912a3215902SJarkko Lavinen 913a3215902SJarkko Lavinen patt = mtdswap_test_patt(test + i); 914a3215902SJarkko Lavinen for (j = 0; j < mtd->writesize/sizeof(int); j++) 915a3215902SJarkko Lavinen if (p1[j] != patt) 916a3215902SJarkko Lavinen goto error; 917a3215902SJarkko Lavinen 918f5b8aa78SBoris BREZILLON for (j = 0; j < mtd->oobavail; j++) 919a3215902SJarkko Lavinen if (p2[j] != (unsigned char)patt) 920a3215902SJarkko Lavinen goto error; 921a3215902SJarkko Lavinen 922a3215902SJarkko Lavinen pos += mtd->writesize; 923a3215902SJarkko Lavinen } 924a3215902SJarkko Lavinen 925a3215902SJarkko Lavinen ret = mtdswap_erase_block(d, eb); 926a3215902SJarkko Lavinen if (ret) 927a3215902SJarkko Lavinen goto error; 928a3215902SJarkko Lavinen } 929a3215902SJarkko Lavinen 930a3215902SJarkko Lavinen eb->flags &= ~EBLOCK_READERR; 931a3215902SJarkko Lavinen return 1; 932a3215902SJarkko Lavinen 933a3215902SJarkko Lavinen error: 934a3215902SJarkko Lavinen mtdswap_handle_badblock(d, eb); 935a3215902SJarkko Lavinen return 0; 936a3215902SJarkko Lavinen } 937a3215902SJarkko Lavinen 938a3215902SJarkko Lavinen static int mtdswap_gc(struct mtdswap_dev *d, unsigned int background) 939a3215902SJarkko Lavinen { 940a3215902SJarkko Lavinen struct swap_eb *eb; 941a3215902SJarkko Lavinen int ret; 942a3215902SJarkko Lavinen 943a3215902SJarkko Lavinen if (d->spare_eblks < MIN_SPARE_EBLOCKS) 944a3215902SJarkko Lavinen return 1; 945a3215902SJarkko Lavinen 946a3215902SJarkko Lavinen eb = mtdswap_pick_gc_eblk(d, background); 947a3215902SJarkko Lavinen if (!eb) 948a3215902SJarkko Lavinen return 1; 949a3215902SJarkko Lavinen 950a3215902SJarkko Lavinen ret = mtdswap_gc_eblock(d, eb); 951a3215902SJarkko Lavinen if (ret == -ENOSPC) 952a3215902SJarkko Lavinen return 1; 953a3215902SJarkko Lavinen 954a3215902SJarkko Lavinen if (eb->flags & EBLOCK_FAILED) { 955a3215902SJarkko Lavinen mtdswap_handle_badblock(d, eb); 956a3215902SJarkko Lavinen return 0; 957a3215902SJarkko Lavinen } 958a3215902SJarkko Lavinen 959a3215902SJarkko Lavinen eb->flags &= ~EBLOCK_BITFLIP; 960a3215902SJarkko Lavinen ret = mtdswap_erase_block(d, eb); 961a3215902SJarkko Lavinen if ((eb->flags & EBLOCK_READERR) && 962a3215902SJarkko Lavinen (ret || !mtdswap_eblk_passes(d, eb))) 963a3215902SJarkko Lavinen return 0; 964a3215902SJarkko Lavinen 965a3215902SJarkko Lavinen if (ret == 0) 966a3215902SJarkko Lavinen ret = mtdswap_write_marker(d, eb, MTDSWAP_TYPE_CLEAN); 967a3215902SJarkko Lavinen 968a3215902SJarkko Lavinen if (ret == 0) 969a3215902SJarkko Lavinen mtdswap_rb_add(d, eb, MTDSWAP_CLEAN); 970d57f4054SBrian Norris else if (ret != -EIO && !mtd_is_eccerr(ret)) 971a3215902SJarkko Lavinen mtdswap_rb_add(d, eb, MTDSWAP_DIRTY); 972a3215902SJarkko Lavinen 973a3215902SJarkko Lavinen return 0; 974a3215902SJarkko Lavinen } 975a3215902SJarkko Lavinen 976a3215902SJarkko Lavinen static void mtdswap_background(struct mtd_blktrans_dev *dev) 977a3215902SJarkko Lavinen { 978a3215902SJarkko Lavinen struct mtdswap_dev *d = MTDSWAP_MBD_TO_MTDSWAP(dev); 979a3215902SJarkko Lavinen int ret; 980a3215902SJarkko Lavinen 981a3215902SJarkko Lavinen while (1) { 982a3215902SJarkko Lavinen ret = mtdswap_gc(d, 1); 983a3215902SJarkko Lavinen if (ret || mtd_blktrans_cease_background(dev)) 984a3215902SJarkko Lavinen return; 985a3215902SJarkko Lavinen } 986a3215902SJarkko Lavinen } 987a3215902SJarkko Lavinen 988a3215902SJarkko Lavinen static void mtdswap_cleanup(struct mtdswap_dev *d) 989a3215902SJarkko Lavinen { 990a3215902SJarkko Lavinen vfree(d->eb_data); 991a3215902SJarkko Lavinen vfree(d->revmap); 992a3215902SJarkko Lavinen vfree(d->page_data); 993a3215902SJarkko Lavinen kfree(d->oob_buf); 994a3215902SJarkko Lavinen kfree(d->page_buf); 995a3215902SJarkko Lavinen } 996a3215902SJarkko Lavinen 997a3215902SJarkko Lavinen static int mtdswap_flush(struct mtd_blktrans_dev *dev) 998a3215902SJarkko Lavinen { 999a3215902SJarkko Lavinen struct mtdswap_dev *d = MTDSWAP_MBD_TO_MTDSWAP(dev); 1000a3215902SJarkko Lavinen 100185f2f2a8SArtem Bityutskiy mtd_sync(d->mtd); 1002a3215902SJarkko Lavinen return 0; 1003a3215902SJarkko Lavinen } 1004a3215902SJarkko Lavinen 1005a3215902SJarkko Lavinen static unsigned int mtdswap_badblocks(struct mtd_info *mtd, uint64_t size) 1006a3215902SJarkko Lavinen { 1007a3215902SJarkko Lavinen loff_t offset; 1008a3215902SJarkko Lavinen unsigned int badcnt; 1009a3215902SJarkko Lavinen 1010a3215902SJarkko Lavinen badcnt = 0; 1011a3215902SJarkko Lavinen 10128f461a73SArtem Bityutskiy if (mtd_can_have_bb(mtd)) 1013a3215902SJarkko Lavinen for (offset = 0; offset < size; offset += mtd->erasesize) 10147086c19dSArtem Bityutskiy if (mtd_block_isbad(mtd, offset)) 1015a3215902SJarkko Lavinen badcnt++; 1016a3215902SJarkko Lavinen 1017a3215902SJarkko Lavinen return badcnt; 1018a3215902SJarkko Lavinen } 1019a3215902SJarkko Lavinen 1020a3215902SJarkko Lavinen static int mtdswap_writesect(struct mtd_blktrans_dev *dev, 1021a3215902SJarkko Lavinen unsigned long page, char *buf) 1022a3215902SJarkko Lavinen { 1023a3215902SJarkko Lavinen struct mtdswap_dev *d = MTDSWAP_MBD_TO_MTDSWAP(dev); 1024a3215902SJarkko Lavinen unsigned int newblock, mapped; 1025a3215902SJarkko Lavinen struct swap_eb *eb; 1026a3215902SJarkko Lavinen int ret; 1027a3215902SJarkko Lavinen 1028a3215902SJarkko Lavinen d->sect_write_count++; 1029a3215902SJarkko Lavinen 1030a3215902SJarkko Lavinen if (d->spare_eblks < MIN_SPARE_EBLOCKS) 1031a3215902SJarkko Lavinen return -ENOSPC; 1032a3215902SJarkko Lavinen 1033a3215902SJarkko Lavinen if (header) { 1034a3215902SJarkko Lavinen /* Ignore writes to the header page */ 1035a3215902SJarkko Lavinen if (unlikely(page == 0)) 1036a3215902SJarkko Lavinen return 0; 1037a3215902SJarkko Lavinen 1038a3215902SJarkko Lavinen page--; 1039a3215902SJarkko Lavinen } 1040a3215902SJarkko Lavinen 1041a3215902SJarkko Lavinen mapped = d->page_data[page]; 1042a3215902SJarkko Lavinen if (mapped <= BLOCK_MAX) { 1043a3215902SJarkko Lavinen eb = d->eb_data + (mapped / d->pages_per_eblk); 1044a3215902SJarkko Lavinen eb->active_count--; 1045a3215902SJarkko Lavinen mtdswap_store_eb(d, eb); 1046a3215902SJarkko Lavinen d->page_data[page] = BLOCK_UNDEF; 1047a3215902SJarkko Lavinen d->revmap[mapped] = PAGE_UNDEF; 1048a3215902SJarkko Lavinen } 1049a3215902SJarkko Lavinen 1050a3215902SJarkko Lavinen ret = mtdswap_write_block(d, buf, page, &newblock, 0); 1051a3215902SJarkko Lavinen d->mtd_write_count++; 1052a3215902SJarkko Lavinen 1053a3215902SJarkko Lavinen if (ret < 0) 1054a3215902SJarkko Lavinen return ret; 1055a3215902SJarkko Lavinen 1056a3215902SJarkko Lavinen eb = d->eb_data + (newblock / d->pages_per_eblk); 1057a3215902SJarkko Lavinen d->page_data[page] = newblock; 1058a3215902SJarkko Lavinen 1059a3215902SJarkko Lavinen return 0; 1060a3215902SJarkko Lavinen } 1061a3215902SJarkko Lavinen 1062a3215902SJarkko Lavinen /* Provide a dummy swap header for the kernel */ 1063a3215902SJarkko Lavinen static int mtdswap_auto_header(struct mtdswap_dev *d, char *buf) 1064a3215902SJarkko Lavinen { 1065a3215902SJarkko Lavinen union swap_header *hd = (union swap_header *)(buf); 1066a3215902SJarkko Lavinen 1067a3215902SJarkko Lavinen memset(buf, 0, PAGE_SIZE - 10); 1068a3215902SJarkko Lavinen 1069a3215902SJarkko Lavinen hd->info.version = 1; 1070a3215902SJarkko Lavinen hd->info.last_page = d->mbd_dev->size - 1; 1071a3215902SJarkko Lavinen hd->info.nr_badpages = 0; 1072a3215902SJarkko Lavinen 1073a3215902SJarkko Lavinen memcpy(buf + PAGE_SIZE - 10, "SWAPSPACE2", 10); 1074a3215902SJarkko Lavinen 1075a3215902SJarkko Lavinen return 0; 1076a3215902SJarkko Lavinen } 1077a3215902SJarkko Lavinen 1078a3215902SJarkko Lavinen static int mtdswap_readsect(struct mtd_blktrans_dev *dev, 1079a3215902SJarkko Lavinen unsigned long page, char *buf) 1080a3215902SJarkko Lavinen { 1081a3215902SJarkko Lavinen struct mtdswap_dev *d = MTDSWAP_MBD_TO_MTDSWAP(dev); 1082a3215902SJarkko Lavinen struct mtd_info *mtd = d->mtd; 1083a3215902SJarkko Lavinen unsigned int realblock, retries; 1084a3215902SJarkko Lavinen loff_t readpos; 1085a3215902SJarkko Lavinen struct swap_eb *eb; 1086a3215902SJarkko Lavinen size_t retlen; 1087a3215902SJarkko Lavinen int ret; 1088a3215902SJarkko Lavinen 1089a3215902SJarkko Lavinen d->sect_read_count++; 1090a3215902SJarkko Lavinen 1091a3215902SJarkko Lavinen if (header) { 1092a3215902SJarkko Lavinen if (unlikely(page == 0)) 1093a3215902SJarkko Lavinen return mtdswap_auto_header(d, buf); 1094a3215902SJarkko Lavinen 1095a3215902SJarkko Lavinen page--; 1096a3215902SJarkko Lavinen } 1097a3215902SJarkko Lavinen 1098a3215902SJarkko Lavinen realblock = d->page_data[page]; 1099a3215902SJarkko Lavinen if (realblock > BLOCK_MAX) { 1100a3215902SJarkko Lavinen memset(buf, 0x0, PAGE_SIZE); 1101a3215902SJarkko Lavinen if (realblock == BLOCK_UNDEF) 1102a3215902SJarkko Lavinen return 0; 1103a3215902SJarkko Lavinen else 1104a3215902SJarkko Lavinen return -EIO; 1105a3215902SJarkko Lavinen } 1106a3215902SJarkko Lavinen 1107a3215902SJarkko Lavinen eb = d->eb_data + (realblock / d->pages_per_eblk); 1108a3215902SJarkko Lavinen BUG_ON(d->revmap[realblock] == PAGE_UNDEF); 1109a3215902SJarkko Lavinen 1110a3215902SJarkko Lavinen readpos = (loff_t)realblock << PAGE_SHIFT; 1111a3215902SJarkko Lavinen retries = 0; 1112a3215902SJarkko Lavinen 1113a3215902SJarkko Lavinen retry: 1114329ad399SArtem Bityutskiy ret = mtd_read(mtd, readpos, PAGE_SIZE, &retlen, buf); 1115a3215902SJarkko Lavinen 1116a3215902SJarkko Lavinen d->mtd_read_count++; 1117d57f4054SBrian Norris if (mtd_is_bitflip(ret)) { 1118a3215902SJarkko Lavinen eb->flags |= EBLOCK_BITFLIP; 1119a3215902SJarkko Lavinen mtdswap_rb_add(d, eb, MTDSWAP_BITFLIP); 1120a3215902SJarkko Lavinen ret = 0; 1121a3215902SJarkko Lavinen } 1122a3215902SJarkko Lavinen 1123a3215902SJarkko Lavinen if (ret < 0) { 1124a3215902SJarkko Lavinen dev_err(d->dev, "Read error %d\n", ret); 1125a3215902SJarkko Lavinen eb->flags |= EBLOCK_READERR; 1126a3215902SJarkko Lavinen mtdswap_rb_add(d, eb, MTDSWAP_FAILING); 1127a3215902SJarkko Lavinen retries++; 1128a3215902SJarkko Lavinen if (retries < MTDSWAP_IO_RETRIES) 1129a3215902SJarkko Lavinen goto retry; 1130a3215902SJarkko Lavinen 1131a3215902SJarkko Lavinen return ret; 1132a3215902SJarkko Lavinen } 1133a3215902SJarkko Lavinen 1134a3215902SJarkko Lavinen if (retlen != PAGE_SIZE) { 113589d8d320SDavid Woodhouse dev_err(d->dev, "Short read %zd\n", retlen); 1136a3215902SJarkko Lavinen return -EIO; 1137a3215902SJarkko Lavinen } 1138a3215902SJarkko Lavinen 1139a3215902SJarkko Lavinen return 0; 1140a3215902SJarkko Lavinen } 1141a3215902SJarkko Lavinen 1142a3215902SJarkko Lavinen static int mtdswap_discard(struct mtd_blktrans_dev *dev, unsigned long first, 1143a3215902SJarkko Lavinen unsigned nr_pages) 1144a3215902SJarkko Lavinen { 1145a3215902SJarkko Lavinen struct mtdswap_dev *d = MTDSWAP_MBD_TO_MTDSWAP(dev); 1146a3215902SJarkko Lavinen unsigned long page; 1147a3215902SJarkko Lavinen struct swap_eb *eb; 1148a3215902SJarkko Lavinen unsigned int mapped; 1149a3215902SJarkko Lavinen 1150a3215902SJarkko Lavinen d->discard_count++; 1151a3215902SJarkko Lavinen 1152a3215902SJarkko Lavinen for (page = first; page < first + nr_pages; page++) { 1153a3215902SJarkko Lavinen mapped = d->page_data[page]; 1154a3215902SJarkko Lavinen if (mapped <= BLOCK_MAX) { 1155a3215902SJarkko Lavinen eb = d->eb_data + (mapped / d->pages_per_eblk); 1156a3215902SJarkko Lavinen eb->active_count--; 1157a3215902SJarkko Lavinen mtdswap_store_eb(d, eb); 1158a3215902SJarkko Lavinen d->page_data[page] = BLOCK_UNDEF; 1159a3215902SJarkko Lavinen d->revmap[mapped] = PAGE_UNDEF; 1160a3215902SJarkko Lavinen d->discard_page_count++; 1161a3215902SJarkko Lavinen } else if (mapped == BLOCK_ERROR) { 1162a3215902SJarkko Lavinen d->page_data[page] = BLOCK_UNDEF; 1163a3215902SJarkko Lavinen d->discard_page_count++; 1164a3215902SJarkko Lavinen } 1165a3215902SJarkko Lavinen } 1166a3215902SJarkko Lavinen 1167a3215902SJarkko Lavinen return 0; 1168a3215902SJarkko Lavinen } 1169a3215902SJarkko Lavinen 1170a3215902SJarkko Lavinen static int mtdswap_show(struct seq_file *s, void *data) 1171a3215902SJarkko Lavinen { 1172a3215902SJarkko Lavinen struct mtdswap_dev *d = (struct mtdswap_dev *) s->private; 1173a3215902SJarkko Lavinen unsigned long sum; 1174a3215902SJarkko Lavinen unsigned int count[MTDSWAP_TREE_CNT]; 1175a3215902SJarkko Lavinen unsigned int min[MTDSWAP_TREE_CNT]; 1176a3215902SJarkko Lavinen unsigned int max[MTDSWAP_TREE_CNT]; 1177a3215902SJarkko Lavinen unsigned int i, cw = 0, cwp = 0, cwecount = 0, bb_cnt, mapped, pages; 1178a3215902SJarkko Lavinen uint64_t use_size; 1179bf657105SColin Ian King static const char * const name[] = { 1180bf657105SColin Ian King "clean", "used", "low", "high", "dirty", "bitflip", "failing" 1181bf657105SColin Ian King }; 1182a3215902SJarkko Lavinen 1183a3215902SJarkko Lavinen mutex_lock(&d->mbd_dev->lock); 1184a3215902SJarkko Lavinen 1185a3215902SJarkko Lavinen for (i = 0; i < MTDSWAP_TREE_CNT; i++) { 1186a3215902SJarkko Lavinen struct rb_root *root = &d->trees[i].root; 1187a3215902SJarkko Lavinen 1188a3215902SJarkko Lavinen if (root->rb_node) { 1189a3215902SJarkko Lavinen count[i] = d->trees[i].count; 1190e4a8aad8SGeliang Tang min[i] = MTDSWAP_ECNT_MIN(root); 1191e4a8aad8SGeliang Tang max[i] = MTDSWAP_ECNT_MAX(root); 1192a3215902SJarkko Lavinen } else 1193a3215902SJarkko Lavinen count[i] = 0; 1194a3215902SJarkko Lavinen } 1195a3215902SJarkko Lavinen 1196a3215902SJarkko Lavinen if (d->curr_write) { 1197a3215902SJarkko Lavinen cw = 1; 1198a3215902SJarkko Lavinen cwp = d->curr_write_pos; 1199a3215902SJarkko Lavinen cwecount = d->curr_write->erase_count; 1200a3215902SJarkko Lavinen } 1201a3215902SJarkko Lavinen 1202a3215902SJarkko Lavinen sum = 0; 1203a3215902SJarkko Lavinen for (i = 0; i < d->eblks; i++) 1204a3215902SJarkko Lavinen sum += d->eb_data[i].erase_count; 1205a3215902SJarkko Lavinen 1206a3215902SJarkko Lavinen use_size = (uint64_t)d->eblks * d->mtd->erasesize; 1207a3215902SJarkko Lavinen bb_cnt = mtdswap_badblocks(d->mtd, use_size); 1208a3215902SJarkko Lavinen 1209a3215902SJarkko Lavinen mapped = 0; 1210a3215902SJarkko Lavinen pages = d->mbd_dev->size; 1211a3215902SJarkko Lavinen for (i = 0; i < pages; i++) 1212a3215902SJarkko Lavinen if (d->page_data[i] != BLOCK_UNDEF) 1213a3215902SJarkko Lavinen mapped++; 1214a3215902SJarkko Lavinen 1215a3215902SJarkko Lavinen mutex_unlock(&d->mbd_dev->lock); 1216a3215902SJarkko Lavinen 1217a3215902SJarkko Lavinen for (i = 0; i < MTDSWAP_TREE_CNT; i++) { 1218a3215902SJarkko Lavinen if (!count[i]) 1219a3215902SJarkko Lavinen continue; 1220a3215902SJarkko Lavinen 1221a3215902SJarkko Lavinen if (min[i] != max[i]) 1222a3215902SJarkko Lavinen seq_printf(s, "%s:\t%5d erase blocks, erased min %d, " 1223a3215902SJarkko Lavinen "max %d times\n", 1224a3215902SJarkko Lavinen name[i], count[i], min[i], max[i]); 1225a3215902SJarkko Lavinen else 1226a3215902SJarkko Lavinen seq_printf(s, "%s:\t%5d erase blocks, all erased %d " 1227a3215902SJarkko Lavinen "times\n", name[i], count[i], min[i]); 1228a3215902SJarkko Lavinen } 1229a3215902SJarkko Lavinen 1230a3215902SJarkko Lavinen if (bb_cnt) 1231a3215902SJarkko Lavinen seq_printf(s, "bad:\t%5u erase blocks\n", bb_cnt); 1232a3215902SJarkko Lavinen 1233a3215902SJarkko Lavinen if (cw) 1234a3215902SJarkko Lavinen seq_printf(s, "current erase block: %u pages used, %u free, " 1235a3215902SJarkko Lavinen "erased %u times\n", 1236a3215902SJarkko Lavinen cwp, d->pages_per_eblk - cwp, cwecount); 1237a3215902SJarkko Lavinen 1238a3215902SJarkko Lavinen seq_printf(s, "total erasures: %lu\n", sum); 1239a3215902SJarkko Lavinen 12406f3c0f16SSamarth Parikh seq_puts(s, "\n"); 1241a3215902SJarkko Lavinen 1242a3215902SJarkko Lavinen seq_printf(s, "mtdswap_readsect count: %llu\n", d->sect_read_count); 1243a3215902SJarkko Lavinen seq_printf(s, "mtdswap_writesect count: %llu\n", d->sect_write_count); 1244a3215902SJarkko Lavinen seq_printf(s, "mtdswap_discard count: %llu\n", d->discard_count); 1245a3215902SJarkko Lavinen seq_printf(s, "mtd read count: %llu\n", d->mtd_read_count); 1246a3215902SJarkko Lavinen seq_printf(s, "mtd write count: %llu\n", d->mtd_write_count); 1247a3215902SJarkko Lavinen seq_printf(s, "discarded pages count: %llu\n", d->discard_page_count); 1248a3215902SJarkko Lavinen 12496f3c0f16SSamarth Parikh seq_puts(s, "\n"); 125089d8d320SDavid Woodhouse seq_printf(s, "total pages: %u\n", pages); 1251a3215902SJarkko Lavinen seq_printf(s, "pages mapped: %u\n", mapped); 1252a3215902SJarkko Lavinen 1253a3215902SJarkko Lavinen return 0; 1254a3215902SJarkko Lavinen } 1255c78f59d7SYangtao Li DEFINE_SHOW_ATTRIBUTE(mtdswap); 1256a3215902SJarkko Lavinen 1257a3215902SJarkko Lavinen static int mtdswap_add_debugfs(struct mtdswap_dev *d) 1258a3215902SJarkko Lavinen { 1259e8e3edb9SMario Rugiero struct dentry *root = d->mtd->dbg.dfs_dir; 1260a3215902SJarkko Lavinen 1261e8e3edb9SMario Rugiero if (!IS_ENABLED(CONFIG_DEBUG_FS)) 1262a3215902SJarkko Lavinen return 0; 1263a3215902SJarkko Lavinen 1264e8e3edb9SMario Rugiero if (IS_ERR_OR_NULL(root)) 1265a3215902SJarkko Lavinen return -1; 1266a3215902SJarkko Lavinen 1267*c2d73ba8SGreg Kroah-Hartman debugfs_create_file("mtdswap_stats", S_IRUSR, root, d, &mtdswap_fops); 1268a3215902SJarkko Lavinen 1269a3215902SJarkko Lavinen return 0; 1270a3215902SJarkko Lavinen } 1271a3215902SJarkko Lavinen 1272a3215902SJarkko Lavinen static int mtdswap_init(struct mtdswap_dev *d, unsigned int eblocks, 1273a3215902SJarkko Lavinen unsigned int spare_cnt) 1274a3215902SJarkko Lavinen { 1275a3215902SJarkko Lavinen struct mtd_info *mtd = d->mbd_dev->mtd; 1276a3215902SJarkko Lavinen unsigned int i, eblk_bytes, pages, blocks; 1277a3215902SJarkko Lavinen int ret = -ENOMEM; 1278a3215902SJarkko Lavinen 1279a3215902SJarkko Lavinen d->mtd = mtd; 1280a3215902SJarkko Lavinen d->eblks = eblocks; 1281a3215902SJarkko Lavinen d->spare_eblks = spare_cnt; 1282a3215902SJarkko Lavinen d->pages_per_eblk = mtd->erasesize >> PAGE_SHIFT; 1283a3215902SJarkko Lavinen 1284a3215902SJarkko Lavinen pages = d->mbd_dev->size; 1285a3215902SJarkko Lavinen blocks = eblocks * d->pages_per_eblk; 1286a3215902SJarkko Lavinen 1287a3215902SJarkko Lavinen for (i = 0; i < MTDSWAP_TREE_CNT; i++) 1288a3215902SJarkko Lavinen d->trees[i].root = RB_ROOT; 1289a3215902SJarkko Lavinen 129042bc47b3SKees Cook d->page_data = vmalloc(array_size(pages, sizeof(int))); 1291a3215902SJarkko Lavinen if (!d->page_data) 1292a3215902SJarkko Lavinen goto page_data_fail; 1293a3215902SJarkko Lavinen 129442bc47b3SKees Cook d->revmap = vmalloc(array_size(blocks, sizeof(int))); 1295a3215902SJarkko Lavinen if (!d->revmap) 1296a3215902SJarkko Lavinen goto revmap_fail; 1297a3215902SJarkko Lavinen 1298a3215902SJarkko Lavinen eblk_bytes = sizeof(struct swap_eb)*d->eblks; 12991712cde2SJoe Perches d->eb_data = vzalloc(eblk_bytes); 1300a3215902SJarkko Lavinen if (!d->eb_data) 1301a3215902SJarkko Lavinen goto eb_data_fail; 1302a3215902SJarkko Lavinen 1303a3215902SJarkko Lavinen for (i = 0; i < pages; i++) 1304a3215902SJarkko Lavinen d->page_data[i] = BLOCK_UNDEF; 1305a3215902SJarkko Lavinen 1306a3215902SJarkko Lavinen for (i = 0; i < blocks; i++) 1307a3215902SJarkko Lavinen d->revmap[i] = PAGE_UNDEF; 1308a3215902SJarkko Lavinen 1309a3215902SJarkko Lavinen d->page_buf = kmalloc(PAGE_SIZE, GFP_KERNEL); 1310a3215902SJarkko Lavinen if (!d->page_buf) 1311a3215902SJarkko Lavinen goto page_buf_fail; 1312a3215902SJarkko Lavinen 13136da2ec56SKees Cook d->oob_buf = kmalloc_array(2, mtd->oobavail, GFP_KERNEL); 1314a3215902SJarkko Lavinen if (!d->oob_buf) 1315a3215902SJarkko Lavinen goto oob_buf_fail; 1316a3215902SJarkko Lavinen 1317a3215902SJarkko Lavinen mtdswap_scan_eblks(d); 1318a3215902SJarkko Lavinen 1319a3215902SJarkko Lavinen return 0; 1320a3215902SJarkko Lavinen 1321a3215902SJarkko Lavinen oob_buf_fail: 1322a3215902SJarkko Lavinen kfree(d->page_buf); 1323a3215902SJarkko Lavinen page_buf_fail: 1324a3215902SJarkko Lavinen vfree(d->eb_data); 1325a3215902SJarkko Lavinen eb_data_fail: 1326a3215902SJarkko Lavinen vfree(d->revmap); 1327a3215902SJarkko Lavinen revmap_fail: 1328a3215902SJarkko Lavinen vfree(d->page_data); 1329a3215902SJarkko Lavinen page_data_fail: 1330a3215902SJarkko Lavinen printk(KERN_ERR "%s: init failed (%d)\n", MTDSWAP_PREFIX, ret); 1331a3215902SJarkko Lavinen return ret; 1332a3215902SJarkko Lavinen } 1333a3215902SJarkko Lavinen 1334a3215902SJarkko Lavinen static void mtdswap_add_mtd(struct mtd_blktrans_ops *tr, struct mtd_info *mtd) 1335a3215902SJarkko Lavinen { 1336a3215902SJarkko Lavinen struct mtdswap_dev *d; 1337a3215902SJarkko Lavinen struct mtd_blktrans_dev *mbd_dev; 1338a3215902SJarkko Lavinen char *parts; 1339a3215902SJarkko Lavinen char *this_opt; 1340a3215902SJarkko Lavinen unsigned long part; 1341a3215902SJarkko Lavinen unsigned int eblocks, eavailable, bad_blocks, spare_cnt; 1342a3215902SJarkko Lavinen uint64_t swap_size, use_size, size_limit; 1343a3215902SJarkko Lavinen int ret; 1344a3215902SJarkko Lavinen 1345a3215902SJarkko Lavinen parts = &partitions[0]; 1346a3215902SJarkko Lavinen if (!*parts) 1347a3215902SJarkko Lavinen return; 1348a3215902SJarkko Lavinen 1349a3215902SJarkko Lavinen while ((this_opt = strsep(&parts, ",")) != NULL) { 13503156231fSJingoo Han if (kstrtoul(this_opt, 0, &part) < 0) 1351a3215902SJarkko Lavinen return; 1352a3215902SJarkko Lavinen 1353a3215902SJarkko Lavinen if (mtd->index == part) 1354a3215902SJarkko Lavinen break; 1355a3215902SJarkko Lavinen } 1356a3215902SJarkko Lavinen 1357a3215902SJarkko Lavinen if (mtd->index != part) 1358a3215902SJarkko Lavinen return; 1359a3215902SJarkko Lavinen 1360a3215902SJarkko Lavinen if (mtd->erasesize < PAGE_SIZE || mtd->erasesize % PAGE_SIZE) { 1361a3215902SJarkko Lavinen printk(KERN_ERR "%s: Erase size %u not multiple of PAGE_SIZE " 1362a3215902SJarkko Lavinen "%lu\n", MTDSWAP_PREFIX, mtd->erasesize, PAGE_SIZE); 1363a3215902SJarkko Lavinen return; 1364a3215902SJarkko Lavinen } 1365a3215902SJarkko Lavinen 1366a3215902SJarkko Lavinen if (PAGE_SIZE % mtd->writesize || mtd->writesize > PAGE_SIZE) { 1367a3215902SJarkko Lavinen printk(KERN_ERR "%s: PAGE_SIZE %lu not multiple of write size" 1368a3215902SJarkko Lavinen " %u\n", MTDSWAP_PREFIX, PAGE_SIZE, mtd->writesize); 1369a3215902SJarkko Lavinen return; 1370a3215902SJarkko Lavinen } 1371a3215902SJarkko Lavinen 1372f5b8aa78SBoris BREZILLON if (!mtd->oobsize || mtd->oobavail < MTDSWAP_OOBSIZE) { 1373a3215902SJarkko Lavinen printk(KERN_ERR "%s: Not enough free bytes in OOB, " 13742130ad32SRandy Dunlap "%d available, %zu needed.\n", 1375f5b8aa78SBoris BREZILLON MTDSWAP_PREFIX, mtd->oobavail, MTDSWAP_OOBSIZE); 1376a3215902SJarkko Lavinen return; 1377a3215902SJarkko Lavinen } 1378a3215902SJarkko Lavinen 1379a3215902SJarkko Lavinen if (spare_eblocks > 100) 1380a3215902SJarkko Lavinen spare_eblocks = 100; 1381a3215902SJarkko Lavinen 1382a3215902SJarkko Lavinen use_size = mtd->size; 1383a3215902SJarkko Lavinen size_limit = (uint64_t) BLOCK_MAX * PAGE_SIZE; 1384a3215902SJarkko Lavinen 1385a3215902SJarkko Lavinen if (mtd->size > size_limit) { 1386a3215902SJarkko Lavinen printk(KERN_WARNING "%s: Device too large. Limiting size to " 1387a3215902SJarkko Lavinen "%llu bytes\n", MTDSWAP_PREFIX, size_limit); 1388a3215902SJarkko Lavinen use_size = size_limit; 1389a3215902SJarkko Lavinen } 1390a3215902SJarkko Lavinen 1391a3215902SJarkko Lavinen eblocks = mtd_div_by_eb(use_size, mtd); 13928c3f3f1dSBrian Norris use_size = (uint64_t)eblocks * mtd->erasesize; 1393a3215902SJarkko Lavinen bad_blocks = mtdswap_badblocks(mtd, use_size); 1394a3215902SJarkko Lavinen eavailable = eblocks - bad_blocks; 1395a3215902SJarkko Lavinen 1396a3215902SJarkko Lavinen if (eavailable < MIN_ERASE_BLOCKS) { 1397a3215902SJarkko Lavinen printk(KERN_ERR "%s: Not enough erase blocks. %u available, " 1398a3215902SJarkko Lavinen "%d needed\n", MTDSWAP_PREFIX, eavailable, 1399a3215902SJarkko Lavinen MIN_ERASE_BLOCKS); 1400a3215902SJarkko Lavinen return; 1401a3215902SJarkko Lavinen } 1402a3215902SJarkko Lavinen 1403a3215902SJarkko Lavinen spare_cnt = div_u64((uint64_t)eavailable * spare_eblocks, 100); 1404a3215902SJarkko Lavinen 1405a3215902SJarkko Lavinen if (spare_cnt < MIN_SPARE_EBLOCKS) 1406a3215902SJarkko Lavinen spare_cnt = MIN_SPARE_EBLOCKS; 1407a3215902SJarkko Lavinen 1408a3215902SJarkko Lavinen if (spare_cnt > eavailable - 1) 1409a3215902SJarkko Lavinen spare_cnt = eavailable - 1; 1410a3215902SJarkko Lavinen 1411a3215902SJarkko Lavinen swap_size = (uint64_t)(eavailable - spare_cnt) * mtd->erasesize + 1412a3215902SJarkko Lavinen (header ? PAGE_SIZE : 0); 1413a3215902SJarkko Lavinen 1414a3215902SJarkko Lavinen printk(KERN_INFO "%s: Enabling MTD swap on device %lu, size %llu KB, " 1415a3215902SJarkko Lavinen "%u spare, %u bad blocks\n", 1416a3215902SJarkko Lavinen MTDSWAP_PREFIX, part, swap_size / 1024, spare_cnt, bad_blocks); 1417a3215902SJarkko Lavinen 1418a3215902SJarkko Lavinen d = kzalloc(sizeof(struct mtdswap_dev), GFP_KERNEL); 1419a3215902SJarkko Lavinen if (!d) 1420a3215902SJarkko Lavinen return; 1421a3215902SJarkko Lavinen 1422a3215902SJarkko Lavinen mbd_dev = kzalloc(sizeof(struct mtd_blktrans_dev), GFP_KERNEL); 1423a3215902SJarkko Lavinen if (!mbd_dev) { 1424a3215902SJarkko Lavinen kfree(d); 1425a3215902SJarkko Lavinen return; 1426a3215902SJarkko Lavinen } 1427a3215902SJarkko Lavinen 1428a3215902SJarkko Lavinen d->mbd_dev = mbd_dev; 1429a3215902SJarkko Lavinen mbd_dev->priv = d; 1430a3215902SJarkko Lavinen 1431a3215902SJarkko Lavinen mbd_dev->mtd = mtd; 1432a3215902SJarkko Lavinen mbd_dev->devnum = mtd->index; 1433a3215902SJarkko Lavinen mbd_dev->size = swap_size >> PAGE_SHIFT; 1434a3215902SJarkko Lavinen mbd_dev->tr = tr; 1435a3215902SJarkko Lavinen 1436a3215902SJarkko Lavinen if (!(mtd->flags & MTD_WRITEABLE)) 1437a3215902SJarkko Lavinen mbd_dev->readonly = 1; 1438a3215902SJarkko Lavinen 1439a3215902SJarkko Lavinen if (mtdswap_init(d, eblocks, spare_cnt) < 0) 1440a3215902SJarkko Lavinen goto init_failed; 1441a3215902SJarkko Lavinen 1442a3215902SJarkko Lavinen if (add_mtd_blktrans_dev(mbd_dev) < 0) 1443a3215902SJarkko Lavinen goto cleanup; 1444a3215902SJarkko Lavinen 1445a3215902SJarkko Lavinen d->dev = disk_to_dev(mbd_dev->disk); 1446a3215902SJarkko Lavinen 1447a3215902SJarkko Lavinen ret = mtdswap_add_debugfs(d); 1448a3215902SJarkko Lavinen if (ret < 0) 1449a3215902SJarkko Lavinen goto debugfs_failed; 1450a3215902SJarkko Lavinen 1451a3215902SJarkko Lavinen return; 1452a3215902SJarkko Lavinen 1453a3215902SJarkko Lavinen debugfs_failed: 1454a3215902SJarkko Lavinen del_mtd_blktrans_dev(mbd_dev); 1455a3215902SJarkko Lavinen 1456a3215902SJarkko Lavinen cleanup: 1457a3215902SJarkko Lavinen mtdswap_cleanup(d); 1458a3215902SJarkko Lavinen 1459a3215902SJarkko Lavinen init_failed: 1460a3215902SJarkko Lavinen kfree(mbd_dev); 1461a3215902SJarkko Lavinen kfree(d); 1462a3215902SJarkko Lavinen } 1463a3215902SJarkko Lavinen 1464a3215902SJarkko Lavinen static void mtdswap_remove_dev(struct mtd_blktrans_dev *dev) 1465a3215902SJarkko Lavinen { 1466a3215902SJarkko Lavinen struct mtdswap_dev *d = MTDSWAP_MBD_TO_MTDSWAP(dev); 1467a3215902SJarkko Lavinen 1468a3215902SJarkko Lavinen del_mtd_blktrans_dev(dev); 1469a3215902SJarkko Lavinen mtdswap_cleanup(d); 1470a3215902SJarkko Lavinen kfree(d); 1471a3215902SJarkko Lavinen } 1472a3215902SJarkko Lavinen 1473a3215902SJarkko Lavinen static struct mtd_blktrans_ops mtdswap_ops = { 1474a3215902SJarkko Lavinen .name = "mtdswap", 1475a3215902SJarkko Lavinen .major = 0, 1476a3215902SJarkko Lavinen .part_bits = 0, 1477a3215902SJarkko Lavinen .blksize = PAGE_SIZE, 1478a3215902SJarkko Lavinen .flush = mtdswap_flush, 1479a3215902SJarkko Lavinen .readsect = mtdswap_readsect, 1480a3215902SJarkko Lavinen .writesect = mtdswap_writesect, 1481a3215902SJarkko Lavinen .discard = mtdswap_discard, 1482a3215902SJarkko Lavinen .background = mtdswap_background, 1483a3215902SJarkko Lavinen .add_mtd = mtdswap_add_mtd, 1484a3215902SJarkko Lavinen .remove_dev = mtdswap_remove_dev, 1485a3215902SJarkko Lavinen .owner = THIS_MODULE, 1486a3215902SJarkko Lavinen }; 1487a3215902SJarkko Lavinen 1488a3215902SJarkko Lavinen static int __init mtdswap_modinit(void) 1489a3215902SJarkko Lavinen { 1490a3215902SJarkko Lavinen return register_mtd_blktrans(&mtdswap_ops); 1491a3215902SJarkko Lavinen } 1492a3215902SJarkko Lavinen 1493a3215902SJarkko Lavinen static void __exit mtdswap_modexit(void) 1494a3215902SJarkko Lavinen { 1495a3215902SJarkko Lavinen deregister_mtd_blktrans(&mtdswap_ops); 1496a3215902SJarkko Lavinen } 1497a3215902SJarkko Lavinen 1498a3215902SJarkko Lavinen module_init(mtdswap_modinit); 1499a3215902SJarkko Lavinen module_exit(mtdswap_modexit); 1500a3215902SJarkko Lavinen 1501a3215902SJarkko Lavinen 1502a3215902SJarkko Lavinen MODULE_LICENSE("GPL"); 1503a3215902SJarkko Lavinen MODULE_AUTHOR("Jarkko Lavinen <jarkko.lavinen@nokia.com>"); 1504a3215902SJarkko Lavinen MODULE_DESCRIPTION("Block device access to an MTD suitable for using as " 1505a3215902SJarkko Lavinen "swap space"); 1506