drivers/block/zram/zram_drv.c

Source file repositories/reference/linux-study-clean/drivers/block/zram/zram_drv.c

File Facts

System
Linux kernel
Corpus path
drivers/block/zram/zram_drv.c
Extension
.c
Size
78168 bytes
Lines
3302
Domain
Driver Families
Bucket
drivers/block
Inferred role
Driver Families: operation-table or driver-model contract
Status
pattern 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.

Dependency Surface

Detected Declarations

Annotated Snippet

static const struct file_operations proc_zram_block_state_op = {
	.open = simple_open,
	.read = read_block_state,
	.llseek = default_llseek,
};

static void zram_debugfs_register(struct zram *zram)
{
	if (!zram_debugfs_root)
		return;

	zram->debugfs_dir = debugfs_create_dir(zram->disk->disk_name,
						zram_debugfs_root);
	debugfs_create_file("block_state", 0400, zram->debugfs_dir,
				zram, &proc_zram_block_state_op);
}

static void zram_debugfs_unregister(struct zram *zram)
{
	debugfs_remove_recursive(zram->debugfs_dir);
}
#else
static void zram_debugfs_create(void) {};
static void zram_debugfs_destroy(void) {};
static void zram_debugfs_register(struct zram *zram) {};
static void zram_debugfs_unregister(struct zram *zram) {};
#endif

/* Only algo parameter given, lookup by algo name */
static int lookup_algo_priority(struct zram *zram, const char *algo,
				u32 min_prio)
{
	s32 prio;

	for (prio = min_prio; prio < ZRAM_MAX_COMPS; prio++) {
		if (!zram->comp_algs[prio])
			continue;

		if (!strcmp(zram->comp_algs[prio], algo))
			return prio;
	}

	return -EINVAL;
}

/* Both algo and priority parameters given, validate them */
static int validate_algo_priority(struct zram *zram, const char *algo, u32 prio)
{
	if (prio >= ZRAM_MAX_COMPS)
		return -EINVAL;
	/* No algo at given priority */
	if (!zram->comp_algs[prio])
		return -EINVAL;
	/* A different algo at given priority */
	if (strcmp(zram->comp_algs[prio], algo))
		return -EINVAL;
	return 0;
}

static void comp_algorithm_set(struct zram *zram, u32 prio, const char *alg)
{
	zram->comp_algs[prio] = alg;
}

static int __comp_algorithm_store(struct zram *zram, u32 prio, const char *buf)
{
	const char *alg;
	size_t sz;

	sz = strlen(buf);
	if (sz >= ZRAM_MAX_ALGO_NAME_SZ)
		return -E2BIG;

	alg = zcomp_lookup_backend_name(buf);
	if (!alg)
		return -EINVAL;

	guard(rwsem_write)(&zram->dev_lock);
	if (init_done(zram)) {
		pr_info("Can't change algorithm for initialized device\n");
		return -EBUSY;
	}

	comp_algorithm_set(zram, prio, alg);
	return 0;
}

static void comp_params_reset(struct zram *zram, u32 prio)
{
	struct zcomp_params *params = &zram->params[prio];

Annotation

Implementation Notes