mars/sy_old/mars_light.c

3104 lines
78 KiB
C
Raw Normal View History

2011-02-23 20:48:06 +00:00
// (c) 2011 Thomas Schoebel-Theuer / 1&1 Internet AG
//#define BRICK_DEBUGGING
//#define MARS_DEBUGGING
2011-03-02 09:30:56 +00:00
//#define IO_DEBUGGING
//#define STAT_DEBUGGING // here means: display full statistics
2011-02-23 20:48:06 +00:00
2011-08-31 11:42:04 +00:00
// disable this only for debugging!
#define RUN_PEERS
#define RUN_DATA
#define RUN_LOGINIT
#define RUN_PRIMARY
#define RUN_SYNCSTATUS
#define RUN_LOGFILES
#define RUN_REPLAY
#define RUN_DEVICE
2011-02-23 20:48:06 +00:00
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/string.h>
#include <linux/debug_locks.h>
#include <linux/major.h>
#include <linux/genhd.h>
#include <linux/blkdev.h>
2011-07-22 10:43:40 +00:00
#include "strategy.h"
2011-02-23 20:48:06 +00:00
#include <linux/kthread.h>
#include <linux/wait.h>
// used brick types
2011-07-22 10:43:40 +00:00
#include "../mars_server.h"
#include "../mars_client.h"
#include "../mars_copy.h"
#include "../mars_bio.h"
#include "../mars_aio.h"
#include "../mars_trans_logger.h"
#include "../mars_if.h"
2011-08-25 10:16:32 +00:00
#include "mars_proc.h"
2011-02-23 20:48:06 +00:00
2011-03-22 14:36:26 +00:00
#if 0
#define inline __attribute__((__noinline__))
#endif
2011-03-10 11:40:06 +00:00
2011-02-23 20:48:06 +00:00
static struct task_struct *main_thread = NULL;
2011-02-28 18:00:32 +00:00
typedef int (*light_worker_fn)(void *buf, struct mars_dent *dent);
2011-02-23 20:48:06 +00:00
struct light_class {
char *cl_name;
int cl_len;
char cl_type;
bool cl_hostcontext;
bool cl_serial;
int cl_father;
light_worker_fn cl_forward;
light_worker_fn cl_backward;
};
2011-03-10 11:40:06 +00:00
///////////////////////////////////////////////////////////////////////
// TUNING
2011-06-30 13:15:52 +00:00
#define CONF_TRANS_SHADOW_LIMIT (65536 * 1) // don't fill the hashtable too much
2011-03-18 13:15:40 +00:00
#define CONF_TRANS_CHUNKSIZE (128 * 1024)
//#define CONF_TRANS_ALIGN 512
#define CONF_TRANS_ALIGN 0
2011-05-19 11:36:00 +00:00
//#define FLUSH_DELAY (HZ / 100 + 1)
#define FLUSH_DELAY 0
2011-03-18 13:15:40 +00:00
2011-03-29 14:40:40 +00:00
//#define TRANS_FAKE
2011-03-18 13:15:40 +00:00
2011-05-26 14:32:32 +00:00
#define CONF_TRANS_BATCHLEN 1024
2011-04-29 09:36:10 +00:00
//#define CONF_TRANS_FLYING 4
#define CONF_TRANS_FLYING 128
2011-03-29 14:40:40 +00:00
#define CONF_TRANS_PRIO MARS_PRIO_HIGH
2011-05-13 11:19:28 +00:00
#define CONF_TRANS_LOG_READS false
//#define CONF_TRANS_LOG_READS true
#define CONF_TRANS_MINIMIZE_LATENCY false
//#define CONF_TRANS_MINIMIZE_LATENCY true
2011-05-26 14:32:32 +00:00
//#define CONF_TRANS_COMPLETION_SEMANTICS 2
#define CONF_TRANS_COMPLETION_SEMANTICS 0
2011-03-10 11:40:06 +00:00
2011-04-08 09:52:46 +00:00
//#define CONF_ALL_BATCHLEN 2
#define CONF_ALL_BATCHLEN 1
2011-06-30 13:15:52 +00:00
#define CONF_ALL_FLYING 4
//#define CONF_ALL_FLYING 1
2011-03-29 14:40:40 +00:00
#define CONF_ALL_CONTENTION 0
#define CONF_ALL_PRESSURE 0
2011-05-19 11:36:00 +00:00
#define CONF_ALL_PRIO MARS_PRIO_NORMAL
2011-03-18 13:15:40 +00:00
#define CONF_ALL_MAX_QUEUE 10000
#define CONF_ALL_MAX_JIFFIES (180 * HZ)
2011-03-11 13:57:54 +00:00
2011-03-24 16:05:46 +00:00
#define IF_SKIP_SYNC true
2011-03-30 12:02:50 +00:00
#define IF_MAX_PLUGGED 10000
2011-06-10 13:57:52 +00:00
#define IF_READAHEAD 0
//#define IF_READAHEAD 1
2011-05-06 10:25:52 +00:00
2011-06-10 13:57:52 +00:00
#define BIO_READAHEAD 0
//#define BIO_READAHEAD 1
2011-05-06 10:25:52 +00:00
#define BIO_NOIDLE true
#define BIO_SYNC true
#define BIO_UNPLUG true
2011-03-11 13:57:54 +00:00
#define AIO_READAHEAD 1
2011-05-06 10:25:52 +00:00
#define AIO_WAIT_DURING_FDSYNC false
2011-03-11 13:57:54 +00:00
2011-06-17 11:32:38 +00:00
#define COPY_APPEND_MODE 0
//#define COPY_APPEND_MODE 1 // FIXME: does not work yet
2011-05-26 14:32:32 +00:00
#define COPY_PRIO MARS_PRIO_LOW
2011-03-10 11:40:06 +00:00
static
2011-08-25 10:16:32 +00:00
int _set_trans_params(struct mars_brick *_brick, void *private)
2011-03-10 11:40:06 +00:00
{
2011-03-11 13:57:54 +00:00
struct trans_logger_brick *trans_brick = (void*)_brick;
2011-03-18 13:15:40 +00:00
if (_brick->type != (void*)&trans_logger_brick_type) {
MARS_ERR("bad brick type\n");
2011-08-25 10:16:32 +00:00
return -EINVAL;
2011-03-18 13:15:40 +00:00
}
2011-04-29 09:36:10 +00:00
if (!trans_brick->q_phase2.q_ordering) {
trans_brick->q_phase1.q_batchlen = CONF_TRANS_BATCHLEN;
trans_brick->q_phase2.q_batchlen = CONF_ALL_BATCHLEN;
trans_brick->q_phase3.q_batchlen = CONF_ALL_BATCHLEN;
trans_brick->q_phase4.q_batchlen = CONF_ALL_BATCHLEN;
trans_brick->q_phase1.q_max_flying = CONF_TRANS_FLYING;
trans_brick->q_phase2.q_max_flying = CONF_ALL_FLYING;
trans_brick->q_phase3.q_max_flying = CONF_ALL_FLYING;
trans_brick->q_phase4.q_max_flying = CONF_ALL_FLYING;
trans_brick->q_phase1.q_max_contention = CONF_ALL_CONTENTION;
trans_brick->q_phase2.q_max_contention = CONF_ALL_CONTENTION;
trans_brick->q_phase3.q_max_contention = CONF_ALL_CONTENTION;
trans_brick->q_phase4.q_max_contention = CONF_ALL_CONTENTION;
trans_brick->q_phase1.q_over_pressure = CONF_ALL_PRESSURE;
trans_brick->q_phase2.q_over_pressure = CONF_ALL_PRESSURE;
trans_brick->q_phase3.q_over_pressure = CONF_ALL_PRESSURE;
trans_brick->q_phase4.q_over_pressure = CONF_ALL_PRESSURE;
trans_brick->q_phase1.q_io_prio = CONF_TRANS_PRIO;
trans_brick->q_phase2.q_io_prio = CONF_ALL_PRIO;
trans_brick->q_phase3.q_io_prio = CONF_ALL_PRIO;
trans_brick->q_phase4.q_io_prio = CONF_ALL_PRIO;
trans_brick->q_phase2.q_max_queued = CONF_ALL_MAX_QUEUE;
trans_brick->q_phase4.q_max_queued = CONF_ALL_MAX_QUEUE;
trans_brick->q_phase2.q_max_jiffies = CONF_ALL_MAX_JIFFIES;
trans_brick->q_phase4.q_max_jiffies = CONF_ALL_MAX_JIFFIES;
trans_brick->q_phase2.q_ordering = true;
trans_brick->q_phase4.q_ordering = true;
2011-05-26 14:32:32 +00:00
2011-06-30 13:15:52 +00:00
trans_brick->shadow_mem_limit = CONF_TRANS_SHADOW_LIMIT;
2011-04-19 14:46:38 +00:00
trans_brick->log_reads = CONF_TRANS_LOG_READS;
2011-05-26 14:32:32 +00:00
trans_brick->completion_semantics = CONF_TRANS_COMPLETION_SEMANTICS;
2011-04-29 09:36:10 +00:00
trans_brick->minimize_latency = CONF_TRANS_MINIMIZE_LATENCY;
2011-03-18 13:15:40 +00:00
#ifdef TRANS_FAKE
trans_brick->debug_shortcut = true;
2011-03-11 13:57:54 +00:00
#endif
2011-03-18 13:15:40 +00:00
trans_brick->align_size = CONF_TRANS_ALIGN;
trans_brick->chunk_size = CONF_TRANS_CHUNKSIZE;
trans_brick->flush_delay = FLUSH_DELAY;
2011-03-10 11:40:06 +00:00
if (!trans_brick->log_reads) {
2011-04-29 09:36:10 +00:00
trans_brick->q_phase2.q_max_queued = 0;
trans_brick->q_phase4.q_max_queued *= 2;
2011-03-10 11:40:06 +00:00
}
}
2011-05-06 10:25:52 +00:00
MARS_INF("name = '%s' path = '%s'\n", _brick->brick_name, _brick->brick_path);
2011-08-25 10:16:32 +00:00
return 1;
2011-03-10 11:40:06 +00:00
}
2011-03-22 14:36:26 +00:00
static
2011-08-25 10:16:32 +00:00
int _set_client_params(struct mars_brick *_brick, void *private)
2011-03-22 14:36:26 +00:00
{
// currently no params
2011-05-06 10:25:52 +00:00
MARS_INF("name = '%s' path = '%s'\n", _brick->brick_name, _brick->brick_path);
2011-08-25 10:16:32 +00:00
return 1;
2011-03-22 14:36:26 +00:00
}
2011-03-11 13:57:54 +00:00
static
2011-08-25 10:16:32 +00:00
int _set_aio_params(struct mars_brick *_brick, void *private)
2011-03-11 13:57:54 +00:00
{
struct aio_brick *aio_brick = (void*)_brick;
2011-03-22 14:36:26 +00:00
if (_brick->type == (void*)&client_brick_type) {
2011-08-25 10:16:32 +00:00
return _set_client_params(_brick, private);
2011-03-22 14:36:26 +00:00
}
2011-03-18 13:15:40 +00:00
if (_brick->type != (void*)&aio_brick_type) {
MARS_ERR("bad brick type\n");
2011-08-25 10:16:32 +00:00
return -EINVAL;
2011-03-18 13:15:40 +00:00
}
2011-03-11 13:57:54 +00:00
aio_brick->readahead = AIO_READAHEAD;
aio_brick->o_direct = false; // important!
aio_brick->o_fdsync = true;
2011-04-29 09:36:10 +00:00
aio_brick->wait_during_fdsync = AIO_WAIT_DURING_FDSYNC;
2011-05-06 10:25:52 +00:00
MARS_INF("name = '%s' path = '%s'\n", _brick->brick_name, _brick->brick_path);
2011-08-25 10:16:32 +00:00
return 1;
2011-03-11 13:57:54 +00:00
}
2011-03-18 13:15:40 +00:00
static
2011-08-25 10:16:32 +00:00
int _set_bio_params(struct mars_brick *_brick, void *private)
2011-03-18 13:15:40 +00:00
{
struct bio_brick *bio_brick;
2011-03-22 14:36:26 +00:00
if (_brick->type == (void*)&client_brick_type) {
2011-08-25 10:16:32 +00:00
return _set_client_params(_brick, private);
2011-03-22 14:36:26 +00:00
}
2011-03-18 13:15:40 +00:00
if (_brick->type == (void*)&aio_brick_type) {
2011-08-25 10:16:32 +00:00
return _set_aio_params(_brick, private);
2011-03-18 13:15:40 +00:00
}
if (_brick->type != (void*)&bio_brick_type) {
MARS_ERR("bad brick type\n");
2011-08-25 10:16:32 +00:00
return -EINVAL;
2011-03-18 13:15:40 +00:00
}
bio_brick = (void*)_brick;
bio_brick->ra_pages = BIO_READAHEAD;
2011-05-06 10:25:52 +00:00
bio_brick->do_noidle = BIO_NOIDLE;
bio_brick->do_sync = BIO_SYNC;
bio_brick->do_unplug = BIO_UNPLUG;
MARS_INF("name = '%s' path = '%s'\n", _brick->brick_name, _brick->brick_path);
2011-08-25 10:16:32 +00:00
return 1;
2011-03-18 13:15:40 +00:00
}
2011-03-11 13:57:54 +00:00
static
2011-08-25 10:16:32 +00:00
int _set_if_params(struct mars_brick *_brick, void *private)
2011-03-11 13:57:54 +00:00
{
struct if_brick *if_brick = (void*)_brick;
2011-03-18 13:15:40 +00:00
if (_brick->type != (void*)&if_brick_type) {
MARS_ERR("bad brick type\n");
2011-08-25 10:16:32 +00:00
return -EINVAL;
2011-03-18 13:15:40 +00:00
}
2011-03-30 12:02:50 +00:00
if_brick->max_plugged = IF_MAX_PLUGGED;
2011-03-11 13:57:54 +00:00
if_brick->readahead = IF_READAHEAD;
2011-03-30 12:02:50 +00:00
if_brick->skip_sync = IF_SKIP_SYNC;
2011-05-06 10:25:52 +00:00
MARS_INF("name = '%s' path = '%s'\n", _brick->brick_name, _brick->brick_path);
2011-08-25 10:16:32 +00:00
return 1;
2011-03-11 13:57:54 +00:00
}
2011-03-10 11:40:06 +00:00
2011-08-25 10:16:32 +00:00
struct copy_cookie {
const char *argv[2];
const char *copy_path;
loff_t start_pos;
const char *fullpath[2];
struct mars_output *output[2];
struct mars_info info[2];
};
2011-05-26 14:32:32 +00:00
static
2011-08-25 10:16:32 +00:00
int _set_copy_params(struct mars_brick *_brick, void *private)
2011-05-26 14:32:32 +00:00
{
struct copy_brick *copy_brick = (void*)_brick;
2011-08-25 10:16:32 +00:00
struct copy_cookie *cc = private;
int status = 1;
2011-05-26 14:32:32 +00:00
if (_brick->type != (void*)&copy_brick_type) {
MARS_ERR("bad brick type\n");
2011-08-25 10:16:32 +00:00
status = -EINVAL;
goto done;
2011-05-26 14:32:32 +00:00
}
copy_brick->append_mode = COPY_APPEND_MODE;
copy_brick->io_prio = COPY_PRIO;
MARS_INF("name = '%s' path = '%s'\n", _brick->brick_name, _brick->brick_path);
2011-08-25 10:16:32 +00:00
/* Determine the copy area, switch on/off when necessary
*/
if (!copy_brick->power.button && copy_brick->power.led_off) {
int i;
copy_brick->copy_last = 0;
for (i = 0; i < 2; i++) {
status = cc->output[i]->ops->mars_get_info(cc->output[i], &cc->info[i]);
if (status < 0) {
MARS_ERR("cannot determine current size of '%s'\n", cc->argv[i]);
goto done;
}
MARS_DBG("%d '%s' current_size = %lld\n", i, cc->fullpath[i], cc->info[i].current_size);
}
copy_brick->copy_start = cc->info[1].current_size;
if (cc->start_pos != -1) {
copy_brick->copy_start = cc->start_pos;
if (unlikely(cc->info[0].current_size != cc->info[1].current_size)) {
MARS_ERR("oops, devices have different size %lld != %lld at '%s'\n", cc->info[0].current_size, cc->info[1].current_size, cc->copy_path);
status = -EINVAL;
goto done;
}
if (unlikely(cc->start_pos > cc->info[0].current_size)) {
MARS_ERR("bad start position %lld is larger than actual size %lld on '%s'\n", cc->start_pos, cc->info[0].current_size, cc->copy_path);
status = -EINVAL;
goto done;
}
}
MARS_DBG("copy_start = %lld\n", copy_brick->copy_start);
copy_brick->copy_end = cc->info[0].current_size;
MARS_DBG("copy_end = %lld\n", copy_brick->copy_end);
if (copy_brick->copy_start < copy_brick->copy_end) {
status = 1;
MARS_DBG("copy switch on\n");
}
} else if (copy_brick->power.button && copy_brick->power.led_on && copy_brick->copy_last == copy_brick->copy_end && copy_brick->copy_end > 0) {
status = 0;
MARS_DBG("copy switch off\n");
}
done:
return status;
2011-05-26 14:32:32 +00:00
}
2011-02-23 20:48:06 +00:00
///////////////////////////////////////////////////////////////////////
// internal helpers
#define MARS_DELIM ','
2011-02-28 18:00:32 +00:00
static int _parse_args(struct mars_dent *dent, char *str, int count)
2011-02-23 20:48:06 +00:00
{
int i;
int status = -EINVAL;
if (!str)
goto done;
if (!dent->d_args) {
2011-08-12 11:09:48 +00:00
dent->d_args = brick_strdup(str);
2011-02-23 20:48:06 +00:00
if (!dent->d_args) {
status = -ENOMEM;
goto done;
}
}
for (i = 0; i < count; i++) {
char *tmp;
int len;
if (!*str)
goto done;
if (i == count-1) {
len = strlen(str);
} else {
char *tmp = strchr(str, MARS_DELIM);
if (!tmp)
goto done;
len = (tmp - str);
}
2011-08-25 10:16:32 +00:00
tmp = brick_string_alloc(len + 1);
2011-02-23 20:48:06 +00:00
if (!tmp) {
status = -ENOMEM;
goto done;
}
2011-08-12 11:09:48 +00:00
brick_string_free(dent->d_argv[i]);
2011-02-23 20:48:06 +00:00
dent->d_argv[i] = tmp;
strncpy(dent->d_argv[i], str, len);
dent->d_argv[i][len] = '\0';
str += len;
if (i != count-1)
str++;
}
status = 0;
done:
if (status < 0) {
MARS_ERR("bad syntax '%s' (should have %d args), status = %d\n", dent->d_args ? dent->d_args : "", count, status);
}
return status;
}
2011-09-27 11:57:04 +00:00
///////////////////////////////////////////////////////////////////////
// needed for logfile rotation
struct mars_rotate {
struct mars_global *global;
struct mars_dent *replay_link;
struct mars_dent *aio_dent;
struct aio_brick *aio_brick;
struct mars_info aio_info;
struct trans_logger_brick *trans_brick;
struct mars_dent *relevant_log;
struct mars_brick *relevant_brick;
struct mars_dent *next_relevant_log;
struct mars_dent *current_log;
struct mars_dent *prev_log;
struct mars_dent *next_log;
struct if_brick *if_brick;
long long last_jiffies;
loff_t start_pos;
loff_t end_pos;
int max_sequence;
bool has_error;
bool try_sync;
bool do_replay;
bool todo_primary;
bool is_primary;
};
///////////////////////////////////////////////////////////////////////
// status display
static
int _show_actual(const char *path, const char *name, bool val)
{
char *src;
char *dst = NULL;
int status = -EINVAL;
src = val ? "1" : "0";
dst = path_make("%s/actual-%s/%s", path, my_id(), name);
status = -ENOMEM;
if (!dst)
goto done;
MARS_DBG("symlink '%s' -> '%s'\n", dst, src);
status = mars_symlink(src, dst, NULL, 0);
done:
brick_string_free(dst);
return status;
}
static
void _show_primary(struct mars_rotate *rot, struct mars_dent *parent)
{
int status;
if (!rot || !parent) {
return;
}
status = _show_actual(parent->d_path, "is-primary", rot->is_primary);
}
static
void _show_brick_status(struct mars_brick *test, bool shutdown)
{
const char *path;
char *src;
char *dst;
int status;
path = test->brick_path;
if (!path) {
MARS_WRN("bad path\n");
return;
}
if (*path != '/') {
MARS_WRN("bogus path '%s'\n", path);
return;
}
src = (test->power.led_on && !shutdown) ? "1" : "0";
dst = backskip_replace(path, '/', true, "/actual-%s/", my_id());
if (!dst) {
return;
}
status = mars_symlink(src, dst, NULL, 0);
MARS_DBG("status symlink '%s' -> '%s' status = %d\n", dst, src, status);
brick_string_free(dst);
}
static
void _show_status_all(struct mars_global *global)
{
struct list_head *tmp;
down_read(&global->brick_mutex);
for (tmp = global->brick_anchor.next; tmp != &global->brick_anchor; tmp = tmp->next) {
struct mars_brick *test;
test = container_of(tmp, struct mars_brick, global_brick_link);
if (!test->show_status)
2011-09-27 11:57:04 +00:00
continue;
_show_brick_status(test, false);
}
up_read(&global->brick_mutex);
}
2011-02-25 11:46:38 +00:00
///////////////////////////////////////////////////////////////////////
static
2011-03-01 09:34:36 +00:00
int __make_copy(
struct mars_global *global,
struct mars_dent *belongs,
2011-03-23 08:09:00 +00:00
const char *switch_path,
2011-03-22 14:36:26 +00:00
const char *copy_path,
2011-03-03 09:02:10 +00:00
const char *parent,
2011-03-01 09:34:36 +00:00
const char *argv[],
2011-03-04 07:41:00 +00:00
loff_t start_pos, // -1 means at EOF
2011-03-01 09:34:36 +00:00
struct copy_brick **__copy)
2011-02-25 11:46:38 +00:00
{
struct mars_brick *copy;
struct copy_brick *_copy;
2011-08-25 10:16:32 +00:00
struct copy_cookie cc = {};
2011-02-25 11:46:38 +00:00
int i;
2011-03-23 17:58:02 +00:00
int status = -EINVAL;
if (!switch_path) {
goto done;
}
2011-02-25 11:46:38 +00:00
for (i = 0; i < 2; i++) {
2011-03-02 09:30:56 +00:00
struct mars_brick *aio;
2011-03-03 09:02:10 +00:00
2011-08-25 10:16:32 +00:00
cc.argv[i] = argv[i];
2011-03-03 09:02:10 +00:00
if (parent) {
2011-08-25 10:16:32 +00:00
cc.fullpath[i] = path_make("%s/%s", parent, argv[i]);
if (!cc.fullpath[i]) {
2011-03-03 09:02:10 +00:00
MARS_ERR("cannot make path '%s/%s'\n", parent, argv[i]);
goto done;
}
} else {
2011-08-25 10:16:32 +00:00
cc.fullpath[i] = argv[i];
2011-03-03 09:02:10 +00:00
}
2011-03-02 09:30:56 +00:00
aio =
2011-03-01 09:34:36 +00:00
make_brick_all(global,
2011-03-11 13:57:54 +00:00
NULL,
2011-09-02 12:17:40 +00:00
false,
2011-03-22 14:36:26 +00:00
_set_bio_params,
2011-03-03 09:02:10 +00:00
NULL,
2011-03-01 09:34:36 +00:00
10 * HZ,
2011-03-03 09:02:10 +00:00
NULL,
2011-03-22 14:36:26 +00:00
(const struct generic_brick_type*)&bio_brick_type,
2011-03-01 09:34:36 +00:00
(const struct generic_brick_type*[]){},
2011-03-23 08:09:00 +00:00
NULL,
2011-08-25 10:16:32 +00:00
cc.fullpath[i],
2011-03-01 09:34:36 +00:00
(const char *[]){},
2011-03-03 09:02:10 +00:00
0);
2011-03-02 09:30:56 +00:00
if (!aio) {
2011-08-25 10:16:32 +00:00
MARS_DBG("cannot instantiate '%s'\n", cc.fullpath[i]);
2011-02-25 11:46:38 +00:00
goto done;
}
2011-08-25 10:16:32 +00:00
cc.output[i] = aio->outputs[0];
2011-02-25 11:46:38 +00:00
}
2011-08-25 10:16:32 +00:00
cc.copy_path = copy_path;
cc.start_pos = start_pos;
2011-03-01 09:34:36 +00:00
copy =
make_brick_all(global,
belongs,
2011-09-02 12:17:40 +00:00
false,
2011-05-26 14:32:32 +00:00
_set_copy_params,
2011-08-25 10:16:32 +00:00
&cc,
10 * HZ,
cc.fullpath[1],
2011-03-01 09:34:36 +00:00
(const struct generic_brick_type*)&copy_brick_type,
(const struct generic_brick_type*[]){NULL,NULL,NULL,NULL},
2011-03-03 09:02:10 +00:00
"%s",
2011-03-23 08:09:00 +00:00
"%s",
2011-03-03 09:02:10 +00:00
(const char *[]){"%s", "%s", "%s", "%s"},
2011-03-02 09:30:56 +00:00
4,
2011-03-23 08:09:00 +00:00
switch_path,
2011-03-22 14:36:26 +00:00
copy_path,
2011-08-25 10:16:32 +00:00
cc.fullpath[0],
cc.fullpath[0],
cc.fullpath[1],
cc.fullpath[1]);
2011-02-25 11:46:38 +00:00
if (!copy) {
2011-06-17 11:32:38 +00:00
MARS_DBG("creation of copy brick '%s' failed\n", copy_path);
2011-03-01 09:34:36 +00:00
goto done;
2011-02-25 11:46:38 +00:00
}
2011-09-27 11:57:04 +00:00
copy->show_status = _show_brick_status;
2011-02-25 11:46:38 +00:00
_copy = (void*)copy;
if (__copy)
*__copy = _copy;
status = 0;
done:
MARS_DBG("status = %d\n", status);
2011-03-03 09:02:10 +00:00
for (i = 0; i < 2; i++) {
2011-08-25 10:16:32 +00:00
if (cc.fullpath[i] && cc.fullpath[i] != argv[i])
brick_string_free(cc.fullpath[i]);
2011-03-03 09:02:10 +00:00
}
2011-02-25 11:46:38 +00:00
return status;
}
2011-02-23 20:48:06 +00:00
///////////////////////////////////////////////////////////////////////
// remote workers
struct mars_peerinfo {
2011-02-24 16:37:32 +00:00
struct mars_global *global;
2011-02-23 20:48:06 +00:00
char *peer;
char *path;
2011-08-31 11:42:04 +00:00
struct mars_socket *socket;
2011-08-25 10:16:32 +00:00
struct task_struct *peer_thread;
2011-03-22 14:36:26 +00:00
spinlock_t lock;
struct list_head remote_dent_list;
//wait_queue_head_t event;
2011-02-23 20:48:06 +00:00
int maxdepth;
};
2011-02-24 16:37:32 +00:00
static
2011-03-23 08:09:00 +00:00
bool _is_usable_dir(const char *name)
{
if (!strncmp(name, "resource-", 9)
|| !strncmp(name, "todo-", 5)
|| !strncmp(name, "actual-", 7)
2011-03-23 08:09:00 +00:00
|| !strncmp(name, "defaults", 8)
) {
return true;
}
return false;
}
static
bool _is_peer_logfile(const char *name, const char *id)
2011-03-22 14:36:26 +00:00
{
int len = strlen(name);
int idlen = id ? strlen(id) : 4 + 9 + 1;
if (len <= idlen ||
strncmp(name, "log-", 4) != 0) {
MARS_DBG("not a logfile at all: '%s'\n", name);
return false;
}
if (id &&
name[len - idlen - 1] == '-' &&
strncmp(name + len - idlen, id, idlen) == 0) {
MARS_DBG("not a peer logfile: '%s'\n", name);
return false;
}
MARS_DBG("found peer logfile: '%s'\n", name);
return true;
}
static
2011-03-23 08:09:00 +00:00
int _update_file(struct mars_global *global, const char *switch_path, const char *copy_path, const char *file, const char *peer, loff_t end_pos)
2011-02-24 16:37:32 +00:00
{
2011-03-22 14:36:26 +00:00
const char *tmp = path_make("%s@%s", file, peer);
const char *argv[2] = { tmp, file };
2011-03-04 07:41:00 +00:00
struct copy_brick *copy = NULL;
2011-03-01 18:00:14 +00:00
int status = -ENOMEM;
2011-02-24 16:37:32 +00:00
2011-03-01 18:00:14 +00:00
if (unlikely(!tmp))
goto done;
2011-02-24 16:37:32 +00:00
2011-03-22 14:36:26 +00:00
MARS_DBG("src = '%s' dst = '%s'\n", tmp, file);
2011-03-23 08:09:00 +00:00
status = __make_copy(global, NULL, switch_path, copy_path, NULL, argv, -1, &copy);
2011-06-17 11:32:38 +00:00
if (status >= 0 && copy && (!copy->append_mode || copy->power.led_off)) {
2011-03-04 07:41:00 +00:00
if (end_pos > copy->copy_end) {
2011-03-22 14:36:26 +00:00
MARS_DBG("appending to '%s' %lld => %lld\n", copy_path, copy->copy_end, end_pos);
2011-03-04 07:41:00 +00:00
copy->copy_end = end_pos;
}
}
2011-02-24 16:37:32 +00:00
2011-03-01 18:00:14 +00:00
done:
2011-03-03 09:02:10 +00:00
if (tmp)
2011-08-12 11:09:48 +00:00
brick_string_free(tmp);
2011-02-24 16:37:32 +00:00
return status;
}
static
2011-03-22 14:36:26 +00:00
int check_logfile(struct mars_peerinfo *peer, struct mars_dent *dent, struct mars_dent *parent, loff_t dst_size)
2011-02-24 16:37:32 +00:00
{
2011-03-22 14:36:26 +00:00
loff_t src_size = dent->new_stat.size;
2011-07-08 07:02:14 +00:00
struct mars_rotate *rot;
2011-03-23 08:09:00 +00:00
const char *switch_path = NULL;
2011-03-22 14:36:26 +00:00
const char *copy_path = NULL;
const char *alias_path = NULL;
struct mars_dent *local_alias;
struct copy_brick *copy_brick;
int status = 0;
2011-02-24 16:37:32 +00:00
2011-03-25 10:34:54 +00:00
// plausibility checks
if (unlikely(dst_size > src_size)) {
MARS_WRN("my local copy is larger than the remote one, ignoring\n");
status = -EINVAL;
2011-03-22 14:36:26 +00:00
goto done;
2011-02-24 16:37:32 +00:00
}
2011-03-22 14:36:26 +00:00
2011-07-08 07:02:14 +00:00
// check whether we are pariticipating in that resource
rot = parent->d_private;
if (!rot) {
MARS_WRN("parent has no rot info\n");
status = -EINVAL;
goto done;
}
if (!rot->try_sync) {
MARS_DBG("logfiles are not for me.");
status = 0;
goto done;
}
2011-03-22 14:36:26 +00:00
// check whether (some/another) copy is already running
copy_path = path_make("%s/logfile-update", parent->d_path);
if (unlikely(!copy_path)) {
status = -ENOMEM;
goto done;
2011-02-28 09:04:16 +00:00
}
2011-03-22 14:36:26 +00:00
copy_brick = (struct copy_brick*)mars_find_brick(peer->global, &copy_brick_type, copy_path);
2011-03-25 10:34:54 +00:00
MARS_DBG("copy_path = '%s' copy_brick = %p dent = '%s'\n", copy_path, copy_brick, dent->d_path);
2011-03-22 14:36:26 +00:00
if (copy_brick) {
bool copy_is_done = (copy_brick->copy_last == copy_brick->copy_end);
bool is_my_copy = !strcmp(copy_brick->brick_name, dent->d_path);
bool is_next_copy = (dent->d_serial == parent->d_logfile_serial + 1);
2011-03-25 10:34:54 +00:00
MARS_DBG("copy brick '%s' copy_last = %lld copy_end = %lld dent '%s' serial = %d/%d is_done = %d is_my_copy = %d is_next_copy = %d\n", copy_brick->brick_name, copy_brick->copy_last, copy_brick->copy_end, dent->d_path, dent->d_serial, parent->d_logfile_serial, copy_is_done, is_my_copy, is_next_copy);
2011-03-22 14:36:26 +00:00
// ensure consecutiveness of logfiles
if (copy_is_done && !is_my_copy && is_next_copy) {
MARS_DBG("killing old copy brick '%s', now going to '%s'\n", copy_brick->brick_name, dent->d_path);
status = mars_kill_brick((void*)copy_brick);
if (status < 0)
goto done;
}
if (!is_my_copy) {
goto done;
}
}
2011-06-01 12:55:06 +00:00
// create local alias symlink
if (unlikely(dent->d_serial <= 0)) {
MARS_ERR("path '%s' has invalid serial %d\n", dent->d_path, dent->d_serial);
status = -EINVAL;
goto done;
}
2011-06-17 11:32:38 +00:00
alias_path = path_make("%s/log-%09d-%s", parent->d_path, dent->d_serial, my_id());
if (unlikely(!alias_path)) {
status = -ENOMEM;
goto done;
}
status = 0;
MARS_DBG("local alias for '%s' is '%s'\n", dent->d_path, alias_path);
local_alias = mars_find_dent((void*)peer->global, alias_path);
if (!local_alias) {
status = mars_symlink(dent->d_name, alias_path, &dent->new_stat.mtime, 0);
MARS_DBG("create alias '%s' -> '%s' status = %d\n", alias_path, dent->d_name, status);
//run_trigger = true;
2011-06-01 12:55:06 +00:00
}
// copy necessary?
2011-03-25 10:34:54 +00:00
status = 0;
if (dst_size >= src_size) { // nothing to do
goto done;
}
2011-03-22 14:36:26 +00:00
// check whether connection is allowed
2011-09-21 11:30:11 +00:00
switch_path = path_make("%s/todo-%s/connect", parent->d_path, my_id());
2011-03-22 14:36:26 +00:00
2011-03-23 17:58:02 +00:00
// start / treat copy brick instance
2011-03-23 08:09:00 +00:00
status = _update_file(peer->global, switch_path, copy_path, dent->d_path, peer->peer, src_size);
2011-03-22 14:36:26 +00:00
MARS_DBG("update '%s' from peer '%s' status = %d\n", dent->d_path, peer->peer, status);
if (status < 0) {
goto done;
}
parent->d_logfile_serial = dent->d_serial;
done:
if (copy_path)
2011-08-12 11:09:48 +00:00
brick_string_free(copy_path);
2011-03-22 14:36:26 +00:00
if (alias_path)
2011-08-12 11:09:48 +00:00
brick_string_free(alias_path);
2011-03-23 17:58:02 +00:00
if (switch_path)
2011-08-12 11:09:48 +00:00
brick_string_free(switch_path);
2011-03-22 14:36:26 +00:00
return status;
2011-02-24 16:37:32 +00:00
}
static
2011-03-22 14:36:26 +00:00
int run_bone(struct mars_peerinfo *peer, struct mars_dent *dent)
2011-02-23 20:48:06 +00:00
{
int status = 0;
2011-02-27 14:17:58 +00:00
struct kstat local_stat = {};
bool stat_ok;
bool update_mtime = true;
bool update_ctime = true;
2011-03-22 14:36:26 +00:00
bool run_trigger = false;
2011-02-24 16:37:32 +00:00
if (!strncmp(dent->d_name, ".tmp", 4)) {
goto done;
}
if (!strncmp(dent->d_name, "ignore", 6)) {
goto done;
}
2011-03-18 13:15:40 +00:00
status = mars_stat(dent->d_path, &local_stat, true);
2011-02-27 14:17:58 +00:00
stat_ok = (status >= 0);
if (stat_ok) {
update_mtime = timespec_compare(&dent->new_stat.mtime, &local_stat.mtime) > 0;
update_ctime = timespec_compare(&dent->new_stat.ctime, &local_stat.ctime) > 0;
2011-06-10 13:57:52 +00:00
MARS_DBG("timestamps '%s' remote = %ld.%09ld local = %ld.%09ld\n", dent->d_path, dent->new_stat.mtime.tv_sec, dent->new_stat.mtime.tv_nsec, local_stat.mtime.tv_sec, local_stat.mtime.tv_nsec);
2011-02-27 14:17:58 +00:00
if ((dent->new_stat.mode & S_IRWXU) !=
(local_stat.mode & S_IRWXU) &&
update_ctime) {
mode_t newmode = local_stat.mode;
MARS_DBG("chmod '%s' 0x%xd -> 0x%xd\n", dent->d_path, newmode & S_IRWXU, dent->new_stat.mode & S_IRWXU);
newmode &= ~S_IRWXU;
newmode |= (dent->new_stat.mode & S_IRWXU);
mars_chmod(dent->d_path, newmode);
2011-03-22 14:36:26 +00:00
run_trigger = true;
2011-02-27 14:17:58 +00:00
}
if (dent->new_stat.uid != local_stat.uid && update_ctime) {
MARS_DBG("lchown '%s' %d -> %d\n", dent->d_path, local_stat.uid, dent->new_stat.uid);
mars_lchown(dent->d_path, dent->new_stat.uid);
2011-03-22 14:36:26 +00:00
run_trigger = true;
2011-02-27 14:17:58 +00:00
}
}
2011-02-23 20:48:06 +00:00
if (S_ISDIR(dent->new_stat.mode)) {
2011-03-23 08:09:00 +00:00
if (!_is_usable_dir(dent->d_name)) {
2011-02-23 20:48:06 +00:00
MARS_DBG("ignoring directory '%s'\n", dent->d_path);
2011-02-24 16:37:32 +00:00
goto done;
}
2011-02-27 14:17:58 +00:00
if (!stat_ok) {
2011-02-24 16:37:32 +00:00
status = mars_mkdir(dent->d_path);
MARS_DBG("create directory '%s' status = %d\n", dent->d_path, status);
2011-03-22 14:36:26 +00:00
if (status >= 0) {
mars_chmod(dent->d_path, dent->new_stat.mode);
mars_lchown(dent->d_path, dent->new_stat.uid);
}
2011-02-23 20:48:06 +00:00
}
} else if (S_ISLNK(dent->new_stat.mode) && dent->new_link) {
2011-02-27 14:17:58 +00:00
if (!stat_ok || update_mtime) {
status = mars_symlink(dent->new_link, dent->d_path, &dent->new_stat.mtime, dent->new_stat.uid);
2011-02-24 16:37:32 +00:00
MARS_DBG("create symlink '%s' -> '%s' status = %d\n", dent->d_path, dent->new_link, status);
2011-03-22 14:36:26 +00:00
run_trigger = true;
2011-02-24 16:37:32 +00:00
}
} else if (S_ISREG(dent->new_stat.mode) && _is_peer_logfile(dent->d_name, my_id())) {
2011-03-22 14:36:26 +00:00
const char *parent_path = backskip_replace(dent->d_path, '/', false, "");
if (likely(parent_path)) {
struct mars_dent *parent = mars_find_dent(peer->global, parent_path);
if (unlikely(!parent)) {
MARS_DBG("ignoring non-existing local resource '%s'\n", parent_path);
} else {
status = check_logfile(peer, dent, parent, local_stat.size);
2011-02-24 16:37:32 +00:00
}
2011-08-12 11:09:48 +00:00
brick_string_free(parent_path);
2011-02-24 16:37:32 +00:00
}
2011-02-23 20:48:06 +00:00
} else {
MARS_DBG("ignoring '%s'\n", dent->d_path);
}
2011-02-24 16:37:32 +00:00
done:
2011-03-22 14:36:26 +00:00
if (status >= 0) {
status = run_trigger ? 1 : 0;
}
return status;
}
static
int run_bones(struct mars_peerinfo *peer)
{
LIST_HEAD(tmp_list);
struct list_head *tmp;
unsigned long flags;
bool run_trigger = false;
int status = 0;
traced_lock(&peer->lock, flags);
list_replace_init(&peer->remote_dent_list, &tmp_list);
traced_unlock(&peer->lock, flags);
for (tmp = tmp_list.next; tmp != &tmp_list; tmp = tmp->next) {
struct mars_dent *dent = container_of(tmp, struct mars_dent, dent_link);
if (!dent->d_path) {
MARS_DBG("NULL\n");
continue;
}
2011-06-10 13:57:52 +00:00
MARS_DBG("path = '%s'\n", dent->d_path);
2011-03-22 14:36:26 +00:00
status = run_bone(peer, dent);
if (status > 0)
run_trigger = true;
//MARS_DBG("path = '%s' worker status = %d\n", dent->d_path, status);
}
2011-09-27 08:46:14 +00:00
mars_free_dent_all(NULL, &tmp_list);
2011-03-22 14:36:26 +00:00
#if 0
if (run_trigger) {
mars_trigger();
}
#endif
2011-02-23 20:48:06 +00:00
return status;
}
///////////////////////////////////////////////////////////////////////
// remote working infrastructure
2011-02-24 16:37:32 +00:00
static
2011-08-31 11:42:04 +00:00
void _peer_cleanup(struct mars_peerinfo *peer, bool do_stop)
2011-02-23 20:48:06 +00:00
{
2011-08-31 11:42:04 +00:00
unsigned long flags;
MARS_DBG("\n");
if (peer->socket) {
mars_shutdown_socket(peer->socket);
}
if (do_stop && peer->peer_thread) {
kthread_stop(peer->peer_thread);
put_task_struct(peer->peer_thread);
peer->peer_thread = NULL;
}
2011-02-23 20:48:06 +00:00
if (peer->socket) {
2011-08-31 11:42:04 +00:00
mars_put_socket(peer->socket);
2011-02-23 20:48:06 +00:00
peer->socket = NULL;
}
2011-08-31 11:42:04 +00:00
if (do_stop) {
2011-09-27 08:46:14 +00:00
LIST_HEAD(tmp_list);
2011-08-31 11:42:04 +00:00
traced_lock(&peer->lock, flags);
2011-09-27 08:46:14 +00:00
list_replace_init(&peer->remote_dent_list, &tmp_list);
2011-08-31 11:42:04 +00:00
traced_unlock(&peer->lock, flags);
2011-09-27 08:46:14 +00:00
mars_free_dent_all(NULL, &tmp_list);
2011-08-31 11:42:04 +00:00
brick_string_free(peer->peer);
brick_string_free(peer->path);
}
2011-02-23 20:48:06 +00:00
}
2011-02-24 16:37:32 +00:00
static
int remote_thread(void *data)
2011-02-23 20:48:06 +00:00
{
struct mars_peerinfo *peer = data;
2011-02-24 16:37:32 +00:00
char *real_peer;
2011-02-23 20:48:06 +00:00
struct sockaddr_storage sockaddr = {};
int status;
if (!peer)
return -1;
2011-07-22 10:43:40 +00:00
real_peer = mars_translate_hostname(peer->peer);
2011-02-24 16:37:32 +00:00
MARS_INF("-------- remote thread starting on peer '%s' (%s)\n", peer->peer, real_peer);
2011-02-23 20:48:06 +00:00
//fake_mm();
2011-02-24 16:37:32 +00:00
status = mars_create_sockaddr(&sockaddr, real_peer);
2011-02-23 20:48:06 +00:00
if (unlikely(status < 0)) {
2011-02-24 16:37:32 +00:00
MARS_ERR("unusable remote address '%s' (%s)\n", real_peer, peer->peer);
2011-02-23 20:48:06 +00:00
goto done;
}
while (!kthread_should_stop()) {
LIST_HEAD(tmp_list);
2011-03-22 14:36:26 +00:00
LIST_HEAD(old_list);
unsigned long flags;
2011-02-23 20:48:06 +00:00
struct mars_cmd cmd = {
.cmd_code = CMD_GETENTS,
.cmd_str1 = peer->path,
.cmd_int1 = peer->maxdepth,
};
if (!peer->socket) {
2011-08-31 11:42:04 +00:00
peer->socket = mars_create_socket(&sockaddr, false);
if (unlikely(IS_ERR(peer->socket))) {
status = PTR_ERR(peer->socket);
2011-02-23 20:48:06 +00:00
peer->socket = NULL;
2011-02-24 16:37:32 +00:00
MARS_INF("no connection to '%s'\n", real_peer);
2011-02-23 20:48:06 +00:00
msleep(5000);
continue;
}
2011-02-24 16:37:32 +00:00
MARS_DBG("successfully opened socket to '%s'\n", real_peer);
2011-02-23 20:48:06 +00:00
continue;
}
2011-08-31 11:42:04 +00:00
status = mars_send_struct(peer->socket, &cmd, mars_cmd_meta);
2011-02-23 20:48:06 +00:00
if (unlikely(status < 0)) {
2011-08-31 11:42:04 +00:00
MARS_WRN("communication error on send, status = %d\n", status);
_peer_cleanup(peer, false);
2011-07-08 07:02:14 +00:00
msleep(2000);
2011-02-23 20:48:06 +00:00
continue;
}
2011-08-31 11:42:04 +00:00
status = mars_recv_dent_list(peer->socket, &tmp_list);
2011-02-23 20:48:06 +00:00
if (unlikely(status < 0)) {
2011-08-31 11:42:04 +00:00
MARS_WRN("communication error on receive, status = %d\n", status);
_peer_cleanup(peer, false);
2011-02-23 20:48:06 +00:00
msleep(5000);
continue;
}
2011-03-22 14:36:26 +00:00
if (list_empty(&tmp_list)) {
msleep(5000);
continue;
}
2011-02-24 16:37:32 +00:00
//MARS_DBG("AHA!!!!!!!!!!!!!!!!!!!!\n");
2011-02-23 20:48:06 +00:00
2011-03-22 14:36:26 +00:00
traced_lock(&peer->lock, flags);
list_replace_init(&peer->remote_dent_list, &old_list);
list_replace_init(&tmp_list, &peer->remote_dent_list);
2011-02-23 20:48:06 +00:00
2011-03-22 14:36:26 +00:00
traced_unlock(&peer->lock, flags);
2011-09-27 08:46:14 +00:00
mars_free_dent_all(NULL, &old_list);
2011-02-23 20:48:06 +00:00
if (!kthread_should_stop())
2011-06-01 12:55:06 +00:00
msleep(5 * 1000);
2011-02-23 20:48:06 +00:00
}
MARS_INF("-------- remote thread terminating\n");
2011-08-31 11:42:04 +00:00
_peer_cleanup(peer, false);
2011-02-23 20:48:06 +00:00
done:
//cleanup_mm();
2011-08-25 10:16:32 +00:00
brick_string_free(real_peer);
2011-02-23 20:48:06 +00:00
return 0;
}
///////////////////////////////////////////////////////////////////////
// helpers for worker functions
2011-02-28 18:00:32 +00:00
static int _kill_peer(void *buf, struct mars_dent *dent)
2011-02-23 20:48:06 +00:00
{
struct mars_global *global = buf;
struct mars_peerinfo *peer = dent->d_private;
if (global->global_power.button) {
return 0;
}
if (!peer) {
return 0;
}
2011-08-31 11:42:04 +00:00
MARS_INF("stopping peer thread...\n");
_peer_cleanup(peer, true);
2011-02-23 20:48:06 +00:00
dent->d_private = NULL;
2011-08-31 11:42:04 +00:00
brick_mem_free(peer);
2011-02-23 20:48:06 +00:00
return 0;
}
2011-03-22 14:36:26 +00:00
static int _make_peer(struct mars_global *global, struct mars_dent *dent, char *mypeer, char *path)
2011-02-23 20:48:06 +00:00
{
static int serial = 0;
struct mars_peerinfo *peer;
int status = 0;
2011-03-22 14:36:26 +00:00
if (!global->global_power.button || !dent->new_link) {
2011-02-23 20:48:06 +00:00
return 0;
}
if (!mypeer) {
status = _parse_args(dent, dent->new_link, 1);
if (status < 0)
goto done;
mypeer = dent->d_argv[0];
}
MARS_DBG("peer '%s'\n", mypeer);
if (!dent->d_private) {
2011-08-12 11:09:48 +00:00
dent->d_private = brick_zmem_alloc(sizeof(struct mars_peerinfo));
2011-02-23 20:48:06 +00:00
if (!dent->d_private) {
MARS_ERR("no memory for peer structure\n");
return -1;
}
peer = dent->d_private;
2011-02-24 16:37:32 +00:00
peer->global = global;
2011-08-31 11:42:04 +00:00
peer->peer = brick_strdup(mypeer);
peer->path = brick_strdup(path);
2011-02-23 20:48:06 +00:00
peer->maxdepth = 2;
2011-03-22 14:36:26 +00:00
spin_lock_init(&peer->lock);
INIT_LIST_HEAD(&peer->remote_dent_list);
//init_waitqueue_head(&peer->event);
2011-02-23 20:48:06 +00:00
}
2011-03-22 14:36:26 +00:00
2011-02-23 20:48:06 +00:00
peer = dent->d_private;
2011-08-25 10:16:32 +00:00
if (!peer->peer_thread) {
peer->peer_thread = kthread_create(remote_thread, peer, "mars_remote%d", serial++);
if (unlikely(IS_ERR(peer->peer_thread))) {
MARS_ERR("cannot start peer thread, status = %d\n", (int)PTR_ERR(peer->peer_thread));
peer->peer_thread = NULL;
2011-02-23 20:48:06 +00:00
return -1;
}
2011-02-24 16:37:32 +00:00
MARS_DBG("starting peer thread\n");
2011-08-31 11:42:04 +00:00
get_task_struct(peer->peer_thread);
2011-08-25 10:16:32 +00:00
wake_up_process(peer->peer_thread);
2011-02-23 20:48:06 +00:00
}
2011-09-27 08:46:14 +00:00
/* This must be called by the main thread in order to
* avoid nasty races.
* The peer thread does nothing but fetching the dent list.
*/
2011-03-22 14:36:26 +00:00
status = run_bones(peer);
2011-02-23 20:48:06 +00:00
done:
return status;
}
2011-02-28 18:00:32 +00:00
static int kill_scan(void *buf, struct mars_dent *dent)
2011-02-23 20:48:06 +00:00
{
return _kill_peer(buf, dent);
}
2011-02-28 18:00:32 +00:00
static int make_scan(void *buf, struct mars_dent *dent)
2011-02-23 20:48:06 +00:00
{
2011-02-24 16:37:32 +00:00
MARS_DBG("path = '%s' peer = '%s'\n", dent->d_path, dent->d_rest);
2011-03-22 14:36:26 +00:00
// don't connect to myself
2011-02-24 16:37:32 +00:00
if (!strcmp(dent->d_rest, my_id())) {
return 0;
}
2011-03-22 14:36:26 +00:00
return _make_peer(buf, dent, dent->d_rest, "/mars");
2011-02-23 20:48:06 +00:00
}
static
2011-08-25 10:16:32 +00:00
int kill_any(void *buf, struct mars_dent *dent)
2011-02-23 20:48:06 +00:00
{
struct mars_global *global = buf;
2011-08-25 10:16:32 +00:00
struct list_head *tmp;
2011-02-23 20:48:06 +00:00
2011-03-23 08:09:00 +00:00
if (global->global_power.button) {
2011-02-23 20:48:06 +00:00
return 0;
}
2011-08-25 10:16:32 +00:00
for (tmp = dent->brick_list.next; tmp != &dent->brick_list; tmp = tmp->next) {
struct mars_brick *brick = container_of(tmp, struct mars_brick, dent_brick_link);
if (brick->nr_outputs > 0 && brick->outputs[0] && brick->outputs[0]->nr_connected) {
MARS_DBG("cannot kill dent '%s' because brick '%s' is wired\n", dent->d_path, brick->brick_path);
return 0;
}
}
2011-03-01 18:00:14 +00:00
MARS_DBG("killing dent = '%s'\n", dent->d_path);
2011-03-23 08:09:00 +00:00
mars_kill_dent(dent);
2011-09-27 11:57:04 +00:00
return 1;
2011-02-23 20:48:06 +00:00
}
///////////////////////////////////////////////////////////////////////
// handlers / helpers for logfile rotation
static
2011-03-23 17:58:02 +00:00
void _create_new_logfile(const char *path)
2011-02-23 20:48:06 +00:00
{
struct file *f;
const int flags = O_RDWR | O_CREAT | O_EXCL;
const int prot = 0600;
mm_segment_t oldfs;
oldfs = get_fs();
set_fs(get_ds());
f = filp_open(path, flags, prot);
set_fs(oldfs);
2011-03-03 09:02:10 +00:00
if (IS_ERR(f)) {
2011-03-25 10:34:54 +00:00
int err = PTR_ERR(f);
if (err == -EEXIST) {
MARS_INF("logfile '%s' already exists\n", path);
} else {
MARS_ERR("could not create logfile '%s' status = %d\n", path, err);
}
2011-03-03 09:02:10 +00:00
} else {
2011-02-23 20:48:06 +00:00
MARS_DBG("created empty logfile '%s'\n", path);
2011-03-03 09:02:10 +00:00
filp_close(f, NULL);
2011-02-23 20:48:06 +00:00
mars_trigger();
}
}
static
2011-05-13 11:19:28 +00:00
int _update_replaylink(struct mars_dent *parent, int sequence, loff_t start_pos, loff_t end_pos, bool check_exist)
2011-02-23 20:48:06 +00:00
{
struct timespec now = {};
2011-03-01 18:00:14 +00:00
char *old;
char *new;
int status = -ENOMEM;
2011-02-23 20:48:06 +00:00
2011-03-07 05:55:10 +00:00
if (check_exist) {
struct kstat kstat;
char *test = path_make("%s/log-%09d-%s", parent->d_path, sequence, my_id());
if (!test) {
goto out_old;
}
2011-03-18 13:15:40 +00:00
status = mars_stat(test, &kstat, true);
2011-08-12 11:09:48 +00:00
brick_string_free(test);
2011-03-07 05:55:10 +00:00
if (status < 0) {
MARS_DBG("could not update replay link to nonexisting logfile %09d\n", sequence);
goto out_old;
}
status = -ENOMEM;
}
2011-05-13 11:19:28 +00:00
old = path_make("log-%09d-%s,%lld,%lld", sequence, my_id(), start_pos, end_pos - start_pos);
2011-03-07 05:55:10 +00:00
if (!old) {
2011-03-01 18:00:14 +00:00
goto out_old;
2011-03-07 05:55:10 +00:00
}
2011-03-03 09:02:10 +00:00
new = path_make("%s/replay-%s", parent->d_path, my_id());
2011-03-07 05:55:10 +00:00
if (!new) {
2011-03-01 18:00:14 +00:00
goto out_new;
2011-03-07 05:55:10 +00:00
}
2011-02-23 20:48:06 +00:00
get_lamport(&now);
2011-02-27 14:17:58 +00:00
status = mars_symlink(old, new, &now, 0);
2011-02-23 20:48:06 +00:00
if (status < 0) {
MARS_ERR("cannot create symlink '%s' -> '%s' status = %d\n", old, new, status);
} else {
2011-04-08 09:52:46 +00:00
MARS_DBG("make replay symlink '%s' -> '%s' status = %d\n", old, new, status);
2011-02-23 20:48:06 +00:00
}
2011-03-01 18:00:14 +00:00
2011-08-12 11:09:48 +00:00
brick_string_free(new);
2011-03-01 18:00:14 +00:00
out_new:
2011-08-12 11:09:48 +00:00
brick_string_free(old);
2011-03-01 18:00:14 +00:00
out_old:
2011-02-23 20:48:06 +00:00
return status;
}
2011-05-13 11:19:28 +00:00
static
2011-09-27 08:46:14 +00:00
int _update_versionlink(struct mars_global *global, struct mars_dent *parent, int sequence, loff_t start_pos, loff_t end_pos)
2011-05-13 11:19:28 +00:00
{
2011-06-17 11:32:38 +00:00
char *prev = NULL;
struct mars_dent *prev_link = NULL;
2011-05-13 11:19:28 +00:00
char *prev_digest = NULL;
struct timespec now = {};
int i;
2011-07-20 13:11:44 +00:00
int status = -ENOMEM;
2011-05-13 11:19:28 +00:00
int len = 0;
int oldlen;
2011-05-13 11:19:28 +00:00
2011-08-12 11:09:48 +00:00
char *new = NULL;
2011-08-25 10:16:32 +00:00
char *data = brick_string_alloc(0);
char *old = brick_string_alloc(0);
2011-09-29 08:06:14 +00:00
unsigned char *digest = brick_string_alloc(0);
2011-07-20 13:11:44 +00:00
if (unlikely(!data || !digest || !old)) {
MARS_ERR("no MEM\n");
goto out;
}
status = -EINVAL;
2011-06-17 11:32:38 +00:00
if (sequence > 1) {
prev = path_make("%s/version-%09d-%s", parent->d_path, sequence-1, my_id());
if (unlikely(!prev)) {
goto out;
}
prev_link = mars_find_dent(global, prev);
if (unlikely(!prev_link)) {
MARS_ERR("cannot find previous version symlink '%s'\n", prev);
goto out;
}
2011-05-13 11:19:28 +00:00
prev_digest = prev_link->new_link;
}
2011-08-12 11:09:48 +00:00
len = sprintf(data, "%d,%lld,%lld,%s", sequence, start_pos, end_pos, prev_digest ? prev_digest : "");
2011-05-13 11:19:28 +00:00
MARS_DBG("data = '%s' len = %d\n", data, len);
mars_digest(digest, data, len);
oldlen = 0;
2011-05-13 11:19:28 +00:00
for (i = 0; i < mars_digest_size; i++) {
oldlen += sprintf(old + oldlen, "%02x", digest[i]);
2011-05-13 11:19:28 +00:00
}
oldlen += sprintf(old + oldlen, ",%lld,%lld", start_pos, end_pos);
2011-05-13 11:19:28 +00:00
new = path_make("%s/version-%09d-%s", parent->d_path, sequence, my_id());
if (!new) {
goto out;
}
get_lamport(&now);
status = mars_symlink(old, new, &now, 0);
if (status < 0) {
MARS_ERR("cannot create symlink '%s' -> '%s' status = %d\n", old, new, status);
} else {
MARS_DBG("make version symlink '%s' -> '%s' status = %d\n", old, new, status);
}
out:
brick_string_free(new);
brick_string_free(prev);
brick_string_free(data);
brick_string_free(digest);
brick_string_free(old);
2011-05-13 11:19:28 +00:00
return status;
}
2011-07-15 10:12:06 +00:00
static
2011-07-20 13:11:44 +00:00
int _check_versionlink(struct mars_global *global, struct mars_dent *parent, int sequence, loff_t end_pos)
2011-07-15 10:12:06 +00:00
{
char *my_log = NULL;
char *my_version = NULL;
char *log_prefix = NULL;
struct mars_dent *my_log_dent;
struct mars_dent *my_version_dent;
2011-09-27 08:46:14 +00:00
struct mars_dent **table = NULL;
int version_prefix_len;
int table_count;
int i;
int other_count = 0;
int ok_count = 0;
2011-07-15 10:12:06 +00:00
int status = -ENOMEM;
my_log = path_make("%s/log-%09d-%s", parent->d_path, sequence, my_id());
if (!my_log) {
2011-07-20 13:11:44 +00:00
goto out;
}
my_version = path_make("%s/version-%09d-%s", parent->d_path, sequence, my_id());
if (!my_version) {
2011-07-15 10:12:06 +00:00
goto out;
}
log_prefix = path_make("%s/log-%09d-", parent->d_path, sequence);
if (!log_prefix) {
2011-07-15 10:12:06 +00:00
goto out;
}
2011-09-27 08:46:14 +00:00
version_prefix_len = strlen(log_prefix);
2011-07-15 10:12:06 +00:00
status = -ENOENT;
my_log_dent = mars_find_dent(global, my_log);
2011-09-27 08:46:14 +00:00
if (!my_log_dent) {
MARS_WRN("cannot find logfile/symlink '%s'\n", my_log);
2011-07-20 13:11:44 +00:00
goto out;
}
my_version_dent = mars_find_dent(global, my_version);
if (!my_version_dent || !my_version_dent->new_link) {
2011-09-27 08:46:14 +00:00
MARS_WRN("cannot find version symlink '%s'\n", my_version);
2011-07-20 13:11:44 +00:00
goto out;
}
table_count = mars_find_dent_all(global, log_prefix, &table);
for (i = 0; i < table_count; i++) {
struct mars_dent *other_log_dent;
struct mars_dent *other_version_dent;
char *other_host;
char *other_version = NULL;
2011-09-27 08:46:14 +00:00
other_log_dent = table[i];
if (other_log_dent->new_link) {
MARS_DBG("'%s' is secondary\n", other_log_dent->d_path);
continue;
}
other_count++;
other_host = other_log_dent->d_path + version_prefix_len;
other_version = path_make("%s/version-%09d-%s", parent->d_path, sequence, other_host);
if (!other_version) {
MARS_ERR("cannot build path for '%s'\n", other_log_dent->d_path);
status = -ENOMEM;
goto out;
}
other_version_dent = mars_find_dent(global, other_version);
if (!other_version_dent || !other_version_dent->new_link) {
2011-09-27 08:46:14 +00:00
MARS_WRN("cannot find symlink '%s'\n", other_version);
} else if (!strcmp(my_version_dent->new_link, other_version_dent->new_link)) {
ok_count++;
}
brick_string_free(other_version);
2011-07-15 10:12:06 +00:00
}
status = 0;
if (other_count == 1 && ok_count > 0) {
2011-07-15 10:12:06 +00:00
status++;
MARS_DBG("versions OK\n");
} else {
MARS_DBG("versions MISMATCH #logfiles=%d #ok=%d\n", other_count, ok_count);
2011-07-15 10:12:06 +00:00
}
out:
brick_string_free(my_log);
brick_string_free(my_version);
brick_string_free(log_prefix);
brick_mem_free(table);
2011-07-20 13:11:44 +00:00
return status;
}
2011-02-23 20:48:06 +00:00
/* This must be called once at every round of logfile checking.
*/
static
2011-07-08 07:02:14 +00:00
int make_log_init(void *buf, struct mars_dent *dent)
2011-02-23 20:48:06 +00:00
{
struct mars_global *global = buf;
2011-07-08 07:02:14 +00:00
struct mars_dent *parent = dent->d_parent;
2011-02-23 20:48:06 +00:00
struct mars_brick *aio_brick;
struct mars_brick *trans_brick;
struct mars_rotate *rot = parent->d_private;
2011-02-28 18:00:32 +00:00
struct mars_dent *replay_link;
struct mars_dent *aio_dent;
2011-02-23 20:48:06 +00:00
struct mars_output *output;
2011-03-23 17:58:02 +00:00
const char *replay_path = NULL;
const char *aio_path = NULL;
const char *switch_path = NULL;
2011-02-23 20:48:06 +00:00
int status;
2011-08-25 10:16:32 +00:00
if (!global->global_power.button) {
return 0;
}
2011-02-23 20:48:06 +00:00
if (!rot) {
2011-08-12 11:09:48 +00:00
rot = brick_zmem_alloc(sizeof(struct mars_rotate));
2011-02-23 20:48:06 +00:00
parent->d_private = rot;
if (!rot) {
MARS_ERR("cannot allocate rot structure\n");
status = -ENOMEM;
goto done;
}
2011-03-01 18:00:14 +00:00
rot->global = global;
2011-02-23 20:48:06 +00:00
}
rot->replay_link = NULL;
rot->aio_dent = NULL;
rot->aio_brick = NULL;
rot->relevant_log = NULL;
2011-03-01 18:00:14 +00:00
rot->relevant_brick = NULL;
2011-06-17 11:32:38 +00:00
rot->next_relevant_log = NULL;
2011-02-23 20:48:06 +00:00
rot->prev_log = NULL;
rot->next_log = NULL;
rot->max_sequence = 0;
rot->has_error = false;
/* Fetch the replay status symlink.
* It must exist, and its value will control everything.
*/
2011-03-03 09:02:10 +00:00
replay_path = path_make("%s/replay-%s", parent->d_path, my_id());
2011-03-01 09:34:36 +00:00
if (unlikely(!replay_path)) {
MARS_ERR("cannot make path\n");
status = -ENOMEM;
goto done;
}
2011-02-23 20:48:06 +00:00
2011-03-01 09:34:36 +00:00
replay_link = (void*)mars_find_dent(global, replay_path);
if (unlikely(!replay_link || !replay_link->new_link)) {
2011-03-22 14:36:26 +00:00
MARS_DBG("replay status symlink '%s' does not exist (%p)\n", replay_path, replay_link);
2011-07-08 07:02:14 +00:00
rot->try_sync = false;
2011-02-23 20:48:06 +00:00
status = -ENOENT;
goto done;
}
2011-05-13 11:19:28 +00:00
status = _parse_args(replay_link, replay_link->new_link, 3);
2011-03-01 09:34:36 +00:00
if (unlikely(status < 0)) {
2011-02-23 20:48:06 +00:00
goto done;
}
rot->replay_link = replay_link;
/* Fetch the referenced AIO dentry.
*/
2011-03-03 09:02:10 +00:00
aio_path = path_make("%s/%s", parent->d_path, replay_link->d_argv[0]);
2011-03-01 09:34:36 +00:00
if (unlikely(!aio_path)) {
MARS_ERR("cannot make path\n");
status = -ENOMEM;
goto done;
}
2011-02-23 20:48:06 +00:00
2011-03-01 09:34:36 +00:00
aio_dent = (void*)mars_find_dent(global, aio_path);
2011-02-23 20:48:06 +00:00
if (unlikely(!aio_dent)) {
2011-03-22 14:36:26 +00:00
MARS_DBG("logfile '%s' does not exist\n", aio_path);
2011-02-23 20:48:06 +00:00
status = -ENOENT;
2011-09-27 08:46:14 +00:00
if (rot->todo_primary) { // try to create an empty logfile
2011-03-01 09:34:36 +00:00
_create_new_logfile(aio_path);
2011-02-23 20:48:06 +00:00
}
goto done;
}
rot->aio_dent = aio_dent;
/* Fetch / make the AIO brick instance
*/
2011-03-01 09:34:36 +00:00
aio_brick =
make_brick_all(global,
2011-03-02 09:30:56 +00:00
aio_dent,
2011-09-02 12:17:40 +00:00
false,
2011-05-06 10:25:52 +00:00
_set_aio_params,
2011-03-11 13:57:54 +00:00
NULL,
2011-03-02 09:30:56 +00:00
10 * HZ,
2011-03-01 09:34:36 +00:00
aio_path,
2011-03-07 10:27:38 +00:00
(const struct generic_brick_type*)&aio_brick_type,
2011-03-01 09:34:36 +00:00
(const struct generic_brick_type*[]){},
2011-03-23 08:09:00 +00:00
NULL,
2011-03-01 09:34:36 +00:00
"%s/%s",
(const char *[]){},
0,
parent->d_path,
replay_link->d_argv[0]);
2011-02-23 20:48:06 +00:00
if (!aio_brick) {
2011-03-01 09:34:36 +00:00
MARS_ERR("cannot access '%s'\n", aio_path);
status = -EIO;
goto done;
2011-02-23 20:48:06 +00:00
}
rot->aio_brick = (void*)aio_brick;
/* Fetch the actual logfile size
*/
output = aio_brick->outputs[0];
status = output->ops->mars_get_info(output, &rot->aio_info);
if (status < 0) {
2011-03-01 09:34:36 +00:00
MARS_ERR("cannot get info on '%s'\n", aio_path);
2011-02-23 20:48:06 +00:00
goto done;
}
2011-03-01 09:34:36 +00:00
MARS_DBG("logfile '%s' size = %lld\n", aio_path, rot->aio_info.current_size);
2011-02-23 20:48:06 +00:00
2011-03-23 17:58:02 +00:00
// check whether attach is allowed
2011-09-21 11:30:11 +00:00
switch_path = path_make("%s/todo-%s/attach", parent->d_path, my_id());
2011-03-23 17:58:02 +00:00
2011-03-01 09:34:36 +00:00
/* Fetch / make the transaction logger.
* We deliberately "forget" to connect the log input here.
2011-08-25 10:16:32 +00:00
* Will be carried out later in make_log_step().
2011-03-01 09:34:36 +00:00
* The final switch-on will be started in make_log_finalize().
2011-02-23 20:48:06 +00:00
*/
2011-03-01 09:34:36 +00:00
trans_brick =
make_brick_all(global,
2011-08-25 10:16:32 +00:00
dent,
2011-09-02 12:17:40 +00:00
false,
2011-03-11 13:57:54 +00:00
_set_trans_params,
NULL,
2011-03-01 09:34:36 +00:00
0,
aio_path,
(const struct generic_brick_type*)&trans_logger_brick_type,
2011-03-18 13:15:40 +00:00
(const struct generic_brick_type*[]){NULL},
2011-03-23 17:58:02 +00:00
switch_path,
2011-03-03 09:02:10 +00:00
"%s/logger",
(const char *[]){"%s/data-%s"},
2011-03-01 09:34:36 +00:00
1,
2011-03-03 09:02:10 +00:00
parent->d_path,
parent->d_path,
2011-03-01 09:34:36 +00:00
my_id());
status = -ENOENT;
2011-02-23 20:48:06 +00:00
if (!trans_brick) {
2011-03-01 09:34:36 +00:00
goto done;
2011-02-23 20:48:06 +00:00
}
rot->trans_brick = (void*)trans_brick;
/* For safety, default is to try an (unnecessary) replay in case
* something goes wrong later.
*/
rot->do_replay = true;
status = 0;
done:
2011-03-01 09:34:36 +00:00
if (aio_path)
2011-08-12 11:09:48 +00:00
brick_string_free(aio_path);
2011-03-01 09:34:36 +00:00
if (replay_path)
2011-08-12 11:09:48 +00:00
brick_string_free(replay_path);
2011-03-23 17:58:02 +00:00
if (switch_path)
2011-08-12 11:09:48 +00:00
brick_string_free(switch_path);
2011-02-23 20:48:06 +00:00
return status;
}
2011-06-17 11:32:38 +00:00
/* Note: this is strictly called in d_serial order.
* This is important!
2011-02-23 20:48:06 +00:00
*/
static
2011-08-25 10:16:32 +00:00
int make_log_step(void *buf, struct mars_dent *dent)
2011-02-23 20:48:06 +00:00
{
2011-06-17 11:32:38 +00:00
struct mars_global *global = buf;
2011-02-28 18:00:32 +00:00
struct mars_dent *parent = dent->d_parent;
2011-02-23 20:48:06 +00:00
struct mars_rotate *rot = parent->d_private;
2011-06-17 11:32:38 +00:00
struct trans_logger_brick *trans_brick;
struct mars_dent *prev_log;
2011-02-23 20:48:06 +00:00
int status = -EINVAL;
2011-06-17 11:32:38 +00:00
CHECK_PTR(rot, err);
2011-02-23 20:48:06 +00:00
status = 0;
2011-06-17 11:32:38 +00:00
trans_brick = rot->trans_brick;
if (!global->global_power.button || !dent->d_parent || !trans_brick || rot->has_error) {
MARS_DBG("nothing to do rot_error = %d\n", rot->has_error);
2011-02-23 20:48:06 +00:00
goto done;
}
2011-06-17 11:32:38 +00:00
/* Check for consecutiveness of logfiles
*/
prev_log = rot->next_log;
if (prev_log && prev_log->d_serial + 1 != dent->d_serial) {
MARS_ERR("transaction logs are not consecutive at '%s' (%d ~> %d)\n", dent->d_path, prev_log->d_serial, dent->d_serial);
status = -EINVAL;
2011-02-23 20:48:06 +00:00
goto done;
}
2011-06-17 11:32:38 +00:00
if (dent->d_serial > rot->max_sequence) {
rot->max_sequence = dent->d_serial;
}
/* Skip any logfiles after the relevant one.
* This should happen only when replaying multiple logfiles
* in sequence, or when starting a new logfile for writing.
*/
status = 0;
if (rot->relevant_log) {
if (!rot->next_relevant_log) {
rot->next_relevant_log = dent;
}
goto ok;
}
/* Preconditions
*/
if (!rot->replay_link || !rot->aio_dent || !rot->aio_brick) {
//MARS_DBG("nothing to do on '%s'\n", dent->d_path);
goto ok;
}
/* Remember the relevant logs.
*/
if (rot->aio_dent->d_serial == dent->d_serial) {
rot->relevant_log = dent;
}
ok:
/* All ok: switch over the indicators.
*/
rot->prev_log = rot->next_log;
rot->next_log = dent;
done:
if (status < 0) {
MARS_DBG("rot_error status = %d\n", status);
rot->has_error = true;
}
err:
return status;
}
/* Internal helper. Return codes:
* ret < 0 : error
* ret == 0 : not relevant
* ret == 1 : relevant, no transaction replay, switch to the next
* ret == 2 : relevant for transaction replay
* ret == 3 : relevant for appending
*/
static
int _check_logging_status(struct mars_rotate *rot, long long *oldpos_start, long long *oldpos_end, long long *newpos)
{
struct mars_dent *dent = rot->relevant_log;
struct mars_dent *parent;
2011-09-27 08:46:14 +00:00
struct mars_global *global = NULL;
2011-06-17 11:32:38 +00:00
int status = 0;
if (!dent)
goto done;
status = -EINVAL;
parent = dent->d_parent;
CHECK_PTR(parent, done);
global = rot->global;
2011-08-31 11:42:04 +00:00
CHECK_PTR_NULL(global, done);
2011-06-17 11:32:38 +00:00
CHECK_PTR(rot->replay_link, done);
CHECK_PTR(rot->aio_brick, done);
2011-05-13 11:19:28 +00:00
if (sscanf(rot->replay_link->d_argv[1], "%lld", oldpos_start) != 1) {
MARS_ERR("bad start position argument '%s'\n", rot->replay_link->d_argv[1]);
goto done;
}
if (sscanf(rot->replay_link->d_argv[2], "%lld", oldpos_end) != 1) {
MARS_ERR("bad end position argument '%s'\n", rot->replay_link->d_argv[2]);
2011-02-23 20:48:06 +00:00
goto done;
}
2011-05-13 11:19:28 +00:00
*oldpos_end += *oldpos_start;
if (unlikely(*oldpos_end < *oldpos_start)) {
MARS_ERR("end_pos %lld < start_pos %lld\n", *oldpos_end, *oldpos_start);
}
2011-02-23 20:48:06 +00:00
2011-05-13 11:19:28 +00:00
if (unlikely(rot->aio_info.current_size < *oldpos_start)) {
MARS_ERR("oops, bad replay position attempted at logfile '%s' (file length %lld should never be smaller than requested position %lld, is your filesystem corrupted?) => please repair this by hand\n", rot->aio_dent->d_path, rot->aio_info.current_size, *oldpos_start);
2011-06-17 11:32:38 +00:00
status = -EBADF;
2011-02-23 20:48:06 +00:00
goto done;
}
2011-06-17 11:32:38 +00:00
status = 0;
if (rot->aio_info.current_size > *oldpos_start || rot->aio_info.current_size < *oldpos_end) {
2011-05-13 11:19:28 +00:00
MARS_DBG("transaction log replay is necessary on '%s' from %lld to %lld (dirty region ends at %lld)\n", rot->aio_dent->d_path, *oldpos_start, rot->aio_info.current_size, *oldpos_end);
2011-02-23 20:48:06 +00:00
*newpos = rot->aio_info.current_size;
status = 2;
2011-06-17 11:32:38 +00:00
} else if (rot->next_relevant_log) {
MARS_DBG("transaction log '%s' is already applied, and the next one is available for switching\n", rot->aio_dent->d_path);
2011-02-23 20:48:06 +00:00
*newpos = rot->aio_info.current_size;
status = 1;
2011-09-27 08:46:14 +00:00
} else if (rot->todo_primary) {
2011-06-17 11:32:38 +00:00
if (!S_ISREG(dent->new_stat.mode)) {
MARS_DBG("transaction log '%s' is a symlink, therefore a fresh local logfile must be created\n", dent->d_path);
*newpos = rot->aio_info.current_size;
status = 1;
} else if (rot->aio_info.current_size > 0) {
MARS_DBG("transaction log '%s' is already applied (would be usable for appending at position %lld, but a fresh logfile will be used for safety reasons)\n", rot->aio_dent->d_path, *oldpos_end);
*newpos = rot->aio_info.current_size;
status = 1;
} else {
MARS_DBG("empty transaction log '%s' is usable for me as a primary node\n", rot->aio_dent->d_path);
status = 3;
}
2011-02-23 20:48:06 +00:00
} else {
2011-06-17 11:32:38 +00:00
MARS_DBG("transaction log '%s' is the last one, currently fully applied\n", rot->aio_dent->d_path);
status = 0;
2011-02-23 20:48:06 +00:00
}
done:
return status;
}
static
2011-06-17 11:32:38 +00:00
int _make_logging_status(struct mars_rotate *rot)
2011-02-23 20:48:06 +00:00
{
2011-06-17 11:32:38 +00:00
struct mars_dent *dent = rot->relevant_log;
struct mars_dent *parent;
2011-09-27 08:46:14 +00:00
struct mars_global *global = NULL;
2011-02-23 20:48:06 +00:00
struct trans_logger_brick *trans_brick;
loff_t start_pos = 0;
2011-05-13 11:19:28 +00:00
loff_t dirty_pos = 0;
2011-02-23 20:48:06 +00:00
loff_t end_pos = 0;
2011-06-17 11:32:38 +00:00
int status = 0;
2011-02-23 20:48:06 +00:00
2011-06-17 11:32:38 +00:00
if (!dent)
goto done;
status = -EINVAL;
parent = dent->d_parent;
CHECK_PTR(parent, done);
global = rot->global;
2011-08-31 11:42:04 +00:00
CHECK_PTR_NULL(global, done);
2011-02-23 20:48:06 +00:00
status = 0;
trans_brick = rot->trans_brick;
2011-06-17 11:32:38 +00:00
if (!global->global_power.button || !trans_brick || rot->has_error) {
2011-03-01 09:34:36 +00:00
MARS_DBG("nothing to do rot_error = %d\n", rot->has_error);
2011-02-23 20:48:06 +00:00
goto done;
}
/* Find current logging status.
*/
2011-06-17 11:32:38 +00:00
status = _check_logging_status(rot, &start_pos, &dirty_pos, &end_pos);
2011-02-23 20:48:06 +00:00
if (status < 0) {
goto done;
}
/* Relevant or not?
*/
switch (status) {
case 0: // not relevant
goto ok;
2011-04-08 09:52:46 +00:00
case 1: /* Relevant, and transaction replay already finished.
2011-06-17 11:32:38 +00:00
* Allow switching over to a new logfile.
2011-03-07 05:55:10 +00:00
*/
2011-07-15 10:12:06 +00:00
if (!trans_brick->power.button && !trans_brick->power.led_on && trans_brick->power.led_off &&
2011-09-27 08:46:14 +00:00
(rot->todo_primary || _check_versionlink(global, dent->d_parent, dent->d_serial, end_pos) > 0)) {
MARS_DBG("switching over transaction log '%s' from version %d to %d\n", dent->d_path, dent->d_serial, dent->d_serial + 1);
_update_replaylink(dent->d_parent, dent->d_serial + 1, 0, 0, !rot->todo_primary);
_update_versionlink(global, dent->d_parent, dent->d_serial + 1, 0, 0);
2011-04-08 09:52:46 +00:00
trans_brick->current_pos = 0;
rot->last_jiffies = jiffies;
2011-06-10 13:57:52 +00:00
//mars_trigger();
2011-02-23 20:48:06 +00:00
}
status = -EAGAIN;
goto done;
case 2: // relevant for transaction replay
MARS_DBG("replaying transaction log '%s' from %lld to %lld\n", dent->d_path, start_pos, end_pos);
rot->do_replay = true;
rot->start_pos = start_pos;
rot->end_pos = end_pos;
rot->relevant_log = dent;
break;
case 3: // relevant for appending
MARS_DBG("appending to transaction log '%s'\n", dent->d_path);
rot->do_replay = false;
rot->start_pos = 0;
rot->end_pos = 0;
rot->relevant_log = dent;
break;
default:
MARS_ERR("bad internal status %d\n", status);
status = -EINVAL;
goto done;
}
ok:
/* All ok: switch over the indicators.
*/
rot->prev_log = rot->next_log;
rot->next_log = dent;
done:
if (status < 0) {
2011-03-01 18:00:14 +00:00
MARS_DBG("rot_error status = %d\n", status);
2011-02-23 20:48:06 +00:00
rot->has_error = true;
}
return status;
}
static
2011-06-01 12:55:06 +00:00
void _change_trans(struct mars_rotate *rot)
2011-02-23 20:48:06 +00:00
{
struct trans_logger_brick *trans_brick = rot->trans_brick;
2011-07-11 14:01:28 +00:00
MARS_DBG("do_replay = %d start_pos = %lld end_pos = %lld\n", trans_brick->do_replay, rot->start_pos, rot->end_pos);
if (trans_brick->do_replay) {
2011-06-01 12:55:06 +00:00
trans_brick->replay_start_pos = rot->start_pos;
trans_brick->replay_end_pos = rot->end_pos;
2011-07-20 13:11:44 +00:00
trans_brick->replay_code = 0;
2011-06-01 12:55:06 +00:00
} else {
trans_brick->log_start_pos = rot->start_pos;
2011-02-23 20:48:06 +00:00
}
2011-06-01 12:55:06 +00:00
}
static
int _start_trans(struct mars_rotate *rot)
{
struct trans_logger_brick *trans_brick = rot->trans_brick;
struct trans_logger_input *trans_input;
int status;
2011-02-23 20:48:06 +00:00
/* Internal safety checks
*/
status = -EINVAL;
2011-06-01 12:55:06 +00:00
if (unlikely(!trans_brick)) {
MARS_ERR("logger instance does not exist\n");
goto done;
}
trans_input = trans_brick->inputs[TL_INPUT_FW_LOG1];
if (unlikely(!trans_input)) {
MARS_ERR("log input does not exist\n");
goto done;
}
2011-02-23 20:48:06 +00:00
if (unlikely(!rot->aio_brick || !rot->relevant_log)) {
MARS_ERR("something is missing, this should not happen\n");
goto done;
}
2011-06-01 12:55:06 +00:00
/* Update status when already working
*/
if (trans_brick->power.button || !trans_brick->power.led_off) {
_change_trans(rot);
status = 0;
2011-03-01 18:00:14 +00:00
goto done;
}
2011-06-01 12:55:06 +00:00
/* Really start transaction logging now.
* Check some preconditions.
*/
if (unlikely(rot->relevant_brick)) {
MARS_ERR("log aio brick already present, this should not happen\n");
2011-05-13 11:19:28 +00:00
goto done;
}
2011-02-23 20:48:06 +00:00
/* For safety, disconnect old connection first
*/
2011-05-13 11:19:28 +00:00
if (trans_input->connect) {
(void)generic_disconnect((void*)trans_input);
2011-02-23 20:48:06 +00:00
}
2011-03-01 18:00:14 +00:00
/* Open new transaction log
*/
rot->relevant_brick =
make_brick_all(rot->global,
2011-03-02 09:30:56 +00:00
rot->relevant_log,
2011-09-02 12:17:40 +00:00
false,
2011-03-11 13:57:54 +00:00
NULL,
NULL,
2011-03-01 18:00:14 +00:00
10 * HZ,
rot->relevant_log->d_path,
2011-03-07 10:27:38 +00:00
(const struct generic_brick_type*)&aio_brick_type,
2011-03-01 18:00:14 +00:00
(const struct generic_brick_type*[]){},
2011-03-23 08:09:00 +00:00
NULL,
2011-03-01 18:00:14 +00:00
rot->relevant_log->d_path,
(const char *[]){},
0);
if (!rot->relevant_brick) {
MARS_ERR("log aio brick not open\n");
goto done;
}
2011-02-23 20:48:06 +00:00
/* Connect to new transaction log
*/
2011-05-13 11:19:28 +00:00
status = generic_connect((void*)trans_input, (void*)rot->relevant_brick->outputs[0]);
2011-02-23 20:48:06 +00:00
if (status < 0) {
goto done;
}
/* Supply all relevant parameters
*/
2011-05-13 11:19:28 +00:00
trans_input->sequence = rot->relevant_log->d_serial;
2011-06-01 12:55:06 +00:00
trans_brick->do_replay = rot->do_replay;
_change_trans(rot);
2011-02-23 20:48:06 +00:00
/* Switch on....
*/
2011-03-23 17:58:02 +00:00
status = mars_power_button((void*)trans_brick, true, false);
2011-02-23 20:48:06 +00:00
MARS_DBG("status = %d\n", status);
done:
return status;
}
static
int _stop_trans(struct mars_rotate *rot)
{
struct trans_logger_brick *trans_brick = rot->trans_brick;
int status = 0;
2011-06-01 12:55:06 +00:00
if (!trans_brick) {
2011-02-23 20:48:06 +00:00
goto done;
}
2011-03-23 17:58:02 +00:00
/* Switch off temporarily....
2011-02-23 20:48:06 +00:00
*/
2011-03-23 17:58:02 +00:00
status = mars_power_button((void*)trans_brick, false, false);
2011-02-23 20:48:06 +00:00
MARS_DBG("status = %d\n", status);
2011-06-01 12:55:06 +00:00
if (status < 0) {
2011-02-23 20:48:06 +00:00
goto done;
}
2011-06-01 12:55:06 +00:00
/* Disconnect old connection(s)
2011-02-23 20:48:06 +00:00
*/
2011-06-01 12:55:06 +00:00
if (trans_brick->power.led_off) {
int i;
for (i = TL_INPUT_FW_LOG1; i <= TL_INPUT_BW_LOG2; i++) {
struct trans_logger_input *trans_input;
trans_input = trans_brick->inputs[i];
if (trans_input && trans_input->connect) {
(void)generic_disconnect((void*)trans_input);
}
}
2011-02-23 20:48:06 +00:00
}
done:
return status;
}
static
2011-08-25 10:16:32 +00:00
int make_log_finalize(struct mars_global *global, struct mars_dent *dent)
2011-02-23 20:48:06 +00:00
{
2011-08-25 10:16:32 +00:00
struct mars_dent *parent = dent->d_parent;
2011-02-23 20:48:06 +00:00
struct mars_rotate *rot = parent->d_private;
struct trans_logger_brick *trans_brick;
int status = -EINVAL;
CHECK_PTR(rot, done);
trans_brick = rot->trans_brick;
status = 0;
if (!trans_brick) {
MARS_DBG("nothing to do\n");
goto done;
}
2011-03-22 14:36:26 +00:00
2011-02-23 20:48:06 +00:00
/* Stopping is also possible in case of errors
*/
if (trans_brick->power.button && trans_brick->power.led_on && !trans_brick->power.led_off) {
2011-05-13 11:19:28 +00:00
bool do_stop = true;
if (trans_brick->do_replay) {
2011-07-20 13:11:44 +00:00
do_stop = trans_brick->replay_code != 0;
2011-05-13 11:19:28 +00:00
} else {
2011-06-17 11:32:38 +00:00
do_stop =
2011-09-27 08:46:14 +00:00
!rot->is_primary ||
2011-06-17 11:32:38 +00:00
(rot->relevant_log && rot->relevant_log != rot->current_log) ||
2011-09-27 08:46:14 +00:00
(rot->current_log && !S_ISREG(rot->current_log->new_stat.mode));
2011-05-13 11:19:28 +00:00
}
2011-06-17 11:32:38 +00:00
2011-02-23 20:48:06 +00:00
MARS_DBG("do_stop = %d\n", (int)do_stop);
if (do_stop || (long long)jiffies > rot->last_jiffies + 5 * HZ) {
2011-06-17 11:32:38 +00:00
struct trans_logger_input *old_input = NULL; // FIXME: kludge, do it right(tm)
2011-05-13 11:19:28 +00:00
int i;
for (i = TL_INPUT_FW_LOG1; i <= TL_INPUT_FW_LOG2; i++) {
struct trans_logger_input *trans_input;
trans_input = trans_brick->inputs[i];
if (!trans_input || trans_input == old_input) {
continue;
}
status = _update_replaylink(parent, trans_input->sequence, trans_input->replay_min_pos, trans_input->replay_max_pos, true);
2011-09-27 08:46:14 +00:00
status = _update_versionlink(global, parent, trans_input->sequence, trans_input->replay_min_pos, trans_input->replay_max_pos);
2011-05-13 11:19:28 +00:00
old_input = trans_input;
}
2011-04-08 09:52:46 +00:00
rot->last_jiffies = jiffies;
2011-02-23 20:48:06 +00:00
}
if (do_stop) {
status = _stop_trans(rot);
}
goto done;
}
2011-06-17 11:32:38 +00:00
2011-05-13 11:19:28 +00:00
/* Starting is only possible when no error occurred.
2011-02-23 20:48:06 +00:00
*/
if (!rot->relevant_log || rot->has_error) {
MARS_DBG("nothing to do\n");
goto done;
}
/* Start when necessary
*/
if (!trans_brick->power.button && !trans_brick->power.led_on && trans_brick->power.led_off) {
2011-06-17 11:32:38 +00:00
bool do_start;
status = _make_logging_status(rot);
if (status <= 0) {
goto done;
}
do_start = (!rot->do_replay || rot->start_pos != rot->end_pos);
2011-02-23 20:48:06 +00:00
MARS_DBG("do_start = %d\n", (int)do_start);
if (do_start) {
status = _start_trans(rot);
rot->current_log = rot->relevant_log;
}
} else {
MARS_DBG("trans_brick %d %d %d\n", trans_brick->power.button, trans_brick->power.led_on, trans_brick->power.led_off);
}
done:
return status;
}
///////////////////////////////////////////////////////////////////////
// specific handlers
static
2011-02-28 18:00:32 +00:00
int make_primary(void *buf, struct mars_dent *dent)
2011-02-23 20:48:06 +00:00
{
2011-08-25 10:16:32 +00:00
struct mars_global *global = buf;
2011-02-28 18:00:32 +00:00
struct mars_dent *parent;
2011-02-27 14:17:58 +00:00
struct mars_rotate *rot;
2011-02-23 20:48:06 +00:00
int status = -EINVAL;
2011-08-25 10:16:32 +00:00
if (!global->global_power.button) {
status = 0;
goto done;
}
2011-02-27 14:17:58 +00:00
parent = dent->d_parent;
CHECK_PTR(parent, done);
rot = parent->d_private;
2011-02-23 20:48:06 +00:00
CHECK_PTR(rot, done);
2011-09-27 08:46:14 +00:00
rot->todo_primary =
global->global_power.button && dent->new_link && !strcmp(dent->new_link, my_id());
2011-07-20 13:11:44 +00:00
rot->is_primary =
2011-09-27 08:46:14 +00:00
rot->if_brick && !rot->if_brick->power.led_off;
2011-02-23 20:48:06 +00:00
status = 0;
done:
return status;
}
static
2011-03-18 13:15:40 +00:00
int make_bio(void *buf, struct mars_dent *dent)
2011-02-23 20:48:06 +00:00
{
struct mars_global *global = buf;
struct mars_brick *brick;
2011-02-27 14:17:58 +00:00
int status = 0;
2011-02-23 20:48:06 +00:00
2011-03-23 08:09:00 +00:00
if (!global->global_power.button) {
2011-02-27 14:17:58 +00:00
goto done;
2011-02-23 20:48:06 +00:00
}
2011-03-18 13:15:40 +00:00
if (mars_find_brick(global, NULL, dent->d_path)) {
2011-02-27 14:17:58 +00:00
goto done;
2011-02-23 20:48:06 +00:00
}
2011-03-01 09:34:36 +00:00
brick =
make_brick_all(global,
2011-03-02 09:30:56 +00:00
dent,
2011-09-02 12:17:40 +00:00
false,
2011-03-18 13:15:40 +00:00
_set_bio_params,
2011-03-11 13:57:54 +00:00
NULL,
2011-03-01 09:34:36 +00:00
10 * HZ,
dent->d_path,
2011-03-18 13:15:40 +00:00
(const struct generic_brick_type*)&bio_brick_type,
2011-03-01 09:34:36 +00:00
(const struct generic_brick_type*[]){},
2011-03-23 08:09:00 +00:00
NULL,
2011-03-01 09:34:36 +00:00
dent->d_path,
(const char *[]){},
0);
2011-02-27 14:17:58 +00:00
if (unlikely(!brick)) {
status = -ENXIO;
goto done;
}
2011-02-23 20:48:06 +00:00
brick->outputs[0]->output_name = dent->d_path;
2011-03-23 17:58:02 +00:00
status = mars_power_button((void*)brick, true, false);
2011-02-23 20:48:06 +00:00
if (status < 0) {
2011-08-25 10:16:32 +00:00
kill_any(buf, dent);
2011-02-23 20:48:06 +00:00
}
2011-02-27 14:17:58 +00:00
done:
2011-02-23 20:48:06 +00:00
return status;
}
2011-06-01 12:55:06 +00:00
static int make_replay(void *buf, struct mars_dent *dent)
2011-02-23 20:48:06 +00:00
{
struct mars_global *global = buf;
2011-02-28 18:00:32 +00:00
struct mars_dent *parent = dent->d_parent;
2011-02-23 20:48:06 +00:00
int status = 0;
2011-03-22 14:36:26 +00:00
if (!global->global_power.button || !parent || !dent->new_link) {
2011-02-23 20:48:06 +00:00
MARS_DBG("nothing to do\n");
goto done;
}
2011-08-25 10:16:32 +00:00
status = make_log_finalize(global, dent);
2011-02-23 20:48:06 +00:00
if (status < 0) {
MARS_DBG("logger not initialized\n");
goto done;
}
2011-06-01 12:55:06 +00:00
done:
return status;
}
2011-06-17 11:32:38 +00:00
static
int make_dev(void *buf, struct mars_dent *dent)
2011-06-01 12:55:06 +00:00
{
struct mars_global *global = buf;
struct mars_dent *parent = dent->d_parent;
2011-06-17 11:32:38 +00:00
struct mars_rotate *rot = NULL;
2011-06-01 12:55:06 +00:00
struct mars_brick *dev_brick;
struct if_brick *_dev_brick;
2011-07-15 10:12:06 +00:00
bool switch_on;
2011-06-01 12:55:06 +00:00
int status = 0;
2011-06-17 11:32:38 +00:00
if (!parent || !dent->new_link) {
MARS_ERR("nothing to do\n");
return -EINVAL;
}
2011-03-22 14:36:26 +00:00
rot = parent->d_private;
2011-06-17 11:32:38 +00:00
if (!rot) {
MARS_DBG("nothing to do\n");
2011-02-23 20:48:06 +00:00
goto done;
}
2011-07-15 10:12:06 +00:00
if (!rot->trans_brick) {
MARS_DBG("transaction logger does not exist\n");
2011-02-23 20:48:06 +00:00
goto done;
}
2011-09-27 11:57:04 +00:00
if (!global->global_power.button &&
(!rot->if_brick || rot->if_brick->power.led_off)) {
MARS_DBG("nothing to do\n");
goto done;
}
2011-02-23 20:48:06 +00:00
status = _parse_args(dent, dent->new_link, 1);
if (status < 0) {
2011-03-01 09:34:36 +00:00
MARS_DBG("fail\n");
2011-02-23 20:48:06 +00:00
goto done;
}
2011-03-01 18:00:14 +00:00
2011-07-15 10:12:06 +00:00
switch_on =
2011-07-20 13:11:44 +00:00
(rot->if_brick && atomic_read(&rot->if_brick->inputs[0]->open_count) > 0) ||
2011-09-27 08:46:14 +00:00
(rot->todo_primary &&
2011-07-20 13:11:44 +00:00
!rot->trans_brick->do_replay &&
rot->trans_brick->power.led_on);
2011-09-27 11:57:04 +00:00
if (!global->global_power.button) {
switch_on = false;
}
2011-07-15 10:12:06 +00:00
2011-03-01 09:34:36 +00:00
dev_brick =
make_brick_all(global,
dent,
2011-09-02 12:17:40 +00:00
false,
2011-03-11 13:57:54 +00:00
_set_if_params,
NULL,
2011-03-01 09:34:36 +00:00
10 * HZ,
2011-03-01 18:00:14 +00:00
dent->d_argv[0],
2011-03-07 10:27:38 +00:00
(const struct generic_brick_type*)&if_brick_type,
2011-03-01 09:34:36 +00:00
(const struct generic_brick_type*[]){(const struct generic_brick_type*)&trans_logger_brick_type},
2011-07-15 10:12:06 +00:00
switch_on ? NULL : "", // KLUDGE
2011-09-27 11:57:04 +00:00
"%s/device-%s",
2011-03-03 09:02:10 +00:00
(const char *[]){"%s/logger"},
2011-03-01 18:00:14 +00:00
1,
2011-03-22 14:36:26 +00:00
parent->d_path,
2011-03-03 09:02:10 +00:00
dent->d_argv[0],
2011-03-22 14:36:26 +00:00
parent->d_path);
2011-06-17 11:32:38 +00:00
rot->if_brick = (void*)dev_brick;
2011-02-23 20:48:06 +00:00
if (!dev_brick) {
2011-06-17 11:32:38 +00:00
MARS_DBG("device not shown\n");
goto done;
2011-02-23 20:48:06 +00:00
}
2011-09-27 11:57:04 +00:00
dev_brick->show_status = _show_brick_status;
2011-03-08 16:45:52 +00:00
_dev_brick = (void*)dev_brick;
#if 0
if (_dev_brick->has_closed) {
_dev_brick->has_closed = false;
MARS_INF("rotating logfile for '%s'\n", parent->d_name);
status = mars_power_button((void*)rot->trans_brick, false);
rot->relevant_log = NULL;
}
#endif
2011-02-23 20:48:06 +00:00
done:
2011-06-17 11:32:38 +00:00
_show_primary(rot, parent);
2011-02-23 20:48:06 +00:00
return status;
}
2011-09-27 11:57:04 +00:00
static
int kill_dev(void *buf, struct mars_dent *dent)
{
struct mars_dent *parent = dent->d_parent;
int status = kill_any(buf, dent);
if (status > 0 && parent) {
struct mars_rotate *rot = parent->d_private;
if (rot) {
rot->if_brick = NULL;
}
}
return status;
}
2011-02-28 18:00:32 +00:00
static int _make_direct(void *buf, struct mars_dent *dent)
2011-02-23 20:48:06 +00:00
{
struct mars_global *global = buf;
struct mars_brick *brick;
2011-03-31 16:16:00 +00:00
char *src_path = NULL;
2011-02-23 20:48:06 +00:00
int status;
2011-03-31 16:16:00 +00:00
bool do_dealloc = false;
2011-02-23 20:48:06 +00:00
if (!global->global_power.button || !dent->d_parent || !dent->new_link) {
return 0;
}
status = _parse_args(dent, dent->new_link, 2);
if (status < 0) {
2011-03-11 13:57:54 +00:00
MARS_DBG("parse status = %d\n", status);
2011-02-23 20:48:06 +00:00
goto done;
}
2011-03-31 16:16:00 +00:00
src_path = dent->d_argv[0];
if (src_path[0] != '/') {
src_path = path_make("%s/%s", dent->d_parent->d_path, dent->d_argv[0]);
2011-08-12 11:09:48 +00:00
if (!src_path) {
MARS_DBG("fail\n");
status = -ENOMEM;
goto done;
}
2011-03-31 16:16:00 +00:00
do_dealloc = true;
}
2011-03-01 09:34:36 +00:00
brick =
make_brick_all(global,
dent,
2011-09-02 12:17:40 +00:00
false,
2011-03-18 13:15:40 +00:00
_set_bio_params,
2011-03-11 13:57:54 +00:00
NULL,
2011-03-01 09:34:36 +00:00
10 * HZ,
2011-03-31 16:16:00 +00:00
src_path,
2011-03-18 13:15:40 +00:00
(const struct generic_brick_type*)&bio_brick_type,
2011-03-01 09:34:36 +00:00
(const struct generic_brick_type*[]){},
2011-03-23 08:09:00 +00:00
NULL,
2011-03-31 16:16:00 +00:00
"%s",
2011-03-01 09:34:36 +00:00
(const char *[]){},
2011-03-02 09:30:56 +00:00
0,
2011-03-31 16:16:00 +00:00
src_path);
2011-03-01 09:34:36 +00:00
status = -1;
2011-02-23 20:48:06 +00:00
if (!brick) {
2011-03-01 09:34:36 +00:00
MARS_DBG("fail\n");
goto done;
2011-02-23 20:48:06 +00:00
}
2011-03-01 09:34:36 +00:00
brick =
make_brick_all(global,
dent,
2011-09-02 12:17:40 +00:00
false,
2011-03-11 13:57:54 +00:00
_set_if_params,
NULL,
2011-03-01 09:34:36 +00:00
10 * HZ,
dent->d_argv[1],
2011-03-07 10:27:38 +00:00
(const struct generic_brick_type*)&if_brick_type,
2011-03-01 09:34:36 +00:00
(const struct generic_brick_type*[]){NULL},
2011-03-23 08:09:00 +00:00
NULL,
2011-09-27 11:57:04 +00:00
"%s/directdevice-%s",
2011-03-31 16:16:00 +00:00
(const char *[]){ "%s" },
2011-03-02 09:30:56 +00:00
1,
2011-03-03 09:02:10 +00:00
dent->d_parent->d_path,
dent->d_argv[1],
2011-03-31 16:16:00 +00:00
src_path);
2011-03-01 09:34:36 +00:00
status = -1;
2011-02-23 20:48:06 +00:00
if (!brick) {
2011-03-01 09:34:36 +00:00
MARS_DBG("fail\n");
goto done;
2011-02-23 20:48:06 +00:00
}
2011-03-01 09:34:36 +00:00
2011-02-23 20:48:06 +00:00
status = 0;
done:
MARS_DBG("status = %d\n", status);
2011-08-12 11:09:48 +00:00
if (do_dealloc && src_path)
brick_string_free(src_path);
2011-02-23 20:48:06 +00:00
return status;
}
2011-02-28 18:00:32 +00:00
static int _make_copy(void *buf, struct mars_dent *dent)
2011-02-23 20:48:06 +00:00
{
2011-02-25 11:46:38 +00:00
struct mars_global *global = buf;
2011-03-23 08:09:00 +00:00
const char *switch_path = NULL;
2011-03-22 14:36:26 +00:00
const char *copy_path = NULL;
2011-02-25 11:46:38 +00:00
int status;
2011-02-23 20:48:06 +00:00
2011-02-25 11:46:38 +00:00
if (!global->global_power.button || !dent->d_parent || !dent->new_link) {
return 0;
2011-02-23 20:48:06 +00:00
}
2011-02-25 11:46:38 +00:00
status = _parse_args(dent, dent->new_link, 2);
if (status < 0) {
goto done;
2011-02-23 20:48:06 +00:00
}
2011-03-22 14:36:26 +00:00
copy_path = backskip_replace(dent->d_path, '/', true, "/copy-");
if (unlikely(!copy_path)) {
status = -ENOMEM;
goto done;
}
2011-03-23 17:58:02 +00:00
// check whether connection is allowed
2011-09-21 11:30:11 +00:00
switch_path = path_make("%s/todo-%s/connect", dent->d_parent->d_path, my_id());
2011-02-23 20:48:06 +00:00
2011-03-23 08:09:00 +00:00
status = __make_copy(global, dent, switch_path, copy_path, dent->d_parent->d_path, (const char**)dent->d_argv, -1, NULL);
2011-02-23 20:48:06 +00:00
done:
MARS_DBG("status = %d\n", status);
2011-03-22 14:36:26 +00:00
if (copy_path)
2011-08-12 11:09:48 +00:00
brick_string_free(copy_path);
2011-03-23 17:58:02 +00:00
if (switch_path)
2011-08-12 11:09:48 +00:00
brick_string_free(switch_path);
2011-02-23 20:48:06 +00:00
return status;
}
2011-02-28 18:00:32 +00:00
static int make_sync(void *buf, struct mars_dent *dent)
2011-02-23 20:48:06 +00:00
{
struct mars_global *global = buf;
2011-07-08 07:02:14 +00:00
struct mars_rotate *rot;
2011-02-25 11:46:38 +00:00
loff_t start_pos = 0;
2011-07-08 07:02:14 +00:00
loff_t end_pos = 0;
struct mars_dent *size_dent;
2011-02-28 18:00:32 +00:00
struct mars_dent *connect_dent;
2011-02-25 11:46:38 +00:00
char *peer;
struct copy_brick *copy = NULL;
2011-03-02 09:30:56 +00:00
char *tmp = NULL;
2011-03-23 08:09:00 +00:00
const char *switch_path = NULL;
2011-03-22 14:36:26 +00:00
const char *copy_path = NULL;
const char *src = NULL;
const char *dst = NULL;
2011-02-23 20:48:06 +00:00
int status;
2011-03-23 08:09:00 +00:00
if (!global->global_power.button || !dent->d_parent || !dent->new_link) {
2011-02-23 20:48:06 +00:00
return 0;
}
2011-02-25 11:46:38 +00:00
/* Analyze replay position
*/
status = sscanf(dent->new_link, "%lld", &start_pos);
if (status != 1) {
MARS_ERR("bad syncstatus symlink syntax '%s' (%s)\n", dent->new_link, dent->d_path);
status = -EINVAL;
goto done;
}
2011-07-08 07:02:14 +00:00
rot = dent->d_parent->d_private;
if (rot) {
rot->try_sync = true;
}
/* Sync necessary?
*/
tmp = path_make("%s/size", dent->d_parent->d_path);
status = -ENOMEM;
if (unlikely(!tmp))
goto done;
size_dent = (void*)mars_find_dent(global, tmp);
if (!size_dent || !size_dent->new_link) {
2011-07-28 11:41:06 +00:00
MARS_ERR("cannot determine size '%s'\n", tmp);
2011-07-08 07:02:14 +00:00
status = -ENOENT;
goto done;
}
status = sscanf(size_dent->new_link, "%lld", &end_pos);
if (status != 1) {
MARS_ERR("bad size symlink syntax '%s' (%s)\n", size_dent->new_link, tmp);
status = -EINVAL;
goto done;
}
if (start_pos == end_pos) {
MARS_DBG("no data sync necessary, size = %lld\n", start_pos);
status = 0;
goto done;
}
2011-08-12 11:09:48 +00:00
brick_string_free(tmp);
2011-07-08 07:02:14 +00:00
2011-02-25 11:46:38 +00:00
/* Determine peer
*/
2011-03-03 09:02:10 +00:00
tmp = path_make("%s/connect-%s", dent->d_parent->d_path, my_id());
2011-03-02 09:30:56 +00:00
status = -ENOMEM;
if (unlikely(!tmp))
goto done;
connect_dent = (void*)mars_find_dent(global, tmp);
2011-02-25 11:46:38 +00:00
if (!connect_dent || !connect_dent->new_link) {
2011-03-02 09:30:56 +00:00
MARS_ERR("cannot determine peer, symlink '%s' is missing\n", tmp);
2011-02-25 11:46:38 +00:00
status = -ENOENT;
2011-02-23 20:48:06 +00:00
goto done;
}
2011-02-25 11:46:38 +00:00
peer = connect_dent->new_link;
/* Start copy
*/
2011-03-03 09:02:10 +00:00
src = path_make("data-%s@%s", peer, peer);
dst = path_make("data-%s", my_id());
2011-03-22 14:36:26 +00:00
copy_path = backskip_replace(dent->d_path, '/', true, "/copy-");
2011-03-01 18:00:14 +00:00
2011-03-23 17:58:02 +00:00
// check whether connection is allowed
2011-09-21 11:30:11 +00:00
switch_path = path_make("%s/todo-%s/sync", dent->d_parent->d_path, my_id());
2011-03-23 17:58:02 +00:00
2011-06-17 11:32:38 +00:00
status = -ENOMEM;
if (unlikely(!src || !dst || !copy_path || !switch_path))
goto done;
2011-02-25 11:46:38 +00:00
MARS_DBG("starting initial sync '%s' => '%s'\n", src, dst);
2011-03-01 18:00:14 +00:00
{
const char *argv[2] = { src, dst };
2011-03-23 08:09:00 +00:00
status = __make_copy(global, dent, switch_path, copy_path, dent->d_parent->d_path, argv, start_pos, &copy);
2011-03-01 18:00:14 +00:00
}
2011-02-23 20:48:06 +00:00
2011-02-25 11:46:38 +00:00
/* Update syncstatus symlink
*/
2011-06-17 11:32:38 +00:00
if (status >= 0 && copy &&
((copy->power.button && copy->power.led_on) ||
(copy->copy_last == copy->copy_end && copy->copy_end > 0))) {
2011-08-12 11:09:48 +00:00
brick_string_free(src);
brick_string_free(dst);
2011-03-03 09:02:10 +00:00
src = path_make("%lld", copy->copy_last);
dst = path_make("%s/syncstatus-%s", dent->d_parent->d_path, my_id());
2011-03-02 09:30:56 +00:00
status = -ENOMEM;
if (unlikely(!src || !dst))
goto done;
2011-02-27 14:17:58 +00:00
status = mars_symlink(src, dst, NULL, 0);
2011-02-25 11:46:38 +00:00
}
2011-02-23 20:48:06 +00:00
done:
MARS_DBG("status = %d\n", status);
2011-03-02 09:30:56 +00:00
if (tmp)
2011-08-12 11:09:48 +00:00
brick_string_free(tmp);
2011-03-01 18:00:14 +00:00
if (src)
2011-08-12 11:09:48 +00:00
brick_string_free(src);
2011-03-01 18:00:14 +00:00
if (dst)
2011-08-12 11:09:48 +00:00
brick_string_free(dst);
2011-03-22 14:36:26 +00:00
if (copy_path)
2011-08-12 11:09:48 +00:00
brick_string_free(copy_path);
2011-03-23 17:58:02 +00:00
if (switch_path)
2011-08-12 11:09:48 +00:00
brick_string_free(switch_path);
2011-02-23 20:48:06 +00:00
return status;
}
///////////////////////////////////////////////////////////////////////
// the order is important!
enum {
2011-02-25 11:46:38 +00:00
// root element: this must have index 0
CL_ROOT,
// replacement for DNS in kernelspace
2011-02-23 20:48:06 +00:00
CL_IPS,
CL_PEERS,
2011-02-25 11:46:38 +00:00
// resource definitions
2011-02-23 20:48:06 +00:00
CL_RESOURCE,
2011-06-10 13:57:52 +00:00
CL_DEFAULTS0,
CL_DEFAULTS,
CL_DEFAULTS_ITEMS0,
CL_DEFAULTS_ITEMS,
2011-09-21 11:30:11 +00:00
CL_TODO,
CL_TODO_ITEMS,
2011-06-10 13:57:52 +00:00
CL_ACTUAL,
CL_ACTUAL_ITEMS,
2011-02-25 11:46:38 +00:00
CL_CONNECT,
2011-02-23 20:48:06 +00:00
CL_DATA,
2011-07-08 07:02:14 +00:00
CL_SIZE,
2011-02-23 20:48:06 +00:00
CL_PRIMARY,
CL__FILE,
CL_SYNC,
CL__COPY,
CL__DIRECT,
2011-05-13 11:19:28 +00:00
CL_VERSION,
2011-02-23 20:48:06 +00:00
CL_LOG,
2011-06-01 12:55:06 +00:00
CL_REPLAYSTATUS,
2011-02-23 20:48:06 +00:00
CL_DEVICE,
};
/* Please keep the order the same as in the enum.
*/
static const struct light_class light_classes[] = {
/* Placeholder for root node /mars/
*/
[CL_ROOT] = {
},
/* Directory containing the addresses of all peers
*/
[CL_IPS] = {
.cl_name = "ips",
.cl_len = 3,
.cl_type = 'd',
.cl_father = CL_ROOT,
},
/* Anyone participating in a MARS cluster must
* be named here (symlink pointing to the IP address).
* We have no DNS in kernel space.
*/
[CL_PEERS] = {
.cl_name = "ip-",
.cl_len = 3,
.cl_type = 'l',
.cl_father = CL_IPS,
2011-08-31 11:42:04 +00:00
#ifdef RUN_PEERS
2011-02-24 16:37:32 +00:00
.cl_forward = make_scan,
#endif
2011-08-31 11:42:04 +00:00
.cl_backward = kill_scan,
2011-02-23 20:48:06 +00:00
},
/* Directory containing all items of a resource
*/
[CL_RESOURCE] = {
.cl_name = "resource-",
.cl_len = 9,
.cl_type = 'd',
.cl_father = CL_ROOT,
},
2011-06-10 13:57:52 +00:00
/* Subdirectory for defaults...
*/
[CL_DEFAULTS0] = {
.cl_name = "defaults",
.cl_len = 8,
.cl_type = 'd',
.cl_hostcontext = false,
.cl_father = CL_RESOURCE,
},
[CL_DEFAULTS] = {
.cl_name = "defaults-",
.cl_len = 9,
.cl_type = 'd',
2011-07-15 10:12:06 +00:00
.cl_hostcontext = false,
2011-06-10 13:57:52 +00:00
.cl_father = CL_RESOURCE,
},
/* ... and its contents
*/
[CL_DEFAULTS_ITEMS0] = {
.cl_name = "",
.cl_len = 0, // catch any
.cl_type = 'l',
.cl_father = CL_DEFAULTS0,
},
[CL_DEFAULTS_ITEMS] = {
.cl_name = "",
.cl_len = 0, // catch any
.cl_type = 'l',
.cl_father = CL_DEFAULTS,
},
2011-03-23 17:58:02 +00:00
/* Subdirectory for controlling items...
*/
2011-09-21 11:30:11 +00:00
[CL_TODO] = {
.cl_name = "todo-",
.cl_len = 5,
2011-03-23 17:58:02 +00:00
.cl_type = 'd',
2011-07-15 10:12:06 +00:00
.cl_hostcontext = false,
2011-03-23 17:58:02 +00:00
.cl_father = CL_RESOURCE,
},
/* ... and its contents
*/
2011-09-21 11:30:11 +00:00
[CL_TODO_ITEMS] = {
2011-03-23 17:58:02 +00:00
.cl_name = "",
.cl_len = 0, // catch any
.cl_type = 'l',
2011-09-21 11:30:11 +00:00
.cl_father = CL_TODO,
2011-03-23 17:58:02 +00:00
},
2011-06-10 13:57:52 +00:00
/* Subdirectory for actual state
*/
[CL_ACTUAL] = {
.cl_name = "actual-",
.cl_len = 7,
.cl_type = 'd',
2011-07-15 10:12:06 +00:00
.cl_hostcontext = false,
2011-06-10 13:57:52 +00:00
.cl_father = CL_RESOURCE,
},
/* ... and its contents
*/
[CL_ACTUAL_ITEMS] = {
.cl_name = "",
.cl_len = 0, // catch any
.cl_type = 'l',
.cl_father = CL_ACTUAL,
},
2011-02-25 11:46:38 +00:00
/* Symlink indicating the current peer
*/
[CL_CONNECT] = {
.cl_name = "connect-",
.cl_len = 8,
.cl_type = 'l',
2011-06-17 11:32:38 +00:00
.cl_hostcontext = false, // not used here
2011-02-25 11:46:38 +00:00
.cl_father = CL_RESOURCE,
},
2011-02-23 20:48:06 +00:00
/* File or symlink to the real device / real (sparse) file
* when hostcontext is missing, the corresponding peer will
* not participate in that resource.
*/
[CL_DATA] = {
.cl_name = "data-",
.cl_len = 5,
.cl_type = 'F',
.cl_hostcontext = true,
.cl_father = CL_RESOURCE,
2011-08-31 11:42:04 +00:00
#ifdef RUN_DATA
2011-03-18 13:15:40 +00:00
.cl_forward = make_bio,
2011-08-31 11:42:04 +00:00
#endif
2011-08-25 10:16:32 +00:00
.cl_backward = kill_any,
2011-02-23 20:48:06 +00:00
},
2011-07-08 07:02:14 +00:00
/* Symlink indicating the (common) size of the resource
*/
[CL_SIZE] = {
.cl_name = "size",
.cl_len = 4,
.cl_type = 'l',
.cl_hostcontext = false,
.cl_father = CL_RESOURCE,
2011-08-31 11:42:04 +00:00
#ifdef RUN_LOGINIT
2011-07-08 07:02:14 +00:00
.cl_forward = make_log_init,
2011-08-31 11:42:04 +00:00
#endif
2011-08-25 10:16:32 +00:00
.cl_backward = kill_any,
2011-07-08 07:02:14 +00:00
},
2011-02-23 20:48:06 +00:00
/* Symlink pointing to the name of the primary node
*/
[CL_PRIMARY] = {
.cl_name = "primary",
.cl_len = 7,
.cl_type = 'l',
.cl_hostcontext = false,
.cl_father = CL_RESOURCE,
2011-08-31 11:42:04 +00:00
#ifdef RUN_PRIMARY
2011-02-23 20:48:06 +00:00
.cl_forward = make_primary,
2011-08-31 11:42:04 +00:00
#endif
2011-02-23 20:48:06 +00:00
.cl_backward = NULL,
},
/* Only for testing: open local file
*/
[CL__FILE] = {
.cl_name = "_file-",
.cl_len = 6,
.cl_type = 'F',
.cl_serial = true,
.cl_hostcontext = true,
.cl_father = CL_RESOURCE,
2011-03-18 13:15:40 +00:00
.cl_forward = make_bio,
2011-08-25 10:16:32 +00:00
.cl_backward = kill_any,
2011-02-23 20:48:06 +00:00
},
/* symlink indicating the current status / end
* of initial data sync.
*/
[CL_SYNC] = {
.cl_name = "syncstatus-",
.cl_len = 11,
.cl_type = 'l',
.cl_hostcontext = true,
.cl_father = CL_RESOURCE,
2011-08-31 11:42:04 +00:00
#ifdef RUN_SYNCSTATUS
2011-02-23 20:48:06 +00:00
.cl_forward = make_sync,
2011-08-31 11:42:04 +00:00
#endif
2011-08-25 10:16:32 +00:00
.cl_backward = kill_any,
2011-02-23 20:48:06 +00:00
},
/* Only for testing: make a copy instance
*/
[CL__COPY] = {
.cl_name = "_copy-",
.cl_len = 6,
.cl_type = 'l',
.cl_serial = true,
.cl_hostcontext = true,
.cl_father = CL_RESOURCE,
.cl_forward = _make_copy,
2011-08-25 10:16:32 +00:00
.cl_backward = kill_any,
2011-02-23 20:48:06 +00:00
},
/* Only for testing: access local data
*/
[CL__DIRECT] = {
.cl_name = "_direct-",
.cl_len = 8,
.cl_type = 'l',
.cl_serial = true,
.cl_hostcontext = true,
.cl_father = CL_RESOURCE,
.cl_forward = _make_direct,
2011-08-25 10:16:32 +00:00
.cl_backward = kill_any,
2011-02-23 20:48:06 +00:00
},
2011-05-13 11:19:28 +00:00
/* Passive symlink indicating the split-brain crypto hash
*/
[CL_VERSION] = {
.cl_name = "version-",
.cl_len = 8,
.cl_type = 'l',
.cl_serial = true,
2011-07-15 10:12:06 +00:00
.cl_hostcontext = false,
2011-05-13 11:19:28 +00:00
.cl_father = CL_RESOURCE,
},
2011-02-23 20:48:06 +00:00
/* Logfiles for transaction logger
*/
[CL_LOG] = {
.cl_name = "log-",
.cl_len = 4,
.cl_type = 'F',
.cl_serial = true,
.cl_hostcontext = true,
.cl_father = CL_RESOURCE,
2011-08-31 11:42:04 +00:00
#ifdef RUN_LOGFILES
2011-08-25 10:16:32 +00:00
.cl_forward = make_log_step,
2011-02-23 20:48:06 +00:00
#endif
2011-08-31 11:42:04 +00:00
.cl_backward = kill_any,
2011-02-23 20:48:06 +00:00
},
2011-06-01 12:55:06 +00:00
/* Symlink indicating the last state of
* transaction log replay.
*/
[CL_REPLAYSTATUS] = {
.cl_name = "replay-",
.cl_len = 7,
.cl_type = 'l',
.cl_hostcontext = true,
.cl_father = CL_RESOURCE,
2011-08-31 11:42:04 +00:00
#ifdef RUN_REPLAY
2011-06-01 12:55:06 +00:00
.cl_forward = make_replay,
2011-08-31 11:42:04 +00:00
#endif
2011-08-25 10:16:32 +00:00
.cl_backward = kill_any,
2011-06-01 12:55:06 +00:00
},
2011-02-23 20:48:06 +00:00
/* Name of the device appearing at the primary
*/
[CL_DEVICE] = {
.cl_name = "device-",
.cl_len = 7,
.cl_type = 'l',
.cl_hostcontext = true,
.cl_father = CL_RESOURCE,
2011-08-31 11:42:04 +00:00
#ifdef RUN_DEVICE
2011-02-23 20:48:06 +00:00
.cl_forward = make_dev,
2011-08-31 11:42:04 +00:00
#endif
2011-09-27 11:57:04 +00:00
.cl_backward = kill_dev,
2011-02-23 20:48:06 +00:00
},
{}
};
/* Helper routine to pre-determine the relevance of a name from the filesystem.
*/
2011-03-23 17:58:02 +00:00
static int light_checker(struct mars_dent *parent, const char *_name, int namlen, unsigned int d_type, int *prefix, int *serial)
2011-02-23 20:48:06 +00:00
{
int class;
2011-03-01 18:00:14 +00:00
int status = -2;
#ifdef MARS_DEBUGGING
2011-08-12 11:09:48 +00:00
const char *name = brick_strdup(_name);
2011-03-01 18:00:14 +00:00
if (!name)
return -ENOMEM;
#else
const char *name = _name;
#endif
2011-02-23 20:48:06 +00:00
//MARS_DBG("trying '%s' '%s'\n", path, name);
for (class = CL_ROOT + 1; ; class++) {
const struct light_class *test = &light_classes[class];
int len = test->cl_len;
2011-03-23 17:58:02 +00:00
if (!test->cl_name) { // end of table
2011-02-23 20:48:06 +00:00
break;
2011-03-23 17:58:02 +00:00
}
2011-02-23 20:48:06 +00:00
//MARS_DBG(" testing class '%s'\n", test->cl_name);
2011-03-23 17:58:02 +00:00
#ifdef MARS_DEBUGGING
2011-02-23 20:48:06 +00:00
if (len != strlen(test->cl_name)) {
2011-03-23 17:58:02 +00:00
MARS_ERR("internal table '%s' mismatch: %d != %d\n", test->cl_name, len, (int)strlen(test->cl_name));
2011-02-23 20:48:06 +00:00
len = strlen(test->cl_name);
}
#endif
2011-03-23 17:58:02 +00:00
if (test->cl_father &&
(!parent || parent->d_class != test->cl_father)) {
continue;
}
if (len > 0 &&
(namlen < len || memcmp(name, test->cl_name, len))) {
continue;
}
//MARS_DBG("path '%s/%s' matches class %d '%s'\n", path, name, class, test->cl_name);
// check special contexts
if (test->cl_serial) {
int plus = 0;
int count;
count = sscanf(name+len, "%d%n", serial, &plus);
if (count < 1) {
//MARS_DBG("'%s' serial number mismatch at '%s'\n", name, name+len);
status = -1;
goto done;
2011-02-23 20:48:06 +00:00
}
2011-06-01 12:55:06 +00:00
//MARS_DBG("'%s' serial number = %d\n", name, *serial);
2011-03-23 17:58:02 +00:00
len += plus;
if (name[len] == '-')
len++;
}
if (prefix)
2011-02-23 20:48:06 +00:00
*prefix = len;
2011-03-23 17:58:02 +00:00
if (test->cl_hostcontext) {
if (memcmp(name+len, my_id(), namlen-len)) {
//MARS_DBG("context mismatch '%s' at '%s'\n", name, name+len);
status = -1;
goto done;
2011-02-23 20:48:06 +00:00
}
}
2011-03-23 17:58:02 +00:00
// all ok
status = class;
goto done;
2011-02-23 20:48:06 +00:00
}
2011-03-23 17:58:02 +00:00
2011-02-23 20:48:06 +00:00
//MARS_DBG("no match for '%s' '%s'\n", path, name);
2011-03-23 17:58:02 +00:00
2011-03-01 18:00:14 +00:00
done:
#ifdef MARS_DEBUGGING
2011-08-12 11:09:48 +00:00
brick_string_free(name);
2011-03-01 18:00:14 +00:00
#endif
return status;
2011-02-23 20:48:06 +00:00
}
/* Do some syntactic checks, then delegate work to the real worker functions
* from the light_classes[] table.
*/
static int light_worker(struct mars_global *global, struct mars_dent *dent, bool direction)
{
light_worker_fn worker;
int class = dent->d_class;
if (class < 0 || class >= sizeof(light_classes)/sizeof(struct light_class)) {
MARS_ERR_ONCE(dent, "bad internal class %d of '%s'\n", class, dent->d_path);
return -EINVAL;
}
switch (light_classes[class].cl_type) {
case 'd':
if (!S_ISDIR(dent->new_stat.mode)) {
MARS_ERR_ONCE(dent, "'%s' should be a directory, but is something else\n", dent->d_path);
return -EINVAL;
}
break;
case 'f':
if (!S_ISREG(dent->new_stat.mode)) {
MARS_ERR_ONCE(dent, "'%s' should be a regular file, but is something else\n", dent->d_path);
return -EINVAL;
}
break;
case 'F':
if (!S_ISREG(dent->new_stat.mode) && !S_ISLNK(dent->new_stat.mode)) {
MARS_ERR_ONCE(dent, "'%s' should be a regular file or a symlink, but is something else\n", dent->d_path);
return -EINVAL;
}
break;
case 'l':
if (!S_ISLNK(dent->new_stat.mode)) {
MARS_ERR_ONCE(dent, "'%s' should be a symlink, but is something else\n", dent->d_path);
return -EINVAL;
}
break;
}
if (likely(class > CL_ROOT)) {
int father = light_classes[class].cl_father;
if (father == CL_ROOT) {
if (unlikely(dent->d_parent)) {
MARS_ERR_ONCE(dent, "'%s' is not at the root of the hierarchy\n", dent->d_path);
return -EINVAL;
}
} else if (unlikely(!dent->d_parent || dent->d_parent->d_class != father)) {
MARS_ERR_ONCE(dent, "last component '%s' from '%s' is at the wrong position in the hierarchy (class = %d, parent_class = %d, parent = '%s')\n", dent->d_name, dent->d_path, father, dent->d_parent ? dent->d_parent->d_class : -9999, dent->d_parent ? dent->d_parent->d_path : "");
return -EINVAL;
}
}
if (direction) {
worker = light_classes[class].cl_backward;
} else {
worker = light_classes[class].cl_forward;
}
if (worker) {
int status;
//MARS_DBG("working %s on '%s' rest='%s'\n", direction ? "backward" : "forward", dent->d_path, dent->d_rest);
status = worker(global, (void*)dent);
MARS_DBG("worked %s on '%s', status = %d\n", direction ? "backward" : "forward", dent->d_path, status);
return status;
}
return 0;
}
2011-03-03 09:02:10 +00:00
#ifdef STAT_DEBUGGING
2011-08-31 11:42:04 +00:00
static
void _show_one(struct mars_brick *test, int *brick_count)
{
int i;
if (*brick_count) {
MARS_STAT("---------\n");
}
MARS_STAT("BRICK type = %s path = '%s' name = '%s' button = %d off = %d on = %d\n", SAFE_STR(test->type->type_name), SAFE_STR(test->brick_path), SAFE_STR(test->brick_name), test->power.button, test->power.led_off, test->power.led_on);
2011-08-31 11:42:04 +00:00
(*brick_count)++;
if (test->ops && test->ops->brick_statistics) {
char *info = test->ops->brick_statistics(test, 0);
if (info) {
MARS_STAT(" %s", info);
brick_string_free(info);
}
}
for (i = 0; i < test->nr_inputs; i++) {
struct mars_input *input = test->inputs[i];
struct mars_output *output = input ? input->connect : NULL;
if (output) {
MARS_STAT(" input %d connected with %s path = '%s' name = '%s'\n", i, SAFE_STR(output->brick->type->type_name), SAFE_STR(output->brick->brick_path), SAFE_STR(output->brick->brick_name));
} else {
MARS_STAT(" input %d not connected\n", i);
}
}
for (i = 0; i < test->nr_outputs; i++) {
struct mars_output *output = test->outputs[i];
MARS_STAT(" output %d nr_connected = %d\n", i, output->nr_connected);
}
}
2011-03-03 09:02:10 +00:00
static
void _show_statist(struct mars_global *global)
{
struct list_head *tmp;
int dent_count = 0;
int brick_count = 0;
2011-08-12 11:09:48 +00:00
brick_mem_statistics();
2011-06-10 13:57:52 +00:00
down_read(&global->brick_mutex);
2011-08-31 11:42:04 +00:00
MARS_STAT("================================== ordinary bricks:\n");
2011-03-03 09:02:10 +00:00
for (tmp = global->brick_anchor.next; tmp != &global->brick_anchor; tmp = tmp->next) {
struct mars_brick *test;
test = container_of(tmp, struct mars_brick, global_brick_link);
2011-08-31 11:42:04 +00:00
_show_one(test, &brick_count);
}
MARS_STAT("================================== server bricks:\n");
for (tmp = global->server_anchor.next; tmp != &global->server_anchor; tmp = tmp->next) {
struct mars_brick *test;
test = container_of(tmp, struct mars_brick, global_brick_link);
_show_one(test, &brick_count);
2011-03-03 09:02:10 +00:00
}
2011-06-10 13:57:52 +00:00
up_read(&global->brick_mutex);
2011-03-03 09:02:10 +00:00
2011-08-25 10:16:32 +00:00
MARS_STAT("================================== dents:\n");
down_read(&global->dent_mutex);
for (tmp = global->dent_anchor.next; tmp != &global->dent_anchor; tmp = tmp->next) {
struct mars_dent *dent;
struct list_head *sub;
dent = container_of(tmp, struct mars_dent, dent_link);
2011-08-31 11:42:04 +00:00
MARS_STAT("dent %d '%s' '%s' stamp=%ld.%09ld\n", dent->d_class, SAFE_STR(dent->d_path), SAFE_STR(dent->new_link), dent->new_stat.mtime.tv_sec, dent->new_stat.mtime.tv_nsec);
2011-08-25 10:16:32 +00:00
dent_count++;
for (sub = dent->brick_list.next; sub != &dent->brick_list; sub = sub->next) {
struct mars_brick *test;
test = container_of(sub, struct mars_brick, dent_brick_link);
2011-08-31 11:42:04 +00:00
MARS_STAT(" owner of brick '%s'\n", SAFE_STR(test->brick_path));
2011-08-25 10:16:32 +00:00
}
}
up_read(&global->dent_mutex);
2011-03-23 17:58:02 +00:00
MARS_INF("==================== STATISTICS: %d dents, %d bricks\n", dent_count, brick_count);
2011-03-03 09:02:10 +00:00
}
#endif
2011-08-31 11:42:04 +00:00
static struct mars_global _global = {
.dent_anchor = LIST_HEAD_INIT(_global.dent_anchor),
.brick_anchor = LIST_HEAD_INIT(_global.brick_anchor),
.server_anchor = LIST_HEAD_INIT(_global.server_anchor),
.global_power = {
.button = true,
},
.dent_mutex = __RWSEM_INITIALIZER(_global.dent_mutex),
.brick_mutex = __RWSEM_INITIALIZER(_global.brick_mutex),
.main_event = __WAIT_QUEUE_HEAD_INITIALIZER(_global.main_event),
};
2011-02-23 20:48:06 +00:00
static int light_thread(void *data)
{
char *id = my_id();
int status = 0;
2011-08-31 11:42:04 +00:00
mars_global = &_global;
2011-02-23 20:48:06 +00:00
if (!id || strlen(id) < 2) {
MARS_ERR("invalid hostname\n");
status = -EFAULT;
goto done;
}
2011-03-02 09:30:56 +00:00
//fake_mm();
2011-02-23 20:48:06 +00:00
MARS_INF("-------- starting as host '%s' ----------\n", id);
2011-08-31 11:42:04 +00:00
while (_global.global_power.button || !list_empty(&_global.brick_anchor)) {
2011-02-23 20:48:06 +00:00
int status;
2011-08-31 11:42:04 +00:00
_global.global_power.button = !kthread_should_stop();
2011-09-02 12:17:40 +00:00
#if 1
2011-08-31 11:42:04 +00:00
if (!_global.global_power.button) {
mars_kill_brick_all(&_global, &_global.server_anchor, false);
}
2011-09-02 12:17:40 +00:00
#endif
2011-02-23 20:48:06 +00:00
2011-08-31 11:42:04 +00:00
status = mars_dent_work(&_global, "/mars", sizeof(struct mars_dent), light_checker, light_worker, &_global, 3);
2011-02-23 20:48:06 +00:00
MARS_DBG("worker status = %d\n", status);
2011-09-27 11:57:04 +00:00
_show_status_all(&_global);
2011-03-03 09:02:10 +00:00
#ifdef STAT_DEBUGGING
2011-08-31 11:42:04 +00:00
_show_statist(&_global);
2011-03-03 09:02:10 +00:00
#endif
2011-06-10 13:57:52 +00:00
msleep(500);
2011-02-27 14:17:58 +00:00
2011-08-31 11:42:04 +00:00
wait_event_interruptible_timeout(_global.main_event, _global.main_trigger, 10 * HZ);
_global.main_trigger = false;
2011-02-23 20:48:06 +00:00
}
done:
MARS_INF("-------- cleaning up ----------\n");
2011-08-31 11:42:04 +00:00
mars_kill_brick_all(&_global, &_global.server_anchor, false);
2011-09-27 08:46:14 +00:00
mars_free_dent_all(&_global, &_global.dent_anchor);
2011-08-31 11:42:04 +00:00
mars_kill_brick_all(&_global, &_global.brick_anchor, false);
2011-02-23 20:48:06 +00:00
2011-09-27 11:57:04 +00:00
_show_status_all(&_global);
2011-08-25 10:16:32 +00:00
#ifdef STAT_DEBUGGING
2011-08-31 11:42:04 +00:00
_show_statist(&_global);
2011-08-25 10:16:32 +00:00
#endif
2011-02-23 20:48:06 +00:00
mars_global = NULL;
main_thread = NULL;
MARS_INF("-------- done status = %d ----------\n", status);
2011-03-02 09:30:56 +00:00
//cleanup_mm();
2011-02-23 20:48:06 +00:00
return status;
}
2011-08-25 10:16:32 +00:00
#ifdef CONFIG_MARS_HAVE_BIGMODULE
#define INIT_MAX 32
static char *exit_names[INIT_MAX] = {};
static void (*exit_fn[INIT_MAX])(void) = {};
static int exit_fn_nr = 0;
#define DO_INIT(name) \
do { \
if ((status = init_##name()) < 0) goto done; \
exit_names[exit_fn_nr] = #name; \
exit_fn[exit_fn_nr++] = exit_##name; \
} while (0)
#endif
2011-02-23 20:48:06 +00:00
static void __exit exit_light(void)
{
2011-08-25 10:16:32 +00:00
struct task_struct *thread;
MARS_DBG("====================== stopping everything...\n");
2011-02-23 20:48:06 +00:00
// TODO: make this thread-safe.
2011-08-25 10:16:32 +00:00
thread = main_thread;
2011-02-23 20:48:06 +00:00
if (thread) {
main_thread = NULL;
2011-08-25 10:16:32 +00:00
MARS_DBG("=== stopping light thread...\n");
2011-02-23 20:48:06 +00:00
kthread_stop_nowait(thread);
mars_trigger();
2011-08-25 10:16:32 +00:00
MARS_INF("stopping thread...\n");
2011-02-23 20:48:06 +00:00
kthread_stop(thread);
put_task_struct(thread);
}
2011-08-25 10:16:32 +00:00
while (exit_fn_nr > 0) {
MARS_DBG("=== stopping module %s ...\n", exit_names[exit_fn_nr - 1]);
exit_fn[--exit_fn_nr]();
}
MARS_DBG("====================== stopped everything.\n");
2011-02-23 20:48:06 +00:00
}
static int __init init_light(void)
{
2011-08-25 10:16:32 +00:00
int status = 0;
2011-02-23 20:48:06 +00:00
struct task_struct *thread;
2011-08-25 10:16:32 +00:00
#ifdef CONFIG_MARS_HAVE_BIGMODULE
/* be careful: order is important!
*/
DO_INIT(brick_mem);
DO_INIT(brick);
DO_INIT(mars);
DO_INIT(log_format);
DO_INIT(mars_net);
DO_INIT(mars_server);
DO_INIT(mars_client);
DO_INIT(mars_aio);
DO_INIT(mars_bio);
DO_INIT(mars_if);
DO_INIT(mars_copy);
DO_INIT(mars_trans_logger);
DO_INIT(sy);
DO_INIT(sy_net);
DO_INIT(mars_proc);
#endif
2011-02-23 20:48:06 +00:00
thread = kthread_create(light_thread, NULL, "mars_light");
if (IS_ERR(thread)) {
2011-08-25 10:16:32 +00:00
status = PTR_ERR(thread);
goto done;
2011-02-23 20:48:06 +00:00
}
get_task_struct(thread);
main_thread = thread;
wake_up_process(thread);
2011-03-07 05:55:10 +00:00
#if 1 // quirk: bump the memory reserve limits. TODO: determine right values.
{
extern int min_free_kbytes;
min_free_kbytes *= 4;
setup_per_zone_wmarks();
}
#endif
2011-08-25 10:16:32 +00:00
done:
if (status < 0) {
MARS_ERR("module init failed with status = %d, exiting.\n", status);
exit_light();
}
return status;
2011-02-23 20:48:06 +00:00
}
2011-03-03 09:02:10 +00:00
// force module loading
const void *dummy1 = &client_brick_type;
const void *dummy2 = &server_brick_type;
2011-02-23 20:48:06 +00:00
MODULE_DESCRIPTION("MARS Light");
MODULE_AUTHOR("Thomas Schoebel-Theuer <tst@1und1.de>");
MODULE_LICENSE("GPL");
module_init(init_light);
module_exit(exit_light);