mars/kernel/mars_generic.c

452 lines
11 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] = {};
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));
2011-08-12 11:09:48 +00:00
}
//up_read(&uts_sem);
2011-07-22 10:43:40 +00:00
}
return id;
}
EXPORT_SYMBOL_GPL(my_id);
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),
2012-08-01 09:55:32 +00:00
META_INI(ref_checksum, struct mref_object, FIELD_INT),
2011-02-28 18:00:32 +00:00
META_INI(ref_flags, struct mref_object, FIELD_INT),
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-08-06 06:53:28 +00:00
#ifdef MARS_HAS_NEW_CRYPTO
/* Nor now, use shash.
* Later, asynchronous support should be added for full exploitation
* of crypto hardware.
*/
#include <crypto/hash.h>
static struct crypto_shash *mars_tfm = NULL;
int mars_digest_size = 0;
struct mars_sdesc {
struct shash_desc shash;
char ctx[];
};
void mars_digest(unsigned char *digest, void *data, int len)
{
int size = sizeof(struct mars_sdesc) + crypto_shash_descsize(mars_tfm);
struct mars_sdesc *sdesc = brick_mem_alloc(size);
int status;
sdesc->shash.tfm = mars_tfm;
sdesc->shash.flags = 0;
memset(digest, 0, mars_digest_size);
status = crypto_shash_digest(&sdesc->shash, data, len, digest);
if (unlikely(status < 0))
MARS_ERR("cannot calculate cksum on %p len=%d, status=%d\n",
data, len,
status);
brick_mem_free(sdesc);
}
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];
int mars_digest_size = 0;
void mars_digest(unsigned char *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;
memset(digest, 0, mars_digest_size);
// TODO: use per-thread instance, omit locking
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-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)
{
unsigned char checksum[mars_digest_size];
int len;
if (mref->ref_cs_mode <= 0 || !mref->ref_data)
return;
mars_digest(checksum, mref->ref_data, mref->ref_len);
len = sizeof(mref->ref_checksum);
if (len > mars_digest_size)
len = mars_digest_size;
memcpy(&mref->ref_checksum, checksum, len);
}
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(get_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_rw ? '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-07-22 10:43:40 +00:00
void (*_mars_trigger)(void) = NULL;
EXPORT_SYMBOL_GPL(_mars_trigger);
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
{
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(get_ds());
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
mars_tfm = crypto_alloc_shash("md5", 0, 0);
if (unlikely(!mars_tfm) || IS_ERR(mars_tfm)) {
MARS_ERR("cannot alloc crypto hash, status=%ld\n",
PTR_ERR(mars_tfm));
return -ELIBACC;
}
mars_digest_size = crypto_shash_digestsize(mars_tfm);
2019-08-06 06:53:28 +00:00
#else /* MARS_HAS_NEW_CRYPTO */
{
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
mars_digest_size = crypto_hash_digestsize(mars_tfm[0]);
2019-08-06 06:53:28 +00:00
#endif /* MARS_HAS_NEW_CRYPTO */
MARS_INF("digest_size = %d\n", mars_digest_size);
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-08-06 06:53:28 +00:00
#ifdef MARS_HAS_NEW_CRYPTO
if (mars_tfm) {
crypto_free_shash(mars_tfm);
}
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
}