mars/kernel/mars_generic.c

1263 lines
30 KiB
C
Raw Normal View History

2014-11-21 10:51:34 +00:00
/*
* MARS Long Distance Replication Software
*
* This file is part of MARS project: http://schoebel.github.io/mars/
*
* Copyright (C) 2010-2014 Thomas Schoebel-Theuer
* Copyright (C) 2011-2014 1&1 Internet AG
*
* 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.
*/
2010-06-14 14:27:40 +00:00
2011-02-23 20:48:06 +00:00
//#define BRICK_DEBUGGING
//#define MARS_DEBUGGING
2010-06-14 14:27:40 +00:00
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/string.h>
2011-02-23 20:48:06 +00:00
#include <linux/uaccess.h>
#include <linux/file.h>
#include <linux/fs.h>
#include <linux/utsname.h>
2010-06-14 14:27:40 +00:00
#include "mars.h"
2011-03-03 09:02:10 +00:00
#include "mars_client.h"
2011-07-22 10:43:40 +00:00
//////////////////////////////////////////////////////////////
// infrastructure
struct banning mars_global_ban = {};
EXPORT_SYMBOL_GPL(mars_global_ban);
atomic_t mars_global_io_flying = ATOMIC_INIT(0);
EXPORT_SYMBOL_GPL(mars_global_io_flying);
2019-11-11 19:48:40 +00:00
static char id[__NEW_UTS_LEN + 2] = {};
2020-11-26 08:53:22 +00:00
static int id_len = 0;
2011-02-23 20:48:06 +00:00
2019-11-11 19:48:40 +00:00
/* TODO: use MAC addresses (or motherboard IDs etc) for _validation_
* of nodenames.
2011-07-22 10:43:40 +00:00
* When the nodename is misconfigured, data might be scrambled.
* In ideal case, further checks should be added to prohibit accidental
* name clashes.
*/
char *my_id(void)
{
2019-11-11 19:48:40 +00:00
if (unlikely(!id[0])) {
struct new_utsname *u;
2011-08-12 11:09:48 +00:00
//down_read(&uts_sem); // FIXME: this is currenty not EXPORTed from the kernel!
u = utsname();
if (u) {
2019-11-11 19:48:40 +00:00
strncpy(id, u->nodename, sizeof(id));
2020-11-26 08:53:22 +00:00
id_len = strlen(id);
2011-08-12 11:09:48 +00:00
}
//up_read(&uts_sem);
2011-07-22 10:43:40 +00:00
}
return id;
}
2020-11-26 08:53:22 +00:00
int my_id_len(void)
{
return id_len;
}
2011-04-19 14:46:38 +00:00
2011-07-22 10:43:40 +00:00
//////////////////////////////////////////////////////////////
// object stuff
const struct generic_object_type mref_type = {
.object_type_name = "mref",
.default_size = sizeof(struct mref_object),
.object_type_nr = OBJ_TYPE_MREF,
2011-07-22 10:43:40 +00:00
};
EXPORT_SYMBOL_GPL(mref_type);
//////////////////////////////////////////////////////////////
// brick stuff
2011-02-28 18:00:32 +00:00
/////////////////////////////////////////////////////////////////////
// meta descriptions
const struct meta mars_info_meta[] = {
META_INI(current_size, struct mars_info, FIELD_INT),
META_INI(tf_align, struct mars_info, FIELD_INT),
META_INI(tf_min_size, struct mars_info, FIELD_INT),
2011-02-28 18:00:32 +00:00
{}
};
EXPORT_SYMBOL_GPL(mars_info_meta);
const struct meta mars_mref_meta[] = {
META_INI(_object_cb.cb_error, struct mref_object, FIELD_INT),
2011-02-28 18:00:32 +00:00
META_INI(ref_pos, struct mref_object, FIELD_INT),
META_INI(ref_len, struct mref_object, FIELD_INT),
META_INI(ref_may_write, struct mref_object, FIELD_INT),
2011-03-10 11:40:06 +00:00
META_INI(ref_prio, struct mref_object, FIELD_INT),
2012-08-01 09:55:32 +00:00
META_INI(ref_cs_mode, struct mref_object, FIELD_INT),
2011-03-07 10:27:38 +00:00
META_INI(ref_timeout, struct mref_object, FIELD_INT),
2011-03-24 16:05:46 +00:00
META_INI(ref_total_size, struct mref_object, FIELD_INT),
2019-07-24 07:28:17 +00:00
/* QUIRK: for compatibility with the old layout, we have to
* pseudo-split the field.
* TODO: port "make data transfer independent from register size and bytesex"
* and then revert this to its old simple form.
* However, all old instances must have been updated before.
*/
{
__META_INI(ref_checksum, FIELD_RAW,
OLD_MARS_DIGEST_SIZE,
offsetof(struct mref_object, ref_checksum)),
},
{
__META_INI(ref_checksum_pseudo, FIELD_RAW,
MARS_DIGEST_SIZE - OLD_MARS_DIGEST_SIZE,
offsetof(struct mref_object, ref_checksum)
+ OLD_MARS_DIGEST_SIZE),
},
2019-03-14 12:44:16 +00:00
META_INI(ref_flags, struct mref_object, FIELD_UINT),
2011-02-28 18:00:32 +00:00
META_INI(ref_rw, struct mref_object, FIELD_INT),
META_INI(ref_id, struct mref_object, FIELD_INT),
2011-03-24 16:05:46 +00:00
META_INI(ref_skip_sync, struct mref_object, FIELD_INT),
2011-02-28 18:00:32 +00:00
{}
};
EXPORT_SYMBOL_GPL(mars_mref_meta);
2019-02-19 09:18:29 +00:00
const struct meta mars_lamport_time_meta[] = {
META_INI(tv_sec, struct lamport_time, FIELD_INT),
META_INI(tv_nsec, struct lamport_time, FIELD_INT),
2011-02-28 18:00:32 +00:00
{}
};
2019-02-19 09:18:29 +00:00
EXPORT_SYMBOL_GPL(mars_lamport_time_meta);
2011-02-28 18:00:32 +00:00
//////////////////////////////////////////////////////////////
// crypto stuff
2019-07-24 07:28:17 +00:00
#define MD5_DIGEST_SIZE 16
2019-03-26 09:57:33 +00:00
__u32 available_digest_mask = MREF_CHKSUM_MD5_OLD;
__u32 usable_digest_mask = MREF_CHKSUM_MD5_OLD;
__u32 used_log_digest = 0;
__u32 used_net_digest = 0;
2019-08-06 06:53:28 +00:00
#ifdef MARS_HAS_NEW_CRYPTO
2019-03-26 09:57:33 +00:00
/* For now, use shash.
* Later, asynchronous support should be added for full exploitation
* of crypto hardware.
*/
#include <crypto/hash.h>
2019-03-26 09:57:33 +00:00
static struct crypto_shash *md5_tfm = NULL;
2020-04-09 05:45:35 +00:00
#ifdef HAS_CRC32C
#define CRC32C_DIGEST_SIZE 4
static struct crypto_shash *crc32c_tfm = NULL;
#endif
#ifdef HAS_CRC32
#define CRC32_DIGEST_SIZE 4
static struct crypto_shash *crc32_tfm = NULL;
#endif
2019-03-26 13:22:08 +00:00
#ifdef HAS_SHA1
#define SHA1_DIGEST_SIZE 20
static struct crypto_shash *sha1_tfm = NULL;
#endif
struct mars_sdesc {
struct shash_desc shash;
char ctx[];
};
2019-07-24 07:28:17 +00:00
/* Note:
* For compatibility to OLD_MARS_DIGEST_SIZE, the higher
* digest bytes up to MARS_DIGEST_SIZE are not exploited
* in this version.
*/
2019-03-26 09:57:33 +00:00
static
2020-04-30 06:16:53 +00:00
void md5_old_digest(void *digest, const void *data, int len)
{
2019-03-26 09:57:33 +00:00
int size = sizeof(struct mars_sdesc) + crypto_shash_descsize(md5_tfm);
struct mars_sdesc *sdesc = brick_mem_alloc(size);
int status;
2019-03-26 09:57:33 +00:00
sdesc->shash.tfm = md5_tfm;
sdesc->shash.flags = 0;
2019-03-26 09:57:33 +00:00
memset(digest, 0, MARS_DIGEST_SIZE);
status = crypto_shash_digest(&sdesc->shash, data, len, digest);
2019-03-26 09:57:33 +00:00
if (unlikely(status < 0)) {
MARS_ERR("cannot calculate md5 chksum on %p len=%d, status=%d\n",
data, len,
status);
2019-03-26 09:57:33 +00:00
memset(digest, 0, MARS_DIGEST_SIZE);
}
brick_mem_free(sdesc);
}
2019-08-07 07:15:21 +00:00
static
2020-04-30 06:16:53 +00:00
void md5_digest(void *digest, const void *data, int len)
2019-08-07 07:15:21 +00:00
{
int size = sizeof(struct mars_sdesc) + crypto_shash_descsize(md5_tfm);
struct mars_sdesc *sdesc = brick_mem_alloc(size);
const int iterations = MARS_DIGEST_SIZE / MD5_DIGEST_SIZE;
int chunksize = len / iterations;
int offset = 0;
int done_len = len;
int i;
int status;
sdesc->shash.tfm = md5_tfm;
sdesc->shash.flags = 0;
memset(digest, 0, MARS_DIGEST_SIZE);
/* exploit the bigger MARS_DIGEST_SIZE by computing MD5 in chunks */
for (i = 0; i < iterations; i++) {
char this_digest[MD5_DIGEST_SIZE] = {};
status = crypto_shash_digest(&sdesc->shash,
data + offset,
chunksize,
this_digest);
if (unlikely(status < 0)) {
MARS_ERR("cannot calculate md5 chksum on %p len=%d, status=%d\n",
data,
chunksize,
status);
memset(digest, 0, MARS_DIGEST_SIZE);
break;
}
memcpy(digest + i * MD5_DIGEST_SIZE,
this_digest, MD5_DIGEST_SIZE);
offset += chunksize;
done_len -= chunksize;
}
if (unlikely(done_len))
MARS_ERR("md5 chksum remain %d\n", done_len);
brick_mem_free(sdesc);
}
2020-04-09 05:45:35 +00:00
#ifdef HAS_CRC32C
static
2020-04-30 06:16:53 +00:00
void crc32c_digest(void *digest, const void *data, int len)
2020-04-09 05:45:35 +00:00
{
int size = sizeof(struct mars_sdesc) + crypto_shash_descsize(crc32c_tfm);
struct mars_sdesc *sdesc = brick_mem_alloc(size);
const int iterations = MARS_DIGEST_SIZE / CRC32C_DIGEST_SIZE;
int chunksize = len / iterations;
int offset = 0;
int done_len = len;
int i;
int status;
sdesc->shash.tfm = crc32c_tfm;
sdesc->shash.flags = 0;
memset(digest, 0, MARS_DIGEST_SIZE);
/* exploit the bigger MARS_DIGEST_SIZE by computing CRC32C in chunks */
for (i = 0; i < iterations; i++) {
char this_digest[CRC32C_DIGEST_SIZE] = {};
if (i == iterations - 1)
chunksize = done_len;
status = crypto_shash_digest(&sdesc->shash,
data + offset, chunksize,
this_digest);
if (unlikely(status < 0)) {
MARS_ERR("cannot calculate crc32c chksum on %p len=%d, status=%d\n",
data, chunksize,
status);
continue;
}
memcpy(digest + i * CRC32C_DIGEST_SIZE, this_digest, CRC32C_DIGEST_SIZE);
offset += chunksize;
done_len -= chunksize;
}
if (unlikely(done_len))
MARS_ERR("crc32c chksum remain %d\n", done_len);
brick_mem_free(sdesc);
}
#endif
#ifdef HAS_CRC32
static
2020-04-30 06:16:53 +00:00
void crc32_digest(void *digest, const void *data, int len)
2020-04-09 05:45:35 +00:00
{
int size = sizeof(struct mars_sdesc) + crypto_shash_descsize(crc32_tfm);
struct mars_sdesc *sdesc = brick_mem_alloc(size);
const int iterations = MARS_DIGEST_SIZE / CRC32_DIGEST_SIZE;
int chunksize = len / iterations;
int offset = 0;
int done_len = len;
int i;
int status;
sdesc->shash.tfm = crc32_tfm;
sdesc->shash.flags = 0;
memset(digest, 0, MARS_DIGEST_SIZE);
/* exploit the bigger MARS_DIGEST_SIZE by computing CRC32 in chunks */
for (i = 0; i < iterations; i++) {
char this_digest[CRC32_DIGEST_SIZE] = {};
if (i == iterations - 1)
chunksize = done_len;
status = crypto_shash_digest(&sdesc->shash,
data + offset, chunksize,
this_digest);
if (unlikely(status < 0)) {
MARS_ERR("cannot calculate crc32 chksum on %p len=%d, status=%d\n",
data, chunksize,
status);
continue;
}
memcpy(digest + i * CRC32_DIGEST_SIZE, this_digest, CRC32_DIGEST_SIZE);
offset += chunksize;
done_len -= chunksize;
}
if (unlikely(done_len))
MARS_ERR("crc32 chksum remain %d\n", done_len);
brick_mem_free(sdesc);
}
#endif
2019-03-26 13:22:08 +00:00
#ifdef HAS_SHA1
static
2020-04-30 06:16:53 +00:00
void sha1_digest(void *digest, const void *data, int len)
2019-03-26 13:22:08 +00:00
{
int size = sizeof(struct mars_sdesc) + crypto_shash_descsize(sha1_tfm);
struct mars_sdesc *sdesc = brick_mem_alloc(size);
unsigned char tmp[SHA1_DIGEST_SIZE] = {};
int status;
sdesc->shash.tfm = sha1_tfm;
sdesc->shash.flags = 0;
status = crypto_shash_digest(&sdesc->shash, data, len, tmp);
if (unlikely(status < 0)) {
MARS_ERR("cannot calculate sha1 chksum on %p len=%d, status=%d\n",
data, len,
status);
memset(digest, 0, MARS_DIGEST_SIZE);
} else {
memcpy(digest, tmp, SHA1_DIGEST_SIZE);
memset(digest + SHA1_DIGEST_SIZE, 0,
MARS_DIGEST_SIZE - SHA1_DIGEST_SIZE);
}
brick_mem_free(sdesc);
}
#endif
2019-03-26 09:57:33 +00:00
__u32 mars_digest(__u32 digest_flags,
__u32 *used_flags,
void *digest,
2020-04-30 06:16:53 +00:00
const void *data, int len)
2019-03-26 09:57:33 +00:00
{
2019-08-07 07:15:21 +00:00
/* The order defines the preference:
* place the most performant algorithms first.
*/
2020-04-09 05:45:35 +00:00
#ifdef HAS_CRC32C
if (digest_flags & MREF_CHKSUM_CRC32C && crc32c_tfm) {
crc32c_digest(digest, data, len);
if (used_flags)
*used_flags = MREF_CHKSUM_CRC32C;
return MREF_CHKSUM_CRC32C;
}
#endif
#ifdef HAS_CRC32
if (digest_flags & MREF_CHKSUM_CRC32 && crc32_tfm) {
crc32_digest(digest, data, len);
if (used_flags)
*used_flags = MREF_CHKSUM_CRC32;
return MREF_CHKSUM_CRC32;
}
#endif
2019-08-07 07:15:21 +00:00
if (digest_flags & MREF_CHKSUM_MD5 && md5_tfm) {
md5_digest(digest, data, len);
if (used_flags)
*used_flags = MREF_CHKSUM_MD5;
return MREF_CHKSUM_MD5;
}
2019-03-26 13:22:08 +00:00
#ifdef HAS_SHA1
if (digest_flags & MREF_CHKSUM_SHA1 && sha1_tfm) {
sha1_digest(digest, data, len);
if (used_flags)
*used_flags = MREF_CHKSUM_SHA1;
return MREF_CHKSUM_SHA1;
}
#endif
2019-03-26 09:57:33 +00:00
/* always fallback to old md5 regardless of digest_flags */
md5_old_digest(digest, data, len);
if (used_flags)
*used_flags = MREF_CHKSUM_MD5_OLD;
return MREF_CHKSUM_MD5_OLD;
}
2019-08-07 07:15:21 +00:00
#ifdef CONFIG_MARS_BENCHMARK
static
void benchmark_digest(char *name, __u32 flags)
{
unsigned char*testpage = kzalloc(PAGE_SIZE, GFP_KERNEL);
unsigned char old_test[MARS_DIGEST_SIZE] = {};
unsigned char new_test[MARS_DIGEST_SIZE];
long long delta;
__u32 res_flags;
unsigned char bit;
int i;
usable_digest_mask = MREF_CHKSUM_ANY;
delta =
TIME_THIS(
for (bit = 1; bit; bit <<= 1) {
for (i = 0; i < PAGE_SIZE; i++) {
testpage[i] ^= bit;
res_flags = mars_digest(flags,
NULL,
new_test,
testpage,
PAGE_SIZE);
if (unlikely(!(res_flags & flags))) {
MARS_ERR("digest %s failed\n",
name);
goto err;
}
if (unlikely(!memcmp(old_test, new_test, MARS_DIGEST_SIZE))) {
MARS_ERR("digest %s is not good enough\n",
name);
goto err;
}
memcpy(old_test, new_test, MARS_DIGEST_SIZE);
}
}
);
printk("%-10s digest duration = %12lld ns\n",
name, delta);
err:
kfree(testpage);
usable_digest_mask = MREF_CHKSUM_MD5_OLD;
}
#endif
2019-03-26 09:57:33 +00:00
static
int init_mars_digest(void)
{
int status;
md5_tfm = crypto_alloc_shash("md5", 0, 0);
if (unlikely(!md5_tfm) || IS_ERR(md5_tfm)) {
MARS_ERR("cannot alloc crypto hash, status=%ld\n",
PTR_ERR(md5_tfm));
md5_tfm = NULL;
return -ELIBACC;
}
status = crypto_shash_digestsize(md5_tfm);
2019-07-24 07:28:17 +00:00
if (unlikely(status != MD5_DIGEST_SIZE)) {
2019-03-26 09:57:33 +00:00
MARS_ERR("md5 bad digest size %d\n", status);
return -ELIBACC;
}
2019-08-07 07:15:21 +00:00
available_digest_mask |= MREF_CHKSUM_MD5;
2019-03-26 09:57:33 +00:00
2020-04-09 05:45:35 +00:00
#ifdef HAS_CRC32C
crc32c_tfm = crypto_alloc_shash("crc32c", 0, 0);
if (unlikely(!crc32c_tfm) || IS_ERR(crc32c_tfm)) {
MARS_ERR("cannot alloc crc32c crypto hash, status=%ld\n",
PTR_ERR(crc32c_tfm));
crc32c_tfm = NULL;
} else {
status = crypto_shash_digestsize(crc32c_tfm);
if (unlikely(status != CRC32C_DIGEST_SIZE)) {
MARS_ERR("crc32c bad digest size %d\n", status);
return -ELIBACC;
}
available_digest_mask |= MREF_CHKSUM_CRC32C;
}
#endif
#ifdef HAS_CRC32
crc32_tfm = crypto_alloc_shash("crc32", 0, 0);
if (unlikely(!crc32_tfm) || IS_ERR(crc32_tfm)) {
MARS_ERR("cannot alloc crc32 crypto hash, status=%ld\n",
PTR_ERR(crc32_tfm));
crc32_tfm = NULL;
} else {
status = crypto_shash_digestsize(crc32_tfm);
if (unlikely(status != CRC32_DIGEST_SIZE)) {
MARS_ERR("crc32 bad digest size %d\n", status);
return -ELIBACC;
}
available_digest_mask |= MREF_CHKSUM_CRC32;
}
#endif
2019-03-26 13:22:08 +00:00
#ifdef HAS_SHA1
sha1_tfm = crypto_alloc_shash("sha1", 0, 0);
if (unlikely(!sha1_tfm) || IS_ERR(sha1_tfm)) {
MARS_ERR("cannot alloc crypto hash, status=%ld\n",
PTR_ERR(sha1_tfm));
sha1_tfm = NULL;
} else {
status = crypto_shash_digestsize(sha1_tfm);
if (unlikely(status != SHA1_DIGEST_SIZE)) {
MARS_ERR("sha1 bad digest size %d\n", status);
return -ELIBACC;
}
available_digest_mask |= MREF_CHKSUM_SHA1;
}
#endif
2019-08-07 07:15:21 +00:00
#ifdef CONFIG_MARS_BENCHMARK
2020-04-09 05:45:35 +00:00
#ifdef HAS_CRC32C
benchmark_digest("crc32c", MREF_CHKSUM_CRC32C);
#endif
#ifdef HAS_CRC32
benchmark_digest("crc32", MREF_CHKSUM_CRC32);
2019-03-26 13:22:08 +00:00
#endif
#ifdef HAS_SHA1
benchmark_digest("sha1", MREF_CHKSUM_SHA1);
2020-04-09 05:45:35 +00:00
#endif
2019-08-07 07:15:21 +00:00
benchmark_digest("md5old", MREF_CHKSUM_MD5_OLD);
benchmark_digest("md5", MREF_CHKSUM_MD5);
#endif
2019-03-26 09:57:33 +00:00
return 0;
}
static
void exit_mars_digest(void)
{
if (md5_tfm) {
crypto_free_shash(md5_tfm);
}
2020-04-09 05:45:35 +00:00
#ifdef HAS_CRC32C
if (crc32c_tfm) {
crypto_free_shash(crc32c_tfm);
}
#endif
#ifdef HAS_CRC32
if (crc32_tfm) {
crypto_free_shash(crc32_tfm);
}
#endif
2019-03-26 13:22:08 +00:00
#ifdef HAS_SHA1
if (sha1_tfm) {
crypto_free_shash(sha1_tfm);
}
#endif
2019-03-26 09:57:33 +00:00
}
2019-08-06 06:53:28 +00:00
#else /* MARS_HAS_NEW_CRYPTO */
/* Old implementation, to disappear.
* Was a quick'n dirty lab prototype with unnecessary
* global variables and locking.
*/
2017-05-17 13:35:29 +00:00
#define OBSOLETE_TFM_MAX 128
static struct crypto_hash *mars_tfm[OBSOLETE_TFM_MAX];
static struct semaphore tfm_sem[OBSOLETE_TFM_MAX];
2019-03-26 09:57:33 +00:00
__u32 mars_digest(__u32 digest_flags,
__u32 *used_flags,
void *digest,
void *data, int len)
{
static unsigned int round_robin = 0;
unsigned int i = round_robin++ % OBSOLETE_TFM_MAX;
struct hash_desc desc = {
.tfm = mars_tfm[i],
.flags = 0,
};
struct scatterlist sg;
2019-03-26 09:57:33 +00:00
memset(digest, 0, MARS_DIGEST_SIZE);
down(&tfm_sem[i]);
crypto_hash_init(&desc);
sg_init_table(&sg, 1);
sg_set_buf(&sg, data, len);
crypto_hash_update(&desc, &sg, sg.length);
crypto_hash_final(&desc, digest);
up(&tfm_sem[i]);
2019-03-26 09:57:33 +00:00
if (used_flags)
*used_flags = MREF_CHKSUM_MD5_OLD;
return MREF_CHKSUM_MD5_OLD;
}
2019-08-06 06:53:28 +00:00
#endif /* MARS_HAS_NEW_CRYPTO */
2012-08-01 10:09:49 +00:00
void mref_checksum(struct mref_object *mref)
{
2019-03-26 09:57:33 +00:00
unsigned char checksum[MARS_DIGEST_SIZE];
2019-03-26 09:57:33 +00:00
__u32 digest_flags;
2012-08-01 10:09:49 +00:00
int len;
2019-03-26 09:57:33 +00:00
digest_flags = mref->ref_flags & MREF_CHKSUM_ANY;
if (!digest_flags || !mref->ref_data)
2012-08-01 10:09:49 +00:00
return;
2019-03-26 09:57:33 +00:00
digest_flags =
mars_digest(digest_flags,
&used_net_digest,
checksum,
mref->ref_data, mref->ref_len);
2019-03-26 09:57:33 +00:00
2019-03-26 09:57:33 +00:00
mref->ref_flags = (mref->ref_flags & ~MREF_CHKSUM_ANY) | digest_flags;
2012-08-01 10:09:49 +00:00
len = sizeof(mref->ref_checksum);
2019-03-26 09:57:33 +00:00
if (len > MARS_DIGEST_SIZE)
len = MARS_DIGEST_SIZE;
2012-08-01 10:09:49 +00:00
memcpy(&mref->ref_checksum, checksum, len);
}
2019-07-25 06:55:17 +00:00
/*******************************************************************/
/* compression */
int compress_overhead = 0;
__u32 available_compression_mask =
2019-07-25 06:55:17 +00:00
#ifdef HAS_LZO
MREF_COMPRESS_LZO |
2019-07-31 12:40:52 +00:00
#endif
#ifdef HAS_LZ4
MREF_COMPRESS_LZ4 |
2020-03-29 16:17:48 +00:00
#endif
#ifdef HAS_ZLIB
MREF_COMPRESS_ZLIB |
2019-07-25 06:55:17 +00:00
#endif
2019-07-25 06:55:17 +00:00
0;
__u32 usable_compression_mask = 0;
__u32 used_compression = 0;
2020-03-29 16:17:48 +00:00
int mars_zlib_compression_level = 3;
2019-07-25 06:55:17 +00:00
int mars_compress(void *src_data,
int src_len,
void *dst_data,
int dst_len,
__u32 check_flags,
__u32 *result_flags)
{
void *tmp_buf = dst_data;
int res = 0;
check_flags &= usable_compression_mask;
if (!(check_flags & MREF_COMPRESS_ANY)) {
used_compression = 0;
return 0;
}
if (unlikely(src_len > MARS_MAX_COMPR_SIZE)) {
MARS_ERR("tryping to compress %d, more than %ld bytes\n",
src_len, MARS_MAX_COMPR_SIZE);
goto done;
}
2019-07-31 12:40:52 +00:00
/* The order determines the preferences */
2019-07-25 06:55:17 +00:00
#ifdef HAS_LZO
if (check_flags & MREF_COMPRESS_LZO) {
int max_len = lzo1x_worst_compress(src_len);
void *wrkmem;
size_t res_len = 0;
int status;
if (!dst_data) {
tmp_buf = brick_mem_alloc(max_len);
} else if (dst_len < max_len) {
MARS_ERR("LZO compression buffer too small: %d < %d\n",
dst_len, max_len);
return 0;
}
wrkmem = brick_mem_alloc(LZO1X_1_MEM_COMPRESS);
status = lzo1x_1_compress(src_data, src_len,
tmp_buf, &res_len, wrkmem);
/* ensure that the result is really smaller */
if (status == LZO_E_OK &&
res_len > 0 &&
res_len < src_len) {
used_compression = MREF_COMPRESS_LZO;
*result_flags |= MREF_COMPRESS_LZO;
res = res_len;
/*
* TODO: avoid memcpy() by swizzling the src_data pointer
*/
if (!dst_data)
memcpy(src_data, tmp_buf, res_len);
}
brick_mem_free(wrkmem);
/* do not try other compression methods */
goto done;
}
2019-07-31 12:40:52 +00:00
#endif
#ifdef HAS_LZ4
if (check_flags & MREF_COMPRESS_LZ4) {
2019-08-05 12:28:05 +00:00
#ifdef HAS_FAST_LZ4
size_t max_len = LZ4_COMPRESSBOUND(src_len);
#else
2019-07-31 12:40:52 +00:00
size_t max_len = lz4_compressbound(src_len);
2019-08-05 12:28:05 +00:00
#endif
2019-07-31 12:40:52 +00:00
size_t res_len = 0;
void *wrkmem;
int status;
if (!dst_data) {
tmp_buf = brick_mem_alloc(max_len);
} else if (dst_len < max_len) {
MARS_ERR("LZ4 compression buffer too small: %d < %lu\n",
dst_len, max_len);
return 0;
}
wrkmem = brick_block_alloc(0, LZ4_MEM_COMPRESS);
2019-08-05 12:28:05 +00:00
#ifdef HAS_FAST_LZ4
res_len = LZ4_compress_fast(src_data,
tmp_buf,
src_len,
max_len,
LZ4_ACCELERATION_DEFAULT,
wrkmem);
status = 0;
#else
2019-07-31 12:40:52 +00:00
status = lz4_compress(src_data, src_len,
tmp_buf, &res_len,
wrkmem);
2019-08-05 12:28:05 +00:00
#endif
2019-07-31 12:40:52 +00:00
if (likely(!status && res_len > 0 && res_len < src_len)) {
used_compression = MREF_COMPRESS_LZ4;
*result_flags |= MREF_COMPRESS_LZ4;
res = res_len;
/*
* TODO: avoid memcpy() by swizzling the src_data pointer
*/
if (!dst_data)
memcpy(src_data, tmp_buf, res_len);
}
brick_block_free(wrkmem, LZ4_MEM_COMPRESS);
/* do not try other compression methods */
goto done;
}
2020-03-29 16:17:48 +00:00
#endif
#ifdef HAS_ZLIB
if (check_flags & MREF_COMPRESS_ZLIB) {
size_t zlib_deflate_wrk_size = zlib_deflate_workspacesize(MAX_WBITS, MAX_MEM_LEVEL);
struct z_stream_s stream = {
.workspace = brick_mem_alloc(zlib_deflate_wrk_size),
};
int status;
if (!dst_data) {
tmp_buf = brick_mem_alloc(src_len);
} else if (dst_len < src_len) {
MARS_ERR("ZLIB compression buffer too small: %d < %d\n",
dst_len, src_len);
return 0;
}
status = zlib_deflateInit(&stream, mars_zlib_compression_level);
if (unlikely(status != Z_OK)) {
MARS_ERR("cannot init zlib compression stream\n");
goto zlib_err;
}
stream.next_in = src_data;
stream.avail_in = src_len;
stream.next_out = tmp_buf;
stream.avail_out = src_len;
status = zlib_deflate(&stream, Z_FINISH);
if (status != Z_STREAM_END)
goto zlib_err;
status = zlib_deflateEnd(&stream);
if (status == Z_OK && stream.total_out < src_len) {
used_compression = MREF_COMPRESS_ZLIB;
*result_flags |= MREF_COMPRESS_ZLIB;
res = stream.total_out;
/*
* TODO: avoid memcpy() by swizzling the src_data pointer
*/
if (!dst_data)
memcpy(src_data, tmp_buf, stream.total_out);
}
zlib_err:
brick_mem_free(stream.workspace);
/* do not try other compression methods */
goto done;
}
2019-07-25 06:55:17 +00:00
#endif
used_compression = 0;
2019-07-25 06:55:17 +00:00
done:
if (!dst_data)
brick_mem_free(tmp_buf);
return res;
}
void *mars_decompress(void *src_data,
int src_len,
void *dst_data,
int dst_len,
__u32 check_flags)
{
void *res_buf = dst_data;
if (!res_buf)
res_buf = brick_mem_alloc(dst_len);
2019-07-25 06:55:17 +00:00
#ifdef HAS_LZO
if (check_flags & MREF_COMPRESS_LZO) {
size_t res_len = dst_len;
int status;
status = lzo1x_decompress_safe(src_data, src_len,
res_buf, &res_len);
if (status == LZO_E_OK && dst_len == res_len)
goto done;
MARS_ERR("bad LZO decompression from %d to %ld bytes (requested %d)\n",
src_len, res_len, dst_len);
goto err;
}
#endif
2019-07-31 12:40:52 +00:00
#ifdef HAS_LZ4
if (check_flags & MREF_COMPRESS_LZ4) {
size_t new_len = src_len;
2019-08-05 12:28:05 +00:00
int status = 0;
#ifdef HAS_FAST_LZ4
new_len = LZ4_decompress_safe(src_data,
res_buf,
src_len,
dst_len);
if (!status && new_len == dst_len)
goto done;
2019-07-31 12:40:52 +00:00
2019-08-05 12:28:05 +00:00
MARS_ERR("bad LZ4 decompression %d to %lu != %d bytes\n",
src_len, new_len, dst_len);
#else
2019-07-31 12:40:52 +00:00
status = lz4_decompress(src_data, &new_len,
res_buf, dst_len);
if (!status && new_len == src_len)
goto done;
MARS_ERR("bad LZ4 decompression %d != %lu to %d bytes\n",
src_len, new_len, dst_len);
2019-08-05 12:28:05 +00:00
#endif
2019-07-31 12:40:52 +00:00
goto err;
}
#endif
2020-03-29 16:17:48 +00:00
#ifdef HAS_ZLIB
if (check_flags & MREF_COMPRESS_ZLIB) {
size_t zlib_inflate_wrk_size = zlib_inflate_workspacesize();
struct z_stream_s stream = {
.workspace = brick_mem_alloc(zlib_inflate_wrk_size),
};
int status;
status = zlib_inflateInit(&stream);
if (unlikely(status != Z_OK)) {
MARS_ERR("cannot init zlib decompression stream\n");
goto zlib_err;
}
stream.next_in = src_data;
stream.avail_in = src_len;
stream.next_out = res_buf;
stream.avail_out = dst_len;
status = zlib_inflate(&stream, Z_FINISH);
if (unlikely(status != Z_STREAM_END)) {
MARS_ERR("bad ZLIB decompression %d (requested %d)\n",
src_len, dst_len);
goto zlib_err;
}
status = zlib_inflateEnd(&stream);
if (likely(status == Z_OK)) {
brick_mem_free(stream.workspace);
goto done;
}
MARS_ERR("unfinished ZLIB decompression %d (requested %d)\n",
src_len, dst_len);
zlib_err:
brick_mem_free(stream.workspace);
goto err;
}
#endif
2019-07-25 06:55:17 +00:00
2019-07-25 06:55:17 +00:00
MARS_ERR("decompression not compiled into kernel module\n");
err:
if (!dst_data)
brick_mem_free(res_buf);
res_buf = NULL;
done:
return res_buf;
}
#ifdef CONFIG_MARS_BENCHMARK
#define MARS_CLEAN_SIZE 256
static
void make_fake_page(__u32 *testpage)
{
int i;
/* some fake compression data */
for (i = 0; i < PAGE_SIZE / sizeof(__u32); i++)
testpage[i] = (__u32)i;
}
static
void benchmark_compress(char *name, __u32 flags)
{
void *testpage = kmalloc(PAGE_SIZE, GFP_KERNEL);
__u32 result_flags;
long long delta;
int status;
int i;
usable_compression_mask = MREF_COMPRESS_ANY;
make_fake_page(testpage);
delta = TIME_THIS(
for (i = 0; i < 10000; i++) {
memset(testpage, 0, MARS_CLEAN_SIZE);
result_flags = 0;
status =
mars_compress(testpage, PAGE_SIZE,
NULL, PAGE_SIZE + compress_overhead,
flags, &result_flags);
if (unlikely(status <= 0) || !(flags & result_flags)) {
MARS_ERR("%s compress failure, status=%d, flags=%x\n",
name, status, result_flags);
goto err;
}
}
);
printk("%-8s compress duration = %12lld ns\n", name, delta);
err:
kfree(testpage);
usable_compression_mask = 0;
}
#endif
static
int init_mars_compress(void)
{
int max_len = 0;
2019-07-25 06:55:17 +00:00
#ifdef HAS_LZO
max_len = lzo1x_worst_compress(MARS_MAX_COMPR_SIZE) - MARS_MAX_COMPR_SIZE;
if (max_len > compress_overhead)
compress_overhead = max_len;
#endif
2019-07-31 12:40:52 +00:00
#ifdef HAS_LZ4
2019-08-05 12:28:05 +00:00
#ifdef HAS_FAST_LZ4
max_len = LZ4_COMPRESSBOUND(MARS_MAX_COMPR_SIZE) - MARS_MAX_COMPR_SIZE;
#else
2019-07-31 12:40:52 +00:00
max_len = lz4_compressbound(MARS_MAX_COMPR_SIZE) - MARS_MAX_COMPR_SIZE;
2019-08-05 12:28:05 +00:00
#endif
2019-07-31 12:40:52 +00:00
if (max_len > compress_overhead)
compress_overhead = max_len;
#endif
2019-07-25 06:55:17 +00:00
2019-07-25 06:55:17 +00:00
#ifdef CONFIG_MARS_BENCHMARK
2019-07-25 06:55:17 +00:00
#ifdef HAS_LZO
benchmark_compress("lzo", MREF_COMPRESS_LZO);
2019-07-31 12:40:52 +00:00
#endif
#ifdef HAS_LZ4
benchmark_compress("lz4", MREF_COMPRESS_LZ4);
2020-03-29 16:17:48 +00:00
#endif
#ifdef HAS_ZLIB
benchmark_compress("zlib", MREF_COMPRESS_ZLIB);
2019-07-25 06:55:17 +00:00
#endif
2019-07-25 06:55:17 +00:00
(void)benchmark_compress;
#endif
return 0;
}
static
void exit_mars_compress(void)
{
}
2011-02-28 18:00:32 +00:00
/////////////////////////////////////////////////////////////////////
2011-03-27 15:18:38 +00:00
// tracing
#ifdef MARS_TRACING
2011-03-29 14:40:40 +00:00
unsigned long long start_trace_clock = 0;
EXPORT_SYMBOL_GPL(start_trace_clock);
2011-03-27 15:18:38 +00:00
struct file *mars_log_file = NULL;
loff_t mars_log_pos = 0;
void _mars_log(char *buf, int len)
{
static DEFINE_MUTEX(trace_lock);
2019-02-21 12:54:36 +00:00
#ifdef MARS_HAS_KERNEL_READ
mutex_lock(&trace_lock);
(void)kernel_write(mars_log_file,
buf,
len,
&mars_log_pos);
mutex_unlock(&trace_lock);
#else
2011-03-27 15:18:38 +00:00
mm_segment_t oldfs;
oldfs = get_fs();
set_fs(KERNEL_DS);
mutex_lock(&trace_lock);
2011-03-27 15:18:38 +00:00
vfs_write(mars_log_file, buf, len, &mars_log_pos);
mutex_unlock(&trace_lock);
2011-03-27 15:18:38 +00:00
set_fs(oldfs);
2019-02-21 12:54:36 +00:00
#endif
2011-03-27 15:18:38 +00:00
}
EXPORT_SYMBOL_GPL(_mars_log);
void mars_log(const char *fmt, ...)
{
2011-08-25 10:16:32 +00:00
char *buf = brick_string_alloc(0);
2011-03-27 15:18:38 +00:00
va_list args;
int len;
if (!buf)
return;
va_start(args, fmt);
len = vscnprintf(buf, PAGE_SIZE, fmt, args);
2011-03-27 15:18:38 +00:00
va_end(args);
_mars_log(buf, len);
2011-08-12 11:09:48 +00:00
brick_string_free(buf);
2011-03-27 15:18:38 +00:00
}
EXPORT_SYMBOL_GPL(mars_log);
void mars_trace(struct mref_object *mref, const char *info)
{
int index = mref->ref_traces;
2011-03-29 14:40:40 +00:00
if (likely(index < MAX_TRACES)) {
mref->ref_trace_stamp[index] = cpu_clock(raw_smp_processor_id());
2011-03-27 15:18:38 +00:00
mref->ref_trace_info[index] = info;
mref->ref_traces++;
}
}
EXPORT_SYMBOL_GPL(mars_trace);
void mars_log_trace(struct mref_object *mref)
{
2011-08-25 10:16:32 +00:00
char *buf = brick_string_alloc(0);
2011-03-29 14:40:40 +00:00
unsigned long long old;
unsigned long long diff;
2011-03-27 15:18:38 +00:00
int i;
int len;
if (!buf) {
return;
}
if (!mars_log_file || !mref->ref_traces) {
goto done;
}
2011-03-29 14:40:40 +00:00
if (!start_trace_clock) {
start_trace_clock = mref->ref_trace_stamp[0];
2011-03-27 15:18:38 +00:00
}
2011-03-29 14:40:40 +00:00
diff = mref->ref_trace_stamp[mref->ref_traces-1] - mref->ref_trace_stamp[0];
2011-03-27 15:18:38 +00:00
len = scnprintf(buf, PAGE_SIZE, "%c ;%12lld ;%6d;%10llu",
mref->ref_flags & MREF_WRITE ? 'W' : 'R',
mref->ref_pos, mref->ref_len, diff / 1000);
2011-03-27 15:18:38 +00:00
2011-03-29 14:40:40 +00:00
old = start_trace_clock;
2011-03-27 15:18:38 +00:00
for (i = 0; i < mref->ref_traces; i++) {
2011-03-29 14:40:40 +00:00
diff = mref->ref_trace_stamp[i] - old;
2011-03-27 15:18:38 +00:00
len += scnprintf(buf + len, PAGE_SIZE - len, " ; %s ;%10llu", mref->ref_trace_info[i], diff / 1000);
2011-03-27 15:18:38 +00:00
old = mref->ref_trace_stamp[i];
}
len +=scnprintf(buf + len, PAGE_SIZE - len, "\n");
2011-03-27 15:18:38 +00:00
_mars_log(buf, len);
done:
2011-08-12 11:09:48 +00:00
brick_string_free(buf);
2011-03-29 14:40:40 +00:00
mref->ref_traces = 0;
2011-03-27 15:18:38 +00:00
}
EXPORT_SYMBOL_GPL(mars_log_trace);
#endif // MARS_TRACING
/////////////////////////////////////////////////////////////////////
2011-07-22 10:43:40 +00:00
// power led handling
2011-03-23 17:58:02 +00:00
void mars_power_led_on(struct mars_brick *brick, bool val)
2011-02-23 20:48:06 +00:00
{
bool oldval = brick->power.led_on;
if (val != oldval) {
2011-07-22 10:43:40 +00:00
//MARS_DBG("brick '%s' type '%s' led_on %d -> %d\n", brick->brick_path, brick->type->type_name, oldval, val);
2011-02-23 20:48:06 +00:00
set_led_on(&brick->power, val);
mars_trigger();
}
}
EXPORT_SYMBOL_GPL(mars_power_led_on);
void mars_power_led_off(struct mars_brick *brick, bool val)
2011-02-23 20:48:06 +00:00
{
bool oldval = brick->power.led_off;
if (val != oldval) {
2011-07-22 10:43:40 +00:00
//MARS_DBG("brick '%s' type '%s' led_off %d -> %d\n", brick->brick_path, brick->type->type_name, oldval, val);
2011-02-23 20:48:06 +00:00
set_led_off(&brick->power, val);
mars_trigger();
}
}
EXPORT_SYMBOL_GPL(mars_power_led_off);
2010-07-30 11:50:20 +00:00
2011-02-23 20:48:06 +00:00
/////////////////////////////////////////////////////////////////////
// init stuff
2011-03-02 09:30:56 +00:00
struct mm_struct *mm_fake = NULL;
EXPORT_SYMBOL_GPL(mm_fake);
2013-01-14 20:02:24 +00:00
struct task_struct *mm_fake_task = NULL;
atomic_t mm_fake_count = ATOMIC_INIT(0);
EXPORT_SYMBOL_GPL(mm_fake_count);
2011-03-02 09:30:56 +00:00
2011-08-25 10:16:32 +00:00
int __init init_mars(void)
2010-06-14 14:27:40 +00:00
{
2019-03-26 09:57:33 +00:00
int status;
2011-02-23 20:48:06 +00:00
MARS_INF("init_mars()\n");
2011-05-13 11:19:28 +00:00
2011-03-02 09:30:56 +00:00
set_fake();
2011-05-13 11:19:28 +00:00
2011-03-27 15:18:38 +00:00
#ifdef MARS_TRACING
{
int flags = O_CREAT | O_TRUNC | O_RDWR | O_LARGEFILE;
int prot = 0600;
mm_segment_t oldfs;
oldfs = get_fs();
set_fs(KERNEL_DS);
2011-03-27 15:18:38 +00:00
mars_log_file = filp_open("/mars/trace.csv", flags, prot);
set_fs(oldfs);
if (IS_ERR(mars_log_file)) {
MARS_ERR("cannot create trace logfile, status = %ld\n", PTR_ERR(mars_log_file));
mars_log_file = NULL;
}
}
#endif
2019-08-06 06:53:28 +00:00
#ifdef MARS_HAS_NEW_CRYPTO
2019-03-26 09:57:33 +00:00
status = init_mars_digest();
if (unlikely(status))
return status;
2019-08-06 06:53:28 +00:00
#else /* MARS_HAS_NEW_CRYPTO */
2019-03-26 09:57:33 +00:00
{
int i;
for (i = 0; i < OBSOLETE_TFM_MAX; i++) {
sema_init(&tfm_sem[i], 1);
mars_tfm[i] = crypto_alloc_hash("md5", 0, CRYPTO_ALG_ASYNC);
if (!mars_tfm[i]) {
MARS_ERR("cannot alloc crypto hash\n");
return -ENOMEM;
}
if (IS_ERR(mars_tfm)) {
MARS_ERR("alloc crypto hash failed, status = %d\n", (int)PTR_ERR(mars_tfm));
return PTR_ERR(mars_tfm);
}
}
}
#if 0
if (crypto_tfm_alg_type(crypto_hash_tfm(mars_tfm)) != CRYPTO_ALG_TYPE_DIGEST) {
MARS_ERR("bad crypto hash type\n");
return -EINVAL;
}
#endif
2019-03-26 09:57:33 +00:00
status = crypto_hash_digestsize(mars_tfm[0]);
MARS_INF("digest_size = %d\n", status);
if (unlikely(status != MARS_DIGEST_SIZE)) {
MARS_ERR("bad md5 crypto hash size %d\n", status);
return -EINVAL;
}
2019-08-06 06:53:28 +00:00
#endif /* MARS_HAS_NEW_CRYPTO */
2019-07-25 06:55:17 +00:00
init_mars_compress();
2010-06-14 14:27:40 +00:00
return 0;
}
2014-04-23 11:16:26 +00:00
void exit_mars(void)
2010-06-14 14:27:40 +00:00
{
2011-02-23 20:48:06 +00:00
MARS_INF("exit_mars()\n");
2011-03-02 09:30:56 +00:00
put_fake();
2019-07-25 06:55:17 +00:00
exit_mars_compress();
2019-08-06 06:53:28 +00:00
#ifdef MARS_HAS_NEW_CRYPTO
2019-03-26 09:57:33 +00:00
exit_mars_digest();
2019-08-06 06:53:28 +00:00
#else /* MARS_HAS_NEW_CRYPTO */
if (mars_tfm[0]) {
int i;
for (i = 0; i < OBSOLETE_TFM_MAX; i++)
crypto_free_hash(mars_tfm[i]);
}
2019-08-06 06:53:28 +00:00
#endif /* MARS_HAS_NEW_CRYPTO */
2011-03-27 15:18:38 +00:00
#ifdef MARS_TRACING
if (mars_log_file) {
filp_close(mars_log_file, NULL);
mars_log_file = NULL;
}
#endif
2010-06-14 14:27:40 +00:00
}