2019-10-22 04:39:27 +02:00
|
|
|
/*
|
|
|
|
* 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/dm-compress.h>
|
|
|
|
|
2019-11-11 21:07:11 +01:00
|
|
|
struct lbatpblk {
|
2019-11-06 22:18:16 +01:00
|
|
|
struct list_head list;
|
2019-10-22 04:39:27 +02:00
|
|
|
u64 pblk;
|
|
|
|
struct mutex reflock;
|
|
|
|
unsigned int ref;
|
|
|
|
|
|
|
|
struct mutex lock;
|
2019-11-11 20:48:46 +01:00
|
|
|
struct compress_params* kparams;
|
2019-11-11 23:58:09 +01:00
|
|
|
struct compress_stats* kstats;
|
2019-10-22 04:39:27 +02:00
|
|
|
struct page* page;
|
|
|
|
u8* buf;
|
|
|
|
bool dirty;
|
|
|
|
};
|
|
|
|
|
2019-10-25 19:03:00 +02:00
|
|
|
static bool
|
2019-11-11 23:58:09 +01:00
|
|
|
lbatpblk_ctr(struct lbatpblk* lp,
|
|
|
|
struct compress_params* kparams,
|
|
|
|
struct compress_stats* kstats)
|
2019-10-22 04:39:27 +02:00
|
|
|
{
|
2019-11-11 21:07:11 +01:00
|
|
|
memset(lp, 0, sizeof(struct lbatpblk));
|
2019-11-06 22:18:16 +01:00
|
|
|
INIT_LIST_HEAD(&lp->list);
|
2019-10-22 04:39:27 +02:00
|
|
|
lp->pblk = PBLK_NONE;
|
|
|
|
mutex_init(&lp->reflock);
|
|
|
|
lp->ref = 0;
|
|
|
|
mutex_init(&lp->lock);
|
2019-11-11 20:48:46 +01:00
|
|
|
lp->kparams = kparams;
|
2019-11-11 23:58:09 +01:00
|
|
|
lp->kstats = kstats;
|
2019-10-22 04:39:27 +02:00
|
|
|
lp->page = cbd_alloc_page();
|
|
|
|
if (!lp->page) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
lp->buf = page_address(lp->page);
|
|
|
|
lp->dirty = false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-10-25 19:03:00 +02:00
|
|
|
static void
|
2019-11-11 21:07:11 +01:00
|
|
|
lbatpblk_dtr(struct lbatpblk* lp)
|
2019-10-22 04:39:27 +02:00
|
|
|
{
|
|
|
|
lp->buf = NULL;
|
|
|
|
cbd_free_page(lp->page);
|
|
|
|
lp->page = NULL;
|
|
|
|
}
|
|
|
|
|
2019-11-01 22:41:11 +01:00
|
|
|
static bool
|
2019-11-11 21:07:11 +01:00
|
|
|
lbatpblk_error(struct lbatpblk* lp)
|
2019-10-22 04:39:27 +02:00
|
|
|
{
|
2019-11-01 22:41:11 +01:00
|
|
|
return PageError(lp->page);
|
2019-10-22 04:39:27 +02:00
|
|
|
}
|
|
|
|
|
2019-10-25 19:03:00 +02:00
|
|
|
static int
|
2019-11-11 21:07:11 +01:00
|
|
|
lbatpblk_flush(struct lbatpblk* lp)
|
2019-10-22 04:39:27 +02:00
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
struct page* iopagev[1];
|
|
|
|
|
|
|
|
mutex_lock(&lp->lock);
|
2019-11-01 22:41:11 +01:00
|
|
|
if (!PageDirty(lp->page)) {
|
|
|
|
goto unlock;
|
|
|
|
}
|
2019-11-11 21:07:11 +01:00
|
|
|
if (lbatpblk_error(lp)) {
|
2019-11-01 22:41:11 +01:00
|
|
|
ret = -EIO;
|
|
|
|
goto unlock;
|
2019-10-22 04:39:27 +02:00
|
|
|
}
|
2019-10-31 22:48:12 +01:00
|
|
|
iopagev[0] = lp->page;
|
2019-11-11 20:48:46 +01:00
|
|
|
pblk_write(lp->kparams, lp->pblk, 1, iopagev);
|
2019-10-22 04:39:27 +02:00
|
|
|
mutex_unlock(&lp->lock);
|
2019-11-11 23:58:09 +01:00
|
|
|
mutex_lock(&lp->kstats->lock);
|
|
|
|
++lp->kstats->lbatpblk_w;
|
|
|
|
mutex_unlock(&lp->kstats->lock);
|
2019-11-01 22:41:11 +01:00
|
|
|
|
2019-10-22 04:39:27 +02:00
|
|
|
return ret;
|
|
|
|
|
2019-11-01 22:41:11 +01:00
|
|
|
unlock:
|
|
|
|
unlock_page(lp->page);
|
|
|
|
mutex_unlock(&lp->lock);
|
|
|
|
|
|
|
|
return ret;
|
2019-10-25 19:03:00 +02:00
|
|
|
}
|
|
|
|
|
2019-11-01 22:41:11 +01:00
|
|
|
static int
|
2019-11-11 21:07:11 +01:00
|
|
|
lbatpblk_read(struct lbatpblk* lp)
|
2019-10-22 04:39:27 +02:00
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
struct page* pagev[1];
|
|
|
|
|
|
|
|
pagev[0] = lp->page;
|
2019-11-11 20:48:46 +01:00
|
|
|
ret = pblk_read_wait(lp->kparams, lp->pblk, 1, pagev);
|
2019-11-11 23:58:09 +01:00
|
|
|
mutex_lock(&lp->kstats->lock);
|
|
|
|
++lp->kstats->lbatpblk_r;
|
|
|
|
mutex_unlock(&lp->kstats->lock);
|
2019-11-01 22:41:11 +01:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2019-11-11 21:07:11 +01:00
|
|
|
lbatpblk_reset(struct lbatpblk* lp, u64 pblk)
|
2019-11-01 22:41:11 +01:00
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
lock_page(lp->page);
|
|
|
|
if (lp->pblk != pblk) {
|
|
|
|
lp->pblk = pblk;
|
2019-11-11 21:07:11 +01:00
|
|
|
ret = lbatpblk_read(lp);
|
2019-11-01 22:41:11 +01:00
|
|
|
}
|
|
|
|
|
2019-10-22 04:39:27 +02:00
|
|
|
if (ret) {
|
2019-11-01 22:41:11 +01:00
|
|
|
unlock_page(lp->page);
|
|
|
|
lp->pblk = PBLK_NONE;
|
2019-10-22 04:39:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
u8*
|
2019-11-11 21:07:11 +01:00
|
|
|
lbatpblk_get_buf(struct lbatpblk* lp, bool rw)
|
2019-10-22 04:39:27 +02:00
|
|
|
{
|
|
|
|
mutex_lock(&lp->lock);
|
|
|
|
if (rw) {
|
2019-11-01 22:41:11 +01:00
|
|
|
SetPageDirty(lp->page);
|
2019-10-22 04:39:27 +02:00
|
|
|
}
|
2019-11-01 22:41:11 +01:00
|
|
|
|
2019-10-22 04:39:27 +02:00
|
|
|
return lp->buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2019-11-11 21:07:11 +01:00
|
|
|
lbatpblk_put_buf(struct lbatpblk* lp)
|
2019-10-22 04:39:27 +02:00
|
|
|
{
|
|
|
|
mutex_unlock(&lp->lock);
|
|
|
|
}
|
|
|
|
|
2019-11-11 21:07:11 +01:00
|
|
|
struct lbatpblkcache {
|
2019-11-08 23:01:19 +01:00
|
|
|
struct mutex cache_lock;
|
2019-11-06 22:18:16 +01:00
|
|
|
struct list_head cache_head;
|
|
|
|
unsigned int cache_len;
|
2019-11-11 21:07:11 +01:00
|
|
|
struct lbatpblk* cache;
|
2019-10-22 04:39:27 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
size_t
|
2019-11-11 21:07:11 +01:00
|
|
|
lbatpblkcache_size(void)
|
2019-10-22 04:39:27 +02:00
|
|
|
{
|
2019-11-11 21:07:11 +01:00
|
|
|
return sizeof(struct lbatpblkcache);
|
2019-10-22 04:39:27 +02:00
|
|
|
}
|
|
|
|
|
2019-11-06 22:18:16 +01:00
|
|
|
bool
|
2019-11-11 21:07:11 +01:00
|
|
|
lbatpblkcache_ctr(struct lbatpblkcache* lpc,
|
2019-11-11 23:58:09 +01:00
|
|
|
struct compress_params* kparams, struct compress_stats* kstats,
|
|
|
|
u32 cache_pages)
|
2019-10-22 04:39:27 +02:00
|
|
|
{
|
2019-11-11 21:07:11 +01:00
|
|
|
struct lbatpblk* cache;
|
2019-11-06 22:18:16 +01:00
|
|
|
u32 cache_len;
|
|
|
|
u32 n;
|
2019-10-22 04:39:27 +02:00
|
|
|
|
2019-11-11 21:07:11 +01:00
|
|
|
memset(lpc, 0, sizeof(struct lbatpblkcache));
|
2019-11-06 22:18:16 +01:00
|
|
|
|
2019-11-11 21:07:11 +01:00
|
|
|
/* lbatpblkcache gets 15/32 of cache pages */
|
2019-11-06 22:18:16 +01:00
|
|
|
cache_len = (cache_pages * 15 / 32);
|
|
|
|
if (!cache_len) {
|
|
|
|
printk(KERN_ERR "%s: Cache too small\n", __func__);
|
2019-10-22 04:39:27 +02:00
|
|
|
return false;
|
|
|
|
}
|
2019-11-06 22:18:16 +01:00
|
|
|
printk(KERN_INFO "%s: cache_len=%u\n", __func__, cache_len);
|
2019-11-11 21:07:11 +01:00
|
|
|
cache = kzalloc(cache_len * sizeof(struct lbatpblk), GFP_KERNEL);
|
2019-11-06 22:18:16 +01:00
|
|
|
if (!cache) {
|
|
|
|
return false;
|
2019-10-22 04:39:27 +02:00
|
|
|
}
|
2019-11-08 23:01:19 +01:00
|
|
|
mutex_init(&lpc->cache_lock);
|
2019-11-06 22:18:16 +01:00
|
|
|
INIT_LIST_HEAD(&lpc->cache_head);
|
|
|
|
lpc->cache_len = cache_len;
|
2019-10-22 04:39:27 +02:00
|
|
|
lpc->cache = cache;
|
2019-11-06 22:18:16 +01:00
|
|
|
for (n = 0; n < cache_len; ++n) {
|
2019-11-11 23:58:09 +01:00
|
|
|
if (!lbatpblk_ctr(&cache[n], kparams, kstats)) {
|
2019-10-22 04:39:27 +02:00
|
|
|
return false;
|
|
|
|
}
|
2019-11-06 22:18:16 +01:00
|
|
|
list_add_tail(&cache[n].list, &lpc->cache_head);
|
2019-10-22 04:39:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2019-11-11 21:07:11 +01:00
|
|
|
lbatpblkcache_dtr(struct lbatpblkcache* lpc)
|
2019-10-22 04:39:27 +02:00
|
|
|
{
|
|
|
|
unsigned int n;
|
2019-11-11 21:07:11 +01:00
|
|
|
struct lbatpblk* lp;
|
2019-10-22 04:39:27 +02:00
|
|
|
|
2019-11-06 22:18:16 +01:00
|
|
|
for (n = 0; n < lpc->cache_len; ++n) {
|
|
|
|
lp = &lpc->cache[n];
|
2019-10-22 04:39:27 +02:00
|
|
|
if (!lp) {
|
|
|
|
continue;
|
|
|
|
}
|
2019-11-11 21:07:11 +01:00
|
|
|
lbatpblk_dtr(lp);
|
2019-10-22 04:39:27 +02:00
|
|
|
if (lp->ref) {
|
2019-11-11 21:07:11 +01:00
|
|
|
printk(KERN_ERR "%s: lbatpblk ref leak: n=%u ref=%u\n", __func__, n, lp->ref);
|
2019-10-22 04:39:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
kfree(lpc->cache);
|
|
|
|
lpc->cache = NULL;
|
2019-11-06 22:18:16 +01:00
|
|
|
lpc->cache_len = 0;
|
|
|
|
INIT_LIST_HEAD(&lpc->cache_head);
|
2019-10-22 04:39:27 +02:00
|
|
|
}
|
|
|
|
|
2019-11-11 21:07:11 +01:00
|
|
|
struct lbatpblk*
|
|
|
|
lbatpblkcache_get(struct lbatpblkcache* lpc, u64 pblk)
|
2019-10-22 04:39:27 +02:00
|
|
|
{
|
2019-11-11 21:07:11 +01:00
|
|
|
struct lbatpblk* lp;
|
2019-10-22 04:39:27 +02:00
|
|
|
|
2019-11-08 23:01:19 +01:00
|
|
|
mutex_lock(&lpc->cache_lock);
|
2019-11-06 22:18:16 +01:00
|
|
|
list_for_each_entry(lp, &lpc->cache_head, list) {
|
2019-10-22 04:39:27 +02:00
|
|
|
mutex_lock(&lp->reflock);
|
|
|
|
if (lp->pblk == pblk) {
|
2019-11-06 22:18:16 +01:00
|
|
|
list_move(&lp->list, &lpc->cache_head);
|
2019-11-08 23:01:19 +01:00
|
|
|
mutex_unlock(&lpc->cache_lock);
|
2019-10-24 22:02:03 +02:00
|
|
|
if (lp->ref == 0) {
|
|
|
|
goto found;
|
|
|
|
}
|
2019-10-22 04:39:27 +02:00
|
|
|
++lp->ref;
|
|
|
|
mutex_unlock(&lp->reflock);
|
2019-11-04 19:22:10 +01:00
|
|
|
return lp;
|
2019-10-22 04:39:27 +02:00
|
|
|
}
|
|
|
|
if (lp->pblk == PBLK_NONE) {
|
2019-11-06 22:18:16 +01:00
|
|
|
list_move(&lp->list, &lpc->cache_head);
|
2019-11-08 23:01:19 +01:00
|
|
|
mutex_unlock(&lpc->cache_lock);
|
2019-10-22 04:39:27 +02:00
|
|
|
goto found;
|
|
|
|
}
|
|
|
|
mutex_unlock(&lp->reflock);
|
|
|
|
}
|
2019-11-06 22:18:16 +01:00
|
|
|
list_for_each_entry_reverse(lp, &lpc->cache_head, list) {
|
2019-10-22 04:39:27 +02:00
|
|
|
mutex_lock(&lp->reflock);
|
2019-11-11 21:07:11 +01:00
|
|
|
if (lp->ref == 0 && !lbatpblk_error(lp)) {
|
2019-11-06 22:18:16 +01:00
|
|
|
list_move(&lp->list, &lpc->cache_head);
|
2019-11-08 23:01:19 +01:00
|
|
|
mutex_unlock(&lpc->cache_lock);
|
2019-10-22 04:39:27 +02:00
|
|
|
goto found;
|
|
|
|
}
|
|
|
|
mutex_unlock(&lp->reflock);
|
|
|
|
}
|
2019-11-06 22:18:16 +01:00
|
|
|
printk(KERN_ERR "%s: failed to find free entry\n", __func__);
|
2019-11-08 23:01:19 +01:00
|
|
|
mutex_unlock(&lpc->cache_lock);
|
2019-11-06 22:18:16 +01:00
|
|
|
return NULL;
|
2019-10-22 04:39:27 +02:00
|
|
|
|
|
|
|
found:
|
2019-11-11 21:07:11 +01:00
|
|
|
if (lbatpblk_reset(lp, pblk) != 0) {
|
2019-11-01 22:41:11 +01:00
|
|
|
mutex_unlock(&lp->reflock);
|
2019-11-04 19:22:10 +01:00
|
|
|
return NULL;
|
2019-11-01 22:41:11 +01:00
|
|
|
}
|
2019-10-22 04:39:27 +02:00
|
|
|
lp->ref = 1;
|
|
|
|
mutex_unlock(&lp->reflock);
|
|
|
|
|
|
|
|
return lp;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2019-11-11 21:07:11 +01:00
|
|
|
lbatpblkcache_put(struct lbatpblkcache* lpc, struct lbatpblk* lp)
|
2019-10-22 04:39:27 +02:00
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
if (!lp) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
mutex_lock(&lp->reflock);
|
|
|
|
if (--lp->ref == 0) {
|
2019-11-11 21:07:11 +01:00
|
|
|
ret = lbatpblk_flush(lp);
|
2019-10-22 04:39:27 +02:00
|
|
|
if (ret) {
|
2019-11-11 21:07:11 +01:00
|
|
|
printk(KERN_ERR "%s: lbatpblk_flush failed\n", __func__);
|
2019-10-22 04:39:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
mutex_unlock(&lp->reflock);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|