825 lines
21 KiB
C
825 lines
21 KiB
C
/*
|
|
* Copyright (c) 2019 Tom Marshall <tdm.code@gmail.com>
|
|
*
|
|
* 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.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* along with this program; if not, write to the Free Software
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
|
* 02110-1301, USA.
|
|
*/
|
|
|
|
#include <linux/module.h>
|
|
#include <linux/kernel.h>
|
|
#include <linux/init.h>
|
|
#include <linux/bio.h>
|
|
#include <linux/device-mapper.h>
|
|
#include <linux/workqueue.h>
|
|
#include <linux/mutex.h>
|
|
|
|
#include <linux/lz4.h>
|
|
#include <linux/zlib.h>
|
|
|
|
#include <linux/dm-compress.h>
|
|
|
|
struct lbd {
|
|
struct list_head lru_list;
|
|
struct list_head flush_list;
|
|
unsigned long flush_jiffies;
|
|
u64 lblk;
|
|
struct mutex reflock;
|
|
unsigned int ref;
|
|
|
|
struct mutex lock;
|
|
struct cbd_params* params;
|
|
struct lbatviewcache* lvc;
|
|
struct lbatview* lv;
|
|
void* percpu;
|
|
struct page** pagev;
|
|
u8* buf;
|
|
u32 c_len;
|
|
};
|
|
|
|
static inline bool
|
|
lblk_is_zeros(struct cbd_params* params, struct lbd* lbd)
|
|
{
|
|
#ifdef CBD_DETECT_ZERO_BLOCKS
|
|
u32 off;
|
|
u32 len = PBLK_SIZE * lblk_per_pblk(params);
|
|
|
|
for (off = 0; off < len; ++off) {
|
|
if (lbd->lblk_buf[off]) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
return true;
|
|
#else
|
|
return false;
|
|
#endif
|
|
}
|
|
|
|
struct lblk_compress_state {
|
|
u8* buf;
|
|
#ifdef COMPRESS_HAVE_LZ4
|
|
u8* lz4_workmem;
|
|
#endif
|
|
#ifdef COMPRESS_HAVE_ZLIB
|
|
z_stream zlib_cstream;
|
|
z_stream zlib_dstream;
|
|
#endif
|
|
};
|
|
|
|
static struct lblk_compress_state*
|
|
lblk_get_compress_state(void* percpu, int cpu)
|
|
{
|
|
struct lblk_compress_state** statep;
|
|
|
|
statep = per_cpu_ptr(percpu, cpu);
|
|
return *statep;
|
|
}
|
|
|
|
#ifdef COMPRESS_HAVE_LZ4
|
|
static size_t
|
|
lblk_compress_lz4(struct lbd* lbd)
|
|
{
|
|
int clen;
|
|
int cpu;
|
|
struct lblk_compress_state* state;
|
|
|
|
cpu = get_cpu();
|
|
state = lblk_get_compress_state(lbd->percpu, cpu);
|
|
BUG_ON(state == NULL);
|
|
clen = LZ4_compress_fast(lbd->buf, state->buf,
|
|
PBLK_SIZE * lblk_per_pblk(lbd->params),
|
|
PBLK_SIZE * (lblk_per_pblk(lbd->params) - 1),
|
|
lbd->params->compression, state->lz4_workmem);
|
|
if (clen <= 0) {
|
|
put_cpu();
|
|
return 0;
|
|
}
|
|
memcpy(lbd->buf, state->buf, clen);
|
|
put_cpu();
|
|
|
|
return (size_t)clen;
|
|
}
|
|
|
|
static bool
|
|
lblk_decompress_lz4(struct lbd* lbd)
|
|
{
|
|
int ret;
|
|
int cpu;
|
|
struct lblk_compress_state* state;
|
|
u32 dlen = PBLK_SIZE * lblk_per_pblk(lbd->params);
|
|
|
|
cpu = get_cpu();
|
|
state = lblk_get_compress_state(lbd->percpu, cpu);
|
|
BUG_ON(state == NULL);
|
|
ret = LZ4_decompress_safe(lbd->buf,
|
|
state->buf,
|
|
lbd->c_len,
|
|
dlen);
|
|
if (ret != dlen) {
|
|
put_cpu();
|
|
return false;
|
|
}
|
|
memcpy(lbd->buf, state->buf, dlen);
|
|
put_cpu();
|
|
|
|
return true;
|
|
}
|
|
#endif
|
|
|
|
#ifdef COMPRESS_HAVE_ZLIB
|
|
static size_t
|
|
lblk_compress_zlib(struct lbd* lbd)
|
|
{
|
|
int ret;
|
|
int clen;
|
|
int cpu;
|
|
struct lblk_compress_state* state;
|
|
z_stream* stream;
|
|
|
|
cpu = get_cpu();
|
|
state = lblk_get_compress_state(lbd->percpu, cpu);
|
|
BUG_ON(state == NULL);
|
|
stream = &state->zlib_cstream;
|
|
ret = zlib_deflateReset(stream);
|
|
BUG_ON(ret != Z_OK);
|
|
stream->next_in = lbd->buf;
|
|
stream->avail_in = PBLK_SIZE * lblk_per_pblk(lbd->params);
|
|
stream->next_out = state->buf;
|
|
stream->avail_out = PBLK_SIZE * (lblk_per_pblk(lbd->params) - 1);
|
|
ret = zlib_deflate(stream, Z_FINISH);
|
|
if (ret != Z_STREAM_END) {
|
|
put_cpu();
|
|
return 0;
|
|
}
|
|
clen = stream->total_out;
|
|
memcpy(lbd->buf, state->buf, clen);
|
|
put_cpu();
|
|
|
|
return (size_t)clen;
|
|
}
|
|
|
|
static bool
|
|
lblk_decompress_zlib(struct lbd* lbd)
|
|
{
|
|
int ret;
|
|
int cpu;
|
|
struct lblk_compress_state* state;
|
|
z_stream* stream;
|
|
u32 dlen = PBLK_SIZE * lblk_per_pblk(lbd->params);
|
|
|
|
cpu = get_cpu();
|
|
state = lblk_get_compress_state(lbd->percpu, cpu);
|
|
BUG_ON(state == NULL);
|
|
stream = &state->zlib_dstream;
|
|
ret = zlib_inflateReset(stream);
|
|
BUG_ON(ret != Z_OK);
|
|
stream->next_in = lbd->buf;
|
|
stream->avail_in = lbd->c_len;
|
|
stream->next_out = state->buf;
|
|
stream->avail_out = dlen;
|
|
ret = zlib_inflate(stream, Z_SYNC_FLUSH);
|
|
/* See xxx */
|
|
if (ret == Z_OK && !stream->avail_in && stream->avail_out) {
|
|
u8 zerostuff = 0;
|
|
stream->next_in = &zerostuff;
|
|
stream->avail_in = 1;
|
|
ret = zlib_inflate(stream, Z_FINISH);
|
|
}
|
|
if (ret != Z_STREAM_END || stream->total_out != dlen) {
|
|
put_cpu();
|
|
return false;
|
|
}
|
|
memcpy(lbd->buf, state->buf, dlen);
|
|
put_cpu();
|
|
|
|
return true;
|
|
}
|
|
#endif
|
|
|
|
/*
|
|
* Compress dc->lblk into dc->lz4_cbuf
|
|
*
|
|
* Returns number of bytes in cbuf or 0 for failure.
|
|
*/
|
|
static size_t
|
|
lblk_compress(struct lbd* lbd)
|
|
{
|
|
#ifdef COMPRESS_HAVE_LZ4
|
|
if (lbd->params->algorithm == CBD_ALG_LZ4) {
|
|
return lblk_compress_lz4(lbd);
|
|
}
|
|
#endif
|
|
#ifdef COMPRESS_HAVE_ZLIB
|
|
if (lbd->params->algorithm == CBD_ALG_ZLIB) {
|
|
return lblk_compress_zlib(lbd);
|
|
}
|
|
#endif
|
|
return 0;
|
|
}
|
|
|
|
/*
|
|
* Decompress dc->lz4_cbuf of size clen into dc->lblk
|
|
*/
|
|
static bool
|
|
lblk_decompress(struct lbd* lbd)
|
|
{
|
|
#ifdef COMPRESS_HAVE_LZ4
|
|
if (lbd->params->algorithm == CBD_ALG_LZ4) {
|
|
return lblk_decompress_lz4(lbd);
|
|
}
|
|
#endif
|
|
#ifdef COMPRESS_HAVE_ZLIB
|
|
if (lbd->params->algorithm == CBD_ALG_ZLIB) {
|
|
return lblk_decompress_zlib(lbd);
|
|
}
|
|
#endif
|
|
return false;
|
|
}
|
|
|
|
static bool
|
|
lbd_ctr(struct lbd* lbd,
|
|
struct cbd_params* params,
|
|
struct lbatviewcache* lvc,
|
|
void* percpu)
|
|
{
|
|
u32 nr_pages = lblk_per_pblk(params);
|
|
|
|
memset(lbd, 0, sizeof(struct lbd));
|
|
INIT_LIST_HEAD(&lbd->lru_list);
|
|
INIT_LIST_HEAD(&lbd->flush_list);
|
|
lbd->flush_jiffies = 0;
|
|
lbd->lblk = LBLK_NONE;
|
|
mutex_init(&lbd->reflock);
|
|
lbd->ref = 0;
|
|
mutex_init(&lbd->lock);
|
|
lbd->params = params;
|
|
lbd->lvc = lvc;
|
|
lbd->lv = NULL;
|
|
lbd->percpu = percpu;
|
|
lbd->pagev = kzalloc(nr_pages * sizeof(struct page*), GFP_KERNEL);
|
|
if (!lbd->pagev) {
|
|
return false;
|
|
}
|
|
if (!cbd_alloc_pagev(lbd->pagev, nr_pages)) {
|
|
return false;
|
|
}
|
|
lbd->buf = vmap(lbd->pagev, nr_pages, VM_MAP, PAGE_KERNEL);
|
|
if (!lbd->buf) {
|
|
return false;
|
|
}
|
|
lbd->c_len = 0;
|
|
|
|
return true;
|
|
}
|
|
|
|
static void
|
|
lbd_dtr(struct lbd* lbd)
|
|
{
|
|
u32 nr_pages = lblk_per_pblk(lbd->params);
|
|
|
|
if (lbatviewcache_put(lbd->lvc, lbd->lv) != 0) {
|
|
printk(KERN_ERR "%s: lbatviewcache_put failed\n", __func__);
|
|
}
|
|
lbd->c_len = 0;
|
|
vunmap(lbd->buf);
|
|
lbd->buf = NULL;
|
|
cbd_free_pagev(lbd->pagev, nr_pages);
|
|
kfree(lbd->pagev);
|
|
lbd->pagev = NULL;
|
|
lbd->percpu = NULL;
|
|
lbd->lv = NULL;
|
|
lbd->lvc = NULL;
|
|
}
|
|
|
|
static bool
|
|
lbd_error(struct lbd* lbd)
|
|
{
|
|
return PageError(lbd->pagev[0]);
|
|
}
|
|
|
|
static int
|
|
lbd_flush(struct lbd* lbd, struct cbd_stats* stats)
|
|
{
|
|
int ret = 0;
|
|
int err;
|
|
u32 n;
|
|
u64 pblk;
|
|
u32 nr_pages = lblk_per_pblk(lbd->params);
|
|
u32 count;
|
|
struct page* iopagev[1];
|
|
|
|
mutex_lock(&lbd->lock);
|
|
if (!PageDirty(lbd->pagev[0])) {
|
|
goto unlock;
|
|
}
|
|
if (lbd_error(lbd)) {
|
|
ret = -EIO;
|
|
goto unlock;
|
|
}
|
|
|
|
if (lblk_is_zeros(lbd->params, lbd)) {
|
|
lbd->c_len = CBD_UNCOMPRESSED;
|
|
ret = lbatview_elem_realloc(lbd->lv, lbd->lblk, 0, stats);
|
|
goto unlock;
|
|
}
|
|
lbd->c_len = lblk_compress(lbd);
|
|
if (lbd->c_len > 0) {
|
|
u32 c_blkrem = lbd->c_len % PBLK_SIZE;
|
|
if (c_blkrem) {
|
|
memset(lbd->buf + lbd->c_len, 0, c_blkrem);
|
|
}
|
|
count = DIV_ROUND_UP(lbd->c_len, PBLK_SIZE);
|
|
}
|
|
else {
|
|
lbd->c_len = CBD_UNCOMPRESSED;
|
|
count = lblk_per_pblk(lbd->params);
|
|
}
|
|
ret = lbatview_elem_realloc(lbd->lv, lbd->lblk, lbd->c_len, stats);
|
|
if (ret) {
|
|
goto unlock;
|
|
}
|
|
for (n = 0; n < count; ++n) {
|
|
pblk = lbatview_elem_pblk(lbd->lv, lbd->lblk, n);
|
|
BUG_ON(pblk == PBLK_NONE);
|
|
iopagev[0] = lbd->pagev[n];
|
|
pblk_write(lbd->params->priv, pblk, 1, iopagev);
|
|
}
|
|
while (n < lblk_per_pblk(lbd->params)) {
|
|
unlock_page(lbd->pagev[n]);
|
|
++n;
|
|
}
|
|
goto out;
|
|
|
|
unlock:
|
|
for (n = 0; n < nr_pages; ++n) {
|
|
unlock_page(lbd->pagev[n]);
|
|
}
|
|
|
|
out:
|
|
err = lbatviewcache_put(lbd->lvc, lbd->lv);
|
|
lbd->lv = NULL;
|
|
if (err) {
|
|
ret = err;
|
|
}
|
|
mutex_unlock(&lbd->lock);
|
|
|
|
return ret;
|
|
}
|
|
|
|
static int
|
|
lbd_read(struct lbd* lbd)
|
|
{
|
|
int ret = 0;
|
|
u32 count;
|
|
u32 n;
|
|
u64 pblk;
|
|
struct page* iopagev[1];
|
|
|
|
/* XXX: can't happen because lbdcache will not use a page with an error */
|
|
if (PageError(lbd->pagev[0])) {
|
|
return -EIO;
|
|
}
|
|
lbd->c_len = lbatview_elem_len(lbd->lv, lbd->lblk);
|
|
if (lbd->c_len == 0) {
|
|
memset(lbd->buf, 0, PBLK_SIZE * lblk_per_pblk(lbd->params));
|
|
}
|
|
else {
|
|
count = (lbd->c_len == CBD_UNCOMPRESSED) ?
|
|
lblk_per_pblk(lbd->params) :
|
|
DIV_ROUND_UP(lbd->c_len, PBLK_SIZE);
|
|
for (n = 0; n < count; ++n) {
|
|
pblk = lbatview_elem_pblk(lbd->lv, lbd->lblk, n);
|
|
if (pblk == PBLK_NONE) {
|
|
ret = -EIO;
|
|
goto out;
|
|
}
|
|
iopagev[0] = lbd->pagev[n];
|
|
/* XXX: Issue non-blocking reads? */
|
|
ret = pblk_read_wait(lbd->params->priv, pblk, 1, iopagev);
|
|
if (ret) {
|
|
goto out;
|
|
}
|
|
}
|
|
if (lbd->c_len != CBD_UNCOMPRESSED) {
|
|
if (!lblk_decompress(lbd)) {
|
|
printk(KERN_ERR " decompress failed\n");
|
|
ret = -EIO;
|
|
goto out;
|
|
}
|
|
}
|
|
}
|
|
lbd->c_len = CBD_UNCOMPRESSED;
|
|
|
|
out:
|
|
return ret;
|
|
}
|
|
|
|
static int
|
|
lbd_reset(struct lbd* lbd, u64 lblk)
|
|
{
|
|
int ret = 0;
|
|
u32 nr_pages = lblk_per_pblk(lbd->params);
|
|
u32 n;
|
|
|
|
if (lbd->lv) { printk(KERN_ERR "%s: lbatview leak\n", __func__); }
|
|
|
|
for (n = 0; n < nr_pages; ++n) {
|
|
lock_page(lbd->pagev[n]);
|
|
}
|
|
lbd->lv = lbatviewcache_get(lbd->lvc, lblk);
|
|
if (!lbd->lv) {
|
|
printk(KERN_ERR "%s: lbatviewcache_get failed\n", __func__);
|
|
ret = -EIO;
|
|
goto out;
|
|
}
|
|
if (lbd->lblk != lblk) {
|
|
lbd->lblk = lblk;
|
|
ret = lbd_read(lbd);
|
|
if (ret) {
|
|
printk(KERN_ERR "%s: lbd_read failed\n", __func__);
|
|
}
|
|
}
|
|
else {
|
|
if (lbd->c_len != CBD_UNCOMPRESSED) {
|
|
if (!lblk_decompress(lbd)) {
|
|
printk(KERN_ERR "%s: lblk_decompress failed\n", __func__);
|
|
ret = -EIO;
|
|
}
|
|
lbd->c_len = CBD_UNCOMPRESSED;
|
|
}
|
|
}
|
|
|
|
out:
|
|
if (ret) {
|
|
lbatviewcache_put(lbd->lvc, lbd->lv);
|
|
lbd->lv = NULL;
|
|
for (n = 0; n < nr_pages; ++n) {
|
|
unlock_page(lbd->pagev[n]);
|
|
}
|
|
lbd->lblk = LBLK_NONE;
|
|
}
|
|
|
|
return ret;
|
|
}
|
|
|
|
u64
|
|
lbd_lblk(struct lbd* lbd)
|
|
{
|
|
return lbd->lblk;
|
|
}
|
|
|
|
void
|
|
lbd_data_read(struct lbd* lbd, u32 off, u32 len, u8* buf)
|
|
{
|
|
/* XXX: convert to BUG_ON */
|
|
if (off + len > PBLK_SIZE * lblk_per_pblk(lbd->params)) {
|
|
printk(KERN_ERR "%s: out of bounds\n", __func__);
|
|
return;
|
|
}
|
|
mutex_lock(&lbd->lock);
|
|
memcpy(buf, lbd->buf + off, len);
|
|
mutex_unlock(&lbd->lock);
|
|
}
|
|
|
|
void
|
|
lbd_data_write(struct lbd* lbd, u32 off, u32 len, const u8* buf)
|
|
{
|
|
/* XXX: convert to BUG_ON */
|
|
if (off + len > PBLK_SIZE * lblk_per_pblk(lbd->params)) {
|
|
printk(KERN_ERR "%s: out of bounds\n", __func__);
|
|
return;
|
|
}
|
|
mutex_lock(&lbd->lock);
|
|
memcpy(lbd->buf + off, buf, len);
|
|
SetPageDirty(lbd->pagev[0]);
|
|
mutex_unlock(&lbd->lock);
|
|
}
|
|
|
|
struct lbdcache
|
|
{
|
|
struct mutex lock;
|
|
struct cbd_params* params;
|
|
struct cbd_stats* stats;
|
|
void* percpu;
|
|
struct lbatviewcache* lvc;
|
|
struct list_head cache_head;
|
|
unsigned int cache_len;
|
|
struct lbd* cache;
|
|
struct delayed_work flush_dwork;
|
|
struct list_head flush_head;
|
|
};
|
|
|
|
static void lbdcache_flush(struct work_struct*);
|
|
|
|
size_t
|
|
lbdcache_size(void)
|
|
{
|
|
return sizeof(struct lbdcache);
|
|
}
|
|
|
|
static bool
|
|
lbdcache_alloc_compress_state(void* percpu, const struct cbd_params* params, int cpu)
|
|
{
|
|
struct lblk_compress_state* state;
|
|
struct lblk_compress_state** statep;
|
|
size_t workmem_len;
|
|
#ifdef COMPRESS_HAVE_ZLIB
|
|
int ret;
|
|
#endif
|
|
|
|
state = kzalloc(sizeof(struct lblk_compress_state), GFP_NOWAIT);
|
|
if (!state) {
|
|
printk(KERN_ERR "%s: failed to alloc state\n", __func__);
|
|
return false;
|
|
}
|
|
statep = per_cpu_ptr(percpu, cpu);
|
|
*statep = state;
|
|
state->buf = vmalloc(PBLK_SIZE * lblk_per_pblk(params));
|
|
if (!state->buf) {
|
|
return false;
|
|
}
|
|
#ifdef COMPRESS_HAVE_LZ4
|
|
workmem_len = LZ4_compressBound(PBLK_SIZE * lblk_per_pblk(params));
|
|
state->lz4_workmem = vzalloc(workmem_len);
|
|
if (!state->lz4_workmem) {
|
|
return false;
|
|
}
|
|
#endif
|
|
#ifdef COMPRESS_HAVE_ZLIB
|
|
workmem_len = zlib_deflate_workspacesize(MAX_WBITS, DEF_MEM_LEVEL);
|
|
state->zlib_cstream.workspace = vzalloc(workmem_len);
|
|
if (!state->zlib_cstream.workspace) {
|
|
return false;
|
|
}
|
|
ret = zlib_deflateInit2(&state->zlib_cstream, params->compression,
|
|
Z_DEFLATED, MAX_WBITS, DEF_MEM_LEVEL,
|
|
Z_DEFAULT_STRATEGY);
|
|
BUG_ON(ret != Z_OK);
|
|
workmem_len = zlib_inflate_workspacesize();
|
|
state->zlib_dstream.workspace = vzalloc(workmem_len);
|
|
if (!state->zlib_dstream.workspace) {
|
|
return false;
|
|
}
|
|
ret = zlib_inflateInit2(&state->zlib_dstream, DEF_WBITS);
|
|
BUG_ON(ret != Z_OK);
|
|
#endif
|
|
|
|
return true;
|
|
}
|
|
|
|
static void
|
|
lbdcache_free_compress_state(void* percpu, const struct cbd_params* params, int cpu)
|
|
{
|
|
struct lblk_compress_state** statep;
|
|
struct lblk_compress_state* state;
|
|
|
|
statep = per_cpu_ptr(percpu, cpu);
|
|
state = *statep;
|
|
if (!state) {
|
|
return;
|
|
}
|
|
#ifdef COMPRESS_HAVE_ZLIB
|
|
vfree(state->zlib_dstream.workspace);
|
|
vfree(state->zlib_cstream.workspace);
|
|
#endif
|
|
#ifdef COMPRESS_HAVE_LZ4
|
|
vfree(state->lz4_workmem);
|
|
#endif
|
|
vfree(state->buf);
|
|
kfree(state);
|
|
}
|
|
|
|
bool
|
|
lbdcache_ctr(struct lbdcache* lc,
|
|
struct cbd_params* params, struct cbd_stats* stats,
|
|
u32 cache_pages)
|
|
{
|
|
int cpu;
|
|
struct lbd* cache;
|
|
u32 cache_len;
|
|
u32 n;
|
|
|
|
memset(lc, 0, sizeof(struct lbdcache));
|
|
mutex_init(&lc->lock);
|
|
lc->params = params;
|
|
lc->stats = stats;
|
|
lc->percpu = alloc_percpu(void*);
|
|
for (cpu = 0; cpu < num_online_cpus(); ++cpu) {
|
|
if (!lbdcache_alloc_compress_state(lc->percpu, params, cpu)) {
|
|
return false;
|
|
}
|
|
}
|
|
lc->lvc = kzalloc(lbatviewcache_size(), GFP_KERNEL);
|
|
if (!lc->lvc) {
|
|
return false;
|
|
}
|
|
if (!lbatviewcache_ctr(lc->lvc, params, cache_pages)) {
|
|
return false;
|
|
}
|
|
|
|
/* lbdcache gets 1/2 of cache_pages */
|
|
cache_len = (cache_pages * 1 / 2) / lblk_per_pblk(params);
|
|
if (!cache_len) {
|
|
printk(KERN_ERR "%s: Cache too small\n", __func__);
|
|
return false;
|
|
}
|
|
printk(KERN_INFO "%s: cache_len=%u\n", __func__, cache_len);
|
|
cache = kzalloc(cache_len * sizeof(struct lbd), GFP_KERNEL);
|
|
if (!cache) {
|
|
return false;
|
|
}
|
|
INIT_LIST_HEAD(&lc->cache_head);
|
|
lc->cache_len = cache_len;
|
|
lc->cache = cache;
|
|
for (n = 0; n < cache_len; ++n) {
|
|
if (!lbd_ctr(&cache[n], params, lc->lvc, lc->percpu)) {
|
|
return false;
|
|
}
|
|
list_add_tail(&cache[n].lru_list, &lc->cache_head);
|
|
}
|
|
INIT_DELAYED_WORK(&lc->flush_dwork, lbdcache_flush);
|
|
INIT_LIST_HEAD(&lc->flush_head);
|
|
|
|
return true;
|
|
}
|
|
|
|
void
|
|
lbdcache_dtr(struct lbdcache* lc)
|
|
{
|
|
int ret;
|
|
unsigned int n;
|
|
struct lbd* lbd;
|
|
int cpu;
|
|
|
|
cancel_delayed_work_sync(&lc->flush_dwork);
|
|
flush_delayed_work(&lc->flush_dwork);
|
|
list_for_each_entry(lbd, &lc->flush_head, flush_list) {
|
|
ret = lbd_flush(lbd, lc->stats);
|
|
if (ret) {
|
|
printk(KERN_ERR "%s: lbd_flush failed\n", __func__);
|
|
}
|
|
}
|
|
for (n = 0; n < lc->cache_len; ++n) {
|
|
lbd = &lc->cache[n];
|
|
if (!lbd) {
|
|
continue;
|
|
}
|
|
lbd_dtr(lbd);
|
|
if (lbd->ref) {
|
|
printk(KERN_ERR "%s: lbd ref leak: n=%u ref=%u\n", __func__, n, lbd->ref);
|
|
}
|
|
}
|
|
kfree(lc->cache);
|
|
lc->cache = NULL;
|
|
lc->cache_len = 0;
|
|
INIT_LIST_HEAD(&lc->cache_head);
|
|
lbatviewcache_dtr(lc->lvc);
|
|
kfree(lc->lvc);
|
|
lc->lvc = NULL;
|
|
for (cpu = 0; cpu < num_online_cpus(); ++cpu) {
|
|
lbdcache_free_compress_state(lc->percpu, lc->params, cpu);
|
|
}
|
|
free_percpu(lc->percpu);
|
|
lc->percpu = NULL;
|
|
lc->params = NULL;
|
|
}
|
|
|
|
static void
|
|
lbdcache_flush(struct work_struct* work)
|
|
{
|
|
struct lbdcache* lc = container_of(work, struct lbdcache, flush_dwork.work);
|
|
unsigned long now = jiffies;
|
|
int ret;
|
|
struct lbd* lbd;
|
|
|
|
mutex_lock(&lc->lock);
|
|
while (!list_empty(&lc->flush_head)) {
|
|
lbd = list_first_entry(&lc->flush_head, struct lbd, flush_list);
|
|
mutex_lock(&lbd->reflock);
|
|
BUG_ON(lbd->ref != 1);
|
|
if (lbd->flush_jiffies > now) {
|
|
mutex_unlock(&lbd->reflock);
|
|
break;
|
|
}
|
|
list_del_init(&lbd->flush_list);
|
|
lbd->ref = 0;
|
|
ret = lbd_flush(lbd, lc->stats);
|
|
if (ret) {
|
|
printk(KERN_ERR "%s: lbd_flush failed\n", __func__);
|
|
}
|
|
mutex_unlock(&lbd->reflock);
|
|
}
|
|
if (!list_empty(&lc->flush_head)) {
|
|
schedule_delayed_work(&lc->flush_dwork, COMPRESS_FLUSH_DELAY);
|
|
}
|
|
mutex_unlock(&lc->lock);
|
|
}
|
|
|
|
struct lbd*
|
|
lbdcache_get(struct lbdcache* lc, u64 lblk)
|
|
{
|
|
struct lbd* lbd;
|
|
|
|
mutex_lock(&lc->lock);
|
|
list_for_each_entry(lbd, &lc->cache_head, lru_list) {
|
|
mutex_lock(&lbd->reflock);
|
|
if (lbd->lblk == lblk) {
|
|
list_move(&lbd->lru_list, &lc->cache_head);
|
|
if (lbd->ref == 0) {
|
|
goto found;
|
|
}
|
|
if (lbd->ref == 1 && !list_empty(&lc->flush_head)) {
|
|
struct lbd* entry;
|
|
list_for_each_entry(entry, &lc->flush_head, flush_list) {
|
|
if (entry == lbd) {
|
|
list_del_init(&lbd->flush_list);
|
|
lbd->ref = 0;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
mutex_unlock(&lc->lock);
|
|
++lbd->ref;
|
|
mutex_unlock(&lbd->reflock);
|
|
return lbd;
|
|
}
|
|
if (lbd->lblk == LBLK_NONE) {
|
|
list_move(&lbd->lru_list, &lc->cache_head);
|
|
goto found;
|
|
}
|
|
mutex_unlock(&lbd->reflock);
|
|
}
|
|
list_for_each_entry_reverse(lbd, &lc->cache_head, lru_list) {
|
|
mutex_lock(&lbd->reflock);
|
|
if (lbd->ref == 0 && !lbd_error(lbd)) {
|
|
list_move(&lbd->lru_list, &lc->cache_head);
|
|
goto found;
|
|
}
|
|
mutex_unlock(&lbd->reflock);
|
|
}
|
|
if (!list_empty(&lc->flush_head)) {
|
|
int ret;
|
|
lbd = list_first_entry(&lc->flush_head, struct lbd, flush_list);
|
|
mutex_lock(&lbd->reflock);
|
|
BUG_ON(lbd->ref != 1);
|
|
list_del_init(&lbd->flush_list);
|
|
lbd->ref = 0;
|
|
ret = lbd_flush(lbd, lc->stats);
|
|
if (ret) {
|
|
printk(KERN_ERR "%s: lbd_flush failed\n", __func__);
|
|
}
|
|
goto found;
|
|
}
|
|
printk(KERN_ERR "%s: failed to find free entry\n", __func__);
|
|
mutex_unlock(&lc->lock);
|
|
return NULL;
|
|
|
|
found:
|
|
mutex_unlock(&lc->lock);
|
|
if (lbd_reset(lbd, lblk) != 0) {
|
|
mutex_unlock(&lbd->reflock);
|
|
return NULL;
|
|
}
|
|
lbd->ref = 1;
|
|
mutex_unlock(&lbd->reflock);
|
|
|
|
return lbd;
|
|
}
|
|
|
|
int
|
|
lbdcache_put(struct lbdcache* lc, struct lbd* lbd)
|
|
{
|
|
int ret = 0;
|
|
|
|
if (!lbd) {
|
|
return 0;
|
|
}
|
|
mutex_lock(&lc->lock);
|
|
mutex_lock(&lbd->reflock);
|
|
if (--lbd->ref == 0) {
|
|
lbd->flush_jiffies = jiffies + COMPRESS_FLUSH_DELAY;
|
|
lbd->ref = 1;
|
|
list_add_tail(&lbd->flush_list, &lc->flush_head);
|
|
/* This is racy, but it does not matter. */
|
|
if (!delayed_work_pending(&lc->flush_dwork)) {
|
|
schedule_delayed_work(&lc->flush_dwork, COMPRESS_FLUSH_DELAY);
|
|
}
|
|
}
|
|
mutex_unlock(&lbd->reflock);
|
|
mutex_unlock(&lc->lock);
|
|
|
|
return ret;
|
|
}
|