zcomp_lzo.c 1.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
/*
 * Copyright (C) 2014 Sergey Senozhatsky.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version
 * 2 of the License, or (at your option) any later version.
 */

#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/lzo.h>
13 14
#include <linux/vmalloc.h>
#include <linux/mm.h>
15 16 17

#include "zcomp_lzo.h"

18
static void *lzo_create(gfp_t flags)
19
{
20 21
	void *ret;

22
	ret = kzalloc(LZO1X_MEM_COMPRESS, flags);
23 24
	if (!ret)
		ret = __vmalloc(LZO1X_MEM_COMPRESS,
25
				flags | __GFP_ZERO | __GFP_HIGHMEM,
26 27
				PAGE_KERNEL);
	return ret;
28 29 30 31
}

static void lzo_destroy(void *private)
{
32
	kvfree(private);
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
}

static int lzo_compress(const unsigned char *src, unsigned char *dst,
		size_t *dst_len, void *private)
{
	int ret = lzo1x_1_compress(src, PAGE_SIZE, dst, dst_len, private);
	return ret == LZO_E_OK ? 0 : ret;
}

static int lzo_decompress(const unsigned char *src, size_t src_len,
		unsigned char *dst)
{
	size_t dst_len = PAGE_SIZE;
	int ret = lzo1x_decompress_safe(src, src_len, dst, &dst_len);
	return ret == LZO_E_OK ? 0 : ret;
}

struct zcomp_backend zcomp_lzo = {
	.compress = lzo_compress,
	.decompress = lzo_decompress,
	.create = lzo_create,
	.destroy = lzo_destroy,
	.name = "lzo",
};