drivers/mtd/mtdswap.c
Source file repositories/reference/linux-study-clean/drivers/mtd/mtdswap.c
File Facts
- System
- Linux kernel
- Corpus path
drivers/mtd/mtdswap.c- Extension
.c- Size
- 33776 bytes
- Lines
- 1492
- Domain
- Driver Families
- Bucket
- drivers/mtd
- Inferred role
- Driver Families: implementation source
- Status
- source implementation candidate
Why This File Exists
Repeatable hardware-adapter layer. Deep compatibility for every driver is out of scope; this atlas records patterns, probe lifecycles, bus glue, IRQ/DMA usage, and links back to core abstractions.
- Repeatable hardware-adapter layer. Deep compatibility for every driver is out of scope; this atlas records patterns, probe lifecycles, bus glue, IRQ/DMA usage, and links back to core abstractions.
- Uses kernel synchronization; read lock ordering, sleepability, and interrupt context assumptions before translating.
- Allocates kernel memory; connect allocation flags and lifetime to context constraints.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
linux/kernel.hlinux/module.hlinux/mtd/mtd.hlinux/mtd/blktrans.hlinux/rbtree.hlinux/sched.hlinux/slab.hlinux/vmalloc.hlinux/blkdev.hlinux/swap.hlinux/debugfs.hlinux/seq_file.hlinux/device.hlinux/math64.h
Detected Declarations
struct swap_ebstruct mtdswap_treestruct mtdswap_devstruct mtdswap_oobdatafunction mtdswap_eb_offsetfunction mtdswap_eb_detachfunction __mtdswap_rb_addfunction mtdswap_rb_addfunction mtdswap_handle_badblockfunction mtdswap_handle_write_errorfunction mtdswap_read_oobfunction mtdswap_read_markersfunction mtdswap_write_markerfunction mtdswap_check_countsfunction mtdswap_scan_eblksfunction mtdswap_store_ebfunction mtdswap_erase_blockfunction mtdswap_map_free_blockfunction mtdswap_free_page_cntfunction mtdswap_enough_free_pagesfunction mtdswap_write_blockfunction mtdswap_move_blockfunction mtdswap_gc_eblockfunction __mtdswap_choose_gc_treefunction mtdswap_wlfreqfunction mtdswap_choose_wl_treefunction mtdswap_choose_gc_treefunction mtdswap_test_pattfunction mtdswap_eblk_passesfunction mtdswap_gcfunction mtdswap_backgroundfunction mtdswap_cleanupfunction mtdswap_flushfunction mtdswap_badblocksfunction mtdswap_writesectfunction mtdswap_auto_headerfunction mtdswap_readsectfunction mtdswap_discardfunction mtdswap_showfunction mtdswap_add_debugfsfunction mtdswap_initfunction mtdswap_add_mtdfunction mtdswap_remove_dev
Annotated Snippet
struct swap_eb {
struct rb_node rb;
struct rb_root *root;
unsigned int flags;
unsigned int active_count;
unsigned int erase_count;
unsigned int pad; /* speeds up pointer decrement */
};
#define MTDSWAP_ECNT_MIN(rbroot) (rb_entry(rb_first(rbroot), struct swap_eb, \
rb)->erase_count)
#define MTDSWAP_ECNT_MAX(rbroot) (rb_entry(rb_last(rbroot), struct swap_eb, \
rb)->erase_count)
struct mtdswap_tree {
struct rb_root root;
unsigned int count;
};
enum {
MTDSWAP_CLEAN,
MTDSWAP_USED,
MTDSWAP_LOWFRAG,
MTDSWAP_HIFRAG,
MTDSWAP_DIRTY,
MTDSWAP_BITFLIP,
MTDSWAP_FAILING,
MTDSWAP_TREE_CNT,
};
struct mtdswap_dev {
struct mtd_blktrans_dev *mbd_dev;
struct mtd_info *mtd;
struct device *dev;
unsigned int *page_data;
unsigned int *revmap;
unsigned int eblks;
unsigned int spare_eblks;
unsigned int pages_per_eblk;
unsigned int max_erase_count;
struct swap_eb *eb_data;
struct mtdswap_tree trees[MTDSWAP_TREE_CNT];
unsigned long long sect_read_count;
unsigned long long sect_write_count;
unsigned long long mtd_write_count;
unsigned long long mtd_read_count;
unsigned long long discard_count;
unsigned long long discard_page_count;
unsigned int curr_write_pos;
struct swap_eb *curr_write;
char *page_buf;
char *oob_buf;
};
struct mtdswap_oobdata {
__le16 magic;
__le32 count;
} __packed;
#define MTDSWAP_MAGIC_CLEAN 0x2095
#define MTDSWAP_MAGIC_DIRTY (MTDSWAP_MAGIC_CLEAN + 1)
#define MTDSWAP_TYPE_CLEAN 0
#define MTDSWAP_TYPE_DIRTY 1
#define MTDSWAP_OOBSIZE sizeof(struct mtdswap_oobdata)
#define MTDSWAP_ERASE_RETRIES 3 /* Before marking erase block bad */
#define MTDSWAP_IO_RETRIES 3
enum {
MTDSWAP_SCANNED_CLEAN,
MTDSWAP_SCANNED_DIRTY,
MTDSWAP_SCANNED_BITFLIP,
MTDSWAP_SCANNED_BAD,
};
/*
* In the worst case mtdswap_writesect() has allocated the last clean
* page from the current block and is then pre-empted by the GC
* thread. The thread can consume a full erase block when moving a
* block.
*/
#define MIN_SPARE_EBLOCKS 2
#define MIN_ERASE_BLOCKS (MIN_SPARE_EBLOCKS + 1)
Annotation
- Immediate include surface: `linux/kernel.h`, `linux/module.h`, `linux/mtd/mtd.h`, `linux/mtd/blktrans.h`, `linux/rbtree.h`, `linux/sched.h`, `linux/slab.h`, `linux/vmalloc.h`.
- Detected declarations: `struct swap_eb`, `struct mtdswap_tree`, `struct mtdswap_dev`, `struct mtdswap_oobdata`, `function mtdswap_eb_offset`, `function mtdswap_eb_detach`, `function __mtdswap_rb_add`, `function mtdswap_rb_add`, `function mtdswap_handle_badblock`, `function mtdswap_handle_write_error`.
- Atlas domain: Driver Families / drivers/mtd.
- Implementation status: source implementation candidate.
- Synchronization appears in or near this file; preserve lock ordering, sleepability, and interrupt-context constraints.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.