summaryrefslogtreecommitdiffstats
path: root/src/kernel/xloop_file_fmt_qcow_cache.c
diff options
context:
space:
mode:
authorManuel Bentele2021-03-15 12:18:10 +0100
committerManuel Bentele2021-03-15 12:18:10 +0100
commit3bc3b89d5a85b9642e96688e6c9c2862a95e7b45 (patch)
tree7f4e067962ced878427d128410671e6d1c1d4154 /src/kernel/xloop_file_fmt_qcow_cache.c
parentAdd support in CMake to validate (lint) the source code (diff)
downloadxloop-3bc3b89d5a85b9642e96688e6c9c2862a95e7b45.tar.gz
xloop-3bc3b89d5a85b9642e96688e6c9c2862a95e7b45.tar.xz
xloop-3bc3b89d5a85b9642e96688e6c9c2862a95e7b45.zip
Refactor kernel code to satisfy Linux kernel code style
Diffstat (limited to 'src/kernel/xloop_file_fmt_qcow_cache.c')
-rw-r--r--src/kernel/xloop_file_fmt_qcow_cache.c83
1 files changed, 35 insertions, 48 deletions
diff --git a/src/kernel/xloop_file_fmt_qcow_cache.c b/src/kernel/xloop_file_fmt_qcow_cache.c
index 14f0b75..d9701b6 100644
--- a/src/kernel/xloop_file_fmt_qcow_cache.c
+++ b/src/kernel/xloop_file_fmt_qcow_cache.c
@@ -1,4 +1,4 @@
-/* SPDX-License-Identifier: GPL-2.0 */
+// SPDX-License-Identifier: GPL-2.0
/*
* xloop_file_fmt_qcow_cache.c
*
@@ -26,20 +26,19 @@
#include "xloop_file_fmt_qcow_main.h"
#include "xloop_file_fmt_qcow_cache.h"
-static inline void *__xloop_file_fmt_qcow_cache_get_table_addr(
- struct xloop_file_fmt_qcow_cache *c, int table)
+static inline void *__xloop_file_fmt_qcow_cache_get_table_addr(struct xloop_file_fmt_qcow_cache *c, int table)
{
- return (u8 *) c->table_array + (size_t) table * c->table_size;
+ return (u8 *)c->table_array + (size_t)table * c->table_size;
}
-static inline int __xloop_file_fmt_qcow_cache_get_table_idx(
- struct xloop_file_fmt_qcow_cache *c, void *table)
+static inline int __xloop_file_fmt_qcow_cache_get_table_idx(struct xloop_file_fmt_qcow_cache *c, void *table)
{
- ptrdiff_t table_offset = (u8 *) table - (u8 *) c->table_array;
+ ptrdiff_t table_offset = (u8 *)table - (u8 *)c->table_array;
int idx = div_s64(table_offset, c->table_size);
#ifdef ASSERT
s32 rem_table_offset_mod_table_size;
+
div_s64_rem(table_offset, c->table_size, &rem_table_offset_mod_table_size);
ASSERT(idx >= 0 && idx < c->size && rem_table_offset_mod_table_size == 0);
#endif
@@ -47,8 +46,8 @@ static inline int __xloop_file_fmt_qcow_cache_get_table_idx(
return idx;
}
-static inline const char *__xloop_file_fmt_qcow_cache_get_name(
- struct xloop_file_fmt *xlo_fmt, struct xloop_file_fmt_qcow_cache *c)
+static inline const char *__xloop_file_fmt_qcow_cache_get_name(struct xloop_file_fmt *xlo_fmt,
+ struct xloop_file_fmt_qcow_cache *c)
{
struct xloop_file_fmt_qcow_data *qcow_data = xlo_fmt->private_data;
@@ -62,8 +61,8 @@ static inline const char *__xloop_file_fmt_qcow_cache_get_name(
}
}
-struct xloop_file_fmt_qcow_cache *xloop_file_fmt_qcow_cache_create(
- struct xloop_file_fmt *xlo_fmt, int num_tables, unsigned table_size)
+struct xloop_file_fmt_qcow_cache *xloop_file_fmt_qcow_cache_create(struct xloop_file_fmt *xlo_fmt, int num_tables,
+ unsigned int table_size)
{
#ifdef CONFIG_DEBUG_DRIVER
struct xloop_file_fmt_qcow_data *qcow_data = xlo_fmt->private_data;
@@ -76,14 +75,12 @@ struct xloop_file_fmt_qcow_cache *xloop_file_fmt_qcow_cache_create(
ASSERT(table_size <= qcow_data->cluster_size);
c = kzalloc(sizeof(*c), GFP_KERNEL);
- if (!c) {
+ if (!c)
return NULL;
- }
c->size = num_tables;
c->table_size = table_size;
- c->entries = vzalloc(sizeof(struct xloop_file_fmt_qcow_cache_table) *
- num_tables);
+ c->entries = vzalloc(sizeof(struct xloop_file_fmt_qcow_cache_table) * num_tables);
c->table_array = vzalloc(num_tables * c->table_size);
if (!c->entries || !c->table_array) {
@@ -102,30 +99,27 @@ void xloop_file_fmt_qcow_cache_destroy(struct xloop_file_fmt *xlo_fmt)
struct xloop_file_fmt_qcow_cache *c = qcow_data->l2_table_cache;
int i;
- for (i = 0; i < c->size; i++) {
+ for (i = 0; i < c->size; i++)
ASSERT(c->entries[i].ref == 0);
- }
vfree(c->table_array);
vfree(c->entries);
kfree(c);
}
-static int __xloop_file_fmt_qcow_cache_entry_flush(
- struct xloop_file_fmt *xlo_fmt, struct xloop_file_fmt_qcow_cache *c, int i)
+static int __xloop_file_fmt_qcow_cache_entry_flush(struct xloop_file_fmt *xlo_fmt, struct xloop_file_fmt_qcow_cache *c,
+ int i)
{
if (!c->entries[i].dirty || !c->entries[i].offset) {
return 0;
} else {
- dev_err_ratelimited(xloop_file_fmt_to_dev(xlo_fmt), "flush dirty "
- "cache tables is not supported yet\n");
- return -ENOSYS;
+ dev_err_ratelimited(xloop_file_fmt_to_dev(xlo_fmt), "flush dirty cache tables is not supported yet\n");
+ return -EINVAL;
}
}
-static int __xloop_file_fmt_qcow_cache_do_get(struct xloop_file_fmt *xlo_fmt,
- struct xloop_file_fmt_qcow_cache *c, u64 offset, void **table,
- bool read_from_disk)
+static int __xloop_file_fmt_qcow_cache_do_get(struct xloop_file_fmt *xlo_fmt, struct xloop_file_fmt_qcow_cache *c,
+ u64 offset, void **table, bool read_from_disk)
{
struct xloop_device *xlo = xloop_file_fmt_get_xlo(xlo_fmt);
int i;
@@ -140,9 +134,9 @@ static int __xloop_file_fmt_qcow_cache_do_get(struct xloop_file_fmt *xlo_fmt,
ASSERT(offset != 0);
if (!IS_ALIGNED(offset, c->table_size)) {
- dev_err_ratelimited(xloop_file_fmt_to_dev(xlo_fmt), "cannot get entry "
- "from %s cache: offset %llx is unaligned\n",
- __xloop_file_fmt_qcow_cache_get_name(xlo_fmt, c), offset);
+ dev_err_ratelimited(xloop_file_fmt_to_dev(xlo_fmt),
+ "cannot get entry from %s cache: offset %llx is unaligned\n",
+ __xloop_file_fmt_qcow_cache_get_name(xlo_fmt, c), offset);
return -EIO;
}
@@ -151,41 +145,37 @@ static int __xloop_file_fmt_qcow_cache_do_get(struct xloop_file_fmt *xlo_fmt,
div_u64_rem(offset_div_table_size, c->size, &lookup_index);
i = lookup_index;
do {
- const struct xloop_file_fmt_qcow_cache_table *t =
- &c->entries[i];
- if (t->offset == offset) {
+ const struct xloop_file_fmt_qcow_cache_table *t = &c->entries[i];
+
+ if (t->offset == offset)
goto found;
- }
+
if (t->ref == 0 && t->lru_counter < min_lru_counter) {
min_lru_counter = t->lru_counter;
min_lru_index = i;
}
- if (++i == c->size) {
+ if (++i == c->size)
i = 0;
- }
+
} while (i != lookup_index);
if (min_lru_index == -1) {
BUG();
- panic("Oops: This can't happen in current synchronous code, "
- "but leave the check here as a reminder for whoever "
- "starts using AIO with the QCOW cache");
+ panic("Oops: This can't happen in current synchronous code while using AIO with the QCOW cache\n");
}
/* Cache miss: write a table back and replace it */
i = min_lru_index;
ret = __xloop_file_fmt_qcow_cache_entry_flush(xlo_fmt, c, i);
- if (ret < 0) {
+ if (ret < 0)
return ret;
- }
c->entries[i].offset = 0;
if (read_from_disk) {
read_offset = offset;
- len = kernel_read(xlo->xlo_backing_file,
- __xloop_file_fmt_qcow_cache_get_table_addr(c, i),
- c->table_size, &read_offset);
+ len = kernel_read(xlo->xlo_backing_file, __xloop_file_fmt_qcow_cache_get_table_addr(c, i),
+ c->table_size, &read_offset);
if (len < 0) {
len = ret;
return ret;
@@ -202,14 +192,12 @@ found:
return 0;
}
-int xloop_file_fmt_qcow_cache_get(struct xloop_file_fmt *xlo_fmt, u64 offset,
- void **table)
+int xloop_file_fmt_qcow_cache_get(struct xloop_file_fmt *xlo_fmt, u64 offset, void **table)
{
struct xloop_file_fmt_qcow_data *qcow_data = xlo_fmt->private_data;
struct xloop_file_fmt_qcow_cache *c = qcow_data->l2_table_cache;
- return __xloop_file_fmt_qcow_cache_do_get(xlo_fmt, c, offset, table,
- true);
+ return __xloop_file_fmt_qcow_cache_do_get(xlo_fmt, c, offset, table, true);
}
void xloop_file_fmt_qcow_cache_put(struct xloop_file_fmt *xlo_fmt, void **table)
@@ -221,9 +209,8 @@ void xloop_file_fmt_qcow_cache_put(struct xloop_file_fmt *xlo_fmt, void **table)
c->entries[i].ref--;
*table = NULL;
- if (c->entries[i].ref == 0) {
+ if (c->entries[i].ref == 0)
c->entries[i].lru_counter = ++c->lru_counter;
- }
ASSERT(c->entries[i].ref >= 0);
}