drivers/block/zram/backend_deflate.c

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

File Facts

System
Linux kernel
Corpus path
drivers/block/zram/backend_deflate.c
Extension
.c
Size
3246 bytes
Lines
149
Domain
Driver Families
Bucket
drivers/block
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.

Dependency Surface

Detected Declarations

Annotated Snippet

struct deflate_ctx {
	struct z_stream_s cctx;
	struct z_stream_s dctx;
};

static void deflate_release_params(struct zcomp_params *params)
{
}

static int deflate_setup_params(struct zcomp_params *params)
{
	if (params->level == ZCOMP_PARAM_NOT_SET)
		params->level = Z_DEFAULT_COMPRESSION;
	if (params->deflate.winbits == ZCOMP_PARAM_NOT_SET)
		params->deflate.winbits = DEFLATE_DEF_WINBITS;

	return 0;
}

static void deflate_destroy(struct zcomp_ctx *ctx)
{
	struct deflate_ctx *zctx = ctx->context;

	if (!zctx)
		return;

	if (zctx->cctx.workspace) {
		zlib_deflateEnd(&zctx->cctx);
		vfree(zctx->cctx.workspace);
	}
	if (zctx->dctx.workspace) {
		zlib_inflateEnd(&zctx->dctx);
		vfree(zctx->dctx.workspace);
	}
	kfree(zctx);
}

static int deflate_create(struct zcomp_params *params, struct zcomp_ctx *ctx)
{
	struct deflate_ctx *zctx;
	size_t sz;
	int ret;

	zctx = kzalloc_obj(*zctx);
	if (!zctx)
		return -ENOMEM;

	ctx->context = zctx;
	sz = zlib_deflate_workspacesize(params->deflate.winbits, MAX_MEM_LEVEL);
	zctx->cctx.workspace = vzalloc(sz);
	if (!zctx->cctx.workspace)
		goto error;

	ret = zlib_deflateInit2(&zctx->cctx, params->level, Z_DEFLATED,
				params->deflate.winbits, DEFLATE_DEF_MEMLEVEL,
				Z_DEFAULT_STRATEGY);
	if (ret != Z_OK)
		goto error;

	sz = zlib_inflate_workspacesize();
	zctx->dctx.workspace = vzalloc(sz);
	if (!zctx->dctx.workspace)
		goto error;

	ret = zlib_inflateInit2(&zctx->dctx, params->deflate.winbits);
	if (ret != Z_OK)
		goto error;

	return 0;

error:
	deflate_destroy(ctx);
	return -EINVAL;
}

static int deflate_compress(struct zcomp_params *params, struct zcomp_ctx *ctx,
			    struct zcomp_req *req)
{
	struct deflate_ctx *zctx = ctx->context;
	struct z_stream_s *deflate;
	int ret;

	deflate = &zctx->cctx;
	ret = zlib_deflateReset(deflate);
	if (ret != Z_OK)
		return -EINVAL;

	deflate->next_in = (u8 *)req->src;
	deflate->avail_in = req->src_len;
	deflate->next_out = (u8 *)req->dst;

Annotation

Implementation Notes