mars/mars.h

347 lines
10 KiB
C
Raw Normal View History

2010-06-14 14:27:40 +00:00
// (c) 2010 Thomas Schoebel-Theuer / 1&1 Internet AG
#ifndef MARS_H
#define MARS_H
#include <linux/list.h>
2011-02-23 20:48:06 +00:00
#include <linux/semaphore.h>
2010-07-30 11:50:20 +00:00
#include <asm/spinlock.h>
2010-07-23 11:55:18 +00:00
#include <asm/atomic.h>
2010-06-14 14:27:40 +00:00
2010-12-10 17:40:20 +00:00
#define MARS_DELAY /**/
//#define MARS_DELAY msleep(20000)
2010-08-20 10:58:24 +00:00
2011-02-23 20:48:06 +00:00
#define MARS_FATAL "MARS_FATAL "
#define MARS_ERROR "MARS_ERROR "
#define MARS_INFO "MARS_INFO "
#define MARS_DEBUG "MARS_DEBUG "
#define _MARS_FMT(fmt) "[%s] " __BASE_FILE__ " %d %s(): " fmt, current->comm, __LINE__, __FUNCTION__
//#define _MARS_FMT(fmt) _BRICK_FMT(fmt)
2010-06-14 14:27:40 +00:00
2011-02-23 20:48:06 +00:00
#define MARS_FAT(fmt, args...) do { printk(MARS_FATAL _MARS_FMT(fmt), ##args); MARS_DELAY; } while (0)
#define MARS_ERR(fmt, args...) do { printk(MARS_ERROR _MARS_FMT(fmt), ##args); MARS_DELAY; } while (0)
#define MARS_INF(fmt, args...) do { printk(MARS_INFO _MARS_FMT(fmt), ##args); } while (0)
2010-07-23 11:55:18 +00:00
#ifdef MARS_DEBUGGING
2011-02-23 20:48:06 +00:00
#define MARS_DBG(fmt, args...) do { printk(MARS_DEBUG _MARS_FMT(fmt), ##args); } while (0)
2010-07-23 11:55:18 +00:00
#else
2010-06-16 13:21:30 +00:00
#define MARS_DBG(args...) /**/
2010-07-23 11:55:18 +00:00
#endif
2011-02-23 20:48:06 +00:00
#ifdef IO_DEBUGGING
#define MARS_IO MARS_DBG
#else
#define MARS_IO(args...) /*empty*/
#endif
2010-06-14 14:27:40 +00:00
2010-12-15 12:13:18 +00:00
#define BRICK_OBJ_MREF 0
2010-08-04 22:38:48 +00:00
#define BRICK_OBJ_NR 1
2010-06-14 14:27:40 +00:00
2010-08-05 15:54:48 +00:00
#define GFP_MARS GFP_NOIO
2010-08-04 17:32:04 +00:00
2010-07-23 11:55:18 +00:00
#include "brick.h"
2010-06-14 14:27:40 +00:00
/////////////////////////////////////////////////////////////////////////
// MARS-specific definitions
2010-06-20 18:55:34 +00:00
// object stuff
2010-12-15 12:13:18 +00:00
/* mref */
2010-07-23 11:55:18 +00:00
2010-12-15 12:13:18 +00:00
#define MREF_UPTODATE 1
#define MREF_READING 2
#define MREF_WRITING 4
2010-07-05 15:56:58 +00:00
2010-12-15 12:13:18 +00:00
extern const struct generic_object_type mref_type;
2010-07-05 15:56:58 +00:00
2010-12-15 12:13:18 +00:00
struct mref_aspect {
GENERIC_ASPECT(mref);
2010-07-05 15:56:58 +00:00
};
2010-12-15 12:13:18 +00:00
struct mref_aspect_layout {
GENERIC_ASPECT_LAYOUT(mref);
2010-07-05 15:56:58 +00:00
};
2010-12-15 12:13:18 +00:00
struct mref_object_layout {
GENERIC_OBJECT_LAYOUT(mref);
2010-07-05 15:56:58 +00:00
};
2010-12-15 12:13:18 +00:00
#define MREF_OBJECT(PREFIX) \
2010-07-23 11:55:18 +00:00
GENERIC_OBJECT(PREFIX); \
2010-08-02 16:31:10 +00:00
/* supplied by caller */ \
2010-08-05 15:54:48 +00:00
loff_t ref_pos; \
int ref_len; \
int ref_may_write; \
2010-11-26 13:45:10 +00:00
void *ref_data; /* preset to NULL for buffered IO */ \
2010-08-05 15:54:48 +00:00
/* maintained by the ref implementation, readable for callers */ \
int ref_flags; \
int ref_rw; \
2011-02-23 20:48:06 +00:00
int ref_id; /* not mandatory; may be used for identification */ \
2010-08-05 15:54:48 +00:00
/* maintained by the ref implementation, incrementable for \
* callers (but not decrementable! use ref_put()) */ \
atomic_t ref_count; \
2010-07-30 11:50:20 +00:00
/* callback part */ \
2010-08-08 14:02:54 +00:00
struct generic_callback *ref_cb; \
2010-12-15 11:58:22 +00:00
struct generic_callback _ref_cb; \
2010-07-23 11:55:18 +00:00
2010-12-15 12:13:18 +00:00
struct mref_object {
MREF_OBJECT(mref);
2010-07-23 11:55:18 +00:00
};
2010-06-20 18:55:34 +00:00
2010-07-07 14:09:16 +00:00
// internal helper structs
struct mars_info {
loff_t current_size;
2010-08-23 05:06:06 +00:00
int transfer_order;
int transfer_size;
2010-07-07 14:09:16 +00:00
struct file *backing_file;
};
2010-06-20 18:55:34 +00:00
// brick stuff
2010-06-28 05:53:46 +00:00
#define MARS_BRICK(PREFIX) \
GENERIC_BRICK(PREFIX); \
2011-02-23 20:48:06 +00:00
struct list_head brick_link; \
const char *brick_path; \
struct mars_global *global; \
2010-06-14 14:27:40 +00:00
struct mars_brick {
MARS_BRICK(mars);
};
2010-06-28 05:53:46 +00:00
#define MARS_INPUT(PREFIX) \
GENERIC_INPUT(PREFIX); \
2010-06-14 14:27:40 +00:00
struct mars_input {
MARS_INPUT(mars);
};
2010-06-28 05:53:46 +00:00
#define MARS_OUTPUT(PREFIX) \
GENERIC_OUTPUT(PREFIX); \
2010-06-14 14:27:40 +00:00
struct mars_output {
MARS_OUTPUT(mars);
};
2010-06-28 05:53:46 +00:00
#define MARS_BRICK_OPS(PREFIX) \
GENERIC_BRICK_OPS(PREFIX); \
int (*mars_dummy_op)(int); \
#define MARS_OUTPUT_OPS(PREFIX) \
GENERIC_OUTPUT_OPS(PREFIX); \
2010-08-03 09:38:12 +00:00
int (*mars_get_info)(struct PREFIX##_output *output, struct mars_info *info); \
2010-12-15 12:13:18 +00:00
/* mref */ \
int (*mref_get)(struct PREFIX##_output *output, struct mref_object *mref); \
void (*mref_io)(struct PREFIX##_output *output, struct mref_object *mref); \
void (*mref_put)(struct PREFIX##_output *output, struct mref_object *mref); \
2010-06-14 14:27:40 +00:00
// all non-extendable types
2010-07-30 11:50:20 +00:00
#define _MARS_TYPES(BRICK) \
\
2010-08-04 22:38:48 +00:00
struct BRICK##_brick_ops { \
MARS_BRICK_OPS(BRICK); \
}; \
\
2010-07-30 11:50:20 +00:00
struct BRICK##_output_ops { \
2010-08-04 22:38:48 +00:00
MARS_OUTPUT_OPS(BRICK); \
}; \
2010-06-28 05:53:46 +00:00
\
2010-08-04 22:38:48 +00:00
struct BRICK##_brick_type { \
GENERIC_BRICK_TYPE(BRICK); \
2010-06-28 05:53:46 +00:00
}; \
\
2010-07-30 11:50:20 +00:00
struct BRICK##_input_type { \
2010-08-04 22:38:48 +00:00
GENERIC_INPUT_TYPE(BRICK); \
2010-06-28 05:53:46 +00:00
}; \
\
2010-07-30 11:50:20 +00:00
struct BRICK##_output_type { \
2010-08-04 22:38:48 +00:00
GENERIC_OUTPUT_TYPE(BRICK); \
2010-06-28 05:53:46 +00:00
}; \
2010-08-04 22:38:48 +00:00
\
2010-08-08 14:02:54 +00:00
struct BRICK##_callback { \
GENERIC_CALLBACK(BRICK); \
}; \
\
2010-07-30 11:50:20 +00:00
GENERIC_MAKE_FUNCTIONS(BRICK); \
GENERIC_MAKE_CONNECT(BRICK,BRICK); \
2010-06-28 05:53:46 +00:00
2010-07-23 11:55:18 +00:00
2010-07-30 11:50:20 +00:00
#define MARS_TYPES(BRICK) \
\
_MARS_TYPES(BRICK) \
\
2010-08-01 20:21:18 +00:00
struct BRICK##_object_layout; \
\
2010-07-30 11:50:20 +00:00
GENERIC_MAKE_CONNECT(generic,BRICK); \
2010-08-01 20:21:18 +00:00
GENERIC_OBJECT_LAYOUT_FUNCTIONS(BRICK); \
2010-12-15 12:13:18 +00:00
GENERIC_ASPECT_LAYOUT_FUNCTIONS(BRICK,mref); \
GENERIC_ASPECT_FUNCTIONS(BRICK,mref); \
2010-07-23 11:55:18 +00:00
// instantiate all mars-specific functions
2010-12-15 12:13:18 +00:00
GENERIC_OBJECT_FUNCTIONS(mref);
2010-07-23 11:55:18 +00:00
2010-12-15 11:58:22 +00:00
// instantiate a pseudo base-class "mars"
_MARS_TYPES(mars);
GENERIC_OBJECT_LAYOUT_FUNCTIONS(mars);
2010-12-15 12:13:18 +00:00
GENERIC_ASPECT_FUNCTIONS(mars,mref);
2010-12-15 11:58:22 +00:00
2010-06-14 14:27:40 +00:00
/////////////////////////////////////////////////////////////////////////
2010-12-15 11:58:22 +00:00
// MARS-specific helpers
2010-06-14 14:27:40 +00:00
2010-07-30 11:50:20 +00:00
#define MARS_MAKE_STATICS(BRICK) \
2010-07-23 11:55:18 +00:00
\
2010-07-30 11:50:20 +00:00
int BRICK##_brick_nr = -EEXIST; \
EXPORT_SYMBOL_GPL(BRICK##_brick_nr); \
2010-07-23 11:55:18 +00:00
\
2010-12-15 12:13:18 +00:00
static const struct generic_aspect_type BRICK##_mref_aspect_type = { \
.aspect_type_name = #BRICK "_mref_aspect_type", \
.object_type = &mref_type, \
.aspect_size = sizeof(struct BRICK##_mref_aspect), \
.init_fn = BRICK##_mref_aspect_init_fn, \
.exit_fn = BRICK##_mref_aspect_exit_fn, \
2010-07-23 11:55:18 +00:00
}; \
\
2010-07-30 11:50:20 +00:00
static const struct generic_aspect_type *BRICK##_aspect_types[BRICK_OBJ_NR] = { \
2010-12-15 12:13:18 +00:00
[BRICK_OBJ_MREF] = &BRICK##_mref_aspect_type, \
2010-07-23 11:55:18 +00:00
}; \
2010-07-30 11:50:20 +00:00
2010-08-08 09:03:42 +00:00
#define _CHECK_ATOMIC(atom,OP,minval) \
2010-08-05 15:54:48 +00:00
do { \
2010-08-08 09:03:42 +00:00
int test = atomic_read(atom); \
2010-08-05 15:54:48 +00:00
if (test OP (minval)) { \
2010-08-08 09:03:42 +00:00
atomic_set(atom, minval); \
2010-12-15 11:58:22 +00:00
MARS_ERR("%d: atomic " #atom " " #OP " " #minval " (%d)\n", __LINE__, test); \
2010-08-05 15:54:48 +00:00
} \
} while (0)
2010-08-08 09:03:42 +00:00
#define CHECK_ATOMIC(atom,minval) \
_CHECK_ATOMIC(atom, <, minval)
2010-08-05 15:54:48 +00:00
2010-08-08 09:03:42 +00:00
#define CHECK_HEAD_EMPTY(head) \
2010-08-20 10:58:24 +00:00
if (unlikely(!list_empty(head))) { \
2010-08-08 09:03:42 +00:00
INIT_LIST_HEAD(head); \
2010-08-27 07:20:26 +00:00
MARS_ERR("%d: list_head " #head " (%p) not empty\n", __LINE__, head); \
2010-08-08 09:03:42 +00:00
} \
2010-08-23 05:06:06 +00:00
#define CHECK_PTR(ptr,label) \
if (unlikely(!(ptr))) { \
2010-08-27 07:20:26 +00:00
MARS_FAT("%d: ptr " #ptr " is NULL\n", __LINE__); \
2010-08-23 05:06:06 +00:00
goto label; \
}
#define _CHECK(ptr,label) \
if (unlikely(!(ptr))) { \
2010-08-27 07:20:26 +00:00
MARS_FAT("%d: condition " #ptr " is VIOLATED\n", __LINE__); \
2010-08-23 05:06:06 +00:00
goto label; \
}
2011-02-23 20:48:06 +00:00
extern const struct meta mars_info_meta[];
extern const struct meta mars_mref_meta[];
/////////////////////////////////////////////////////////////////////////
extern struct mars_global *mars_global;
extern void mars_trigger(void);
extern void mars_power_button(struct mars_brick *brick, bool val);
extern void mars_power_led_on(struct mars_brick *brick, bool val);
extern void mars_power_led_off(struct mars_brick *brick, bool val);
/////////////////////////////////////////////////////////////////////////
#ifdef _STRATEGY // call this only in strategy bricks, never in ordinary bricks
#define MARS_ARGV_MAX 4
extern char *my_id(void);
#define MARS_DENT(TYPE) \
struct list_head sub_link; \
struct TYPE *d_parent; \
char *d_argv[MARS_ARGV_MAX]; /* for internal use, will be automatically deallocated*/ \
char *d_args; /* ditto uninterpreted */ \
char *d_name; /* current path component */ \
char *d_rest; /* some "meaningful" rest of d_name*/ \
char *d_path; /* full absolute path */ \
int d_namelen; \
int d_pathlen; \
int d_depth; \
unsigned int d_type; /* from readdir() => often DT_UNKNOWN => don't rely on it, use new_stat.mode instead */ \
int d_class; /* for pre-grouping order */ \
int d_serial; /* for pre-grouping order */ \
int d_version; /* dynamic programming per call of mars_ent_work() */ \
char d_error; \
struct kstat new_stat; \
struct kstat old_stat; \
char *new_link; \
char *old_link; \
void *d_private;
struct mars_dent {
MARS_DENT(mars_dent);
};
extern const struct meta mars_timespec_meta[];
extern const struct meta mars_kstat_meta[];
extern const struct meta mars_dent_meta[];
struct mars_global {
struct list_head dent_anchor;
struct list_head brick_anchor;
struct generic_switch global_power;
struct semaphore mutex;
volatile bool main_trigger;
wait_queue_head_t main_event;
//void *private;
};
typedef int (*mars_dent_checker)(const char *path, const char *name, int namlen, unsigned int d_type, int *prefix, int *serial);
typedef int (*mars_dent_worker)(struct mars_global *global, struct mars_dent *dent, bool direction);
extern int mars_dent_work(struct mars_global *global, char *dirname, int allocsize, mars_dent_checker checker, mars_dent_worker worker, void *buf, int maxdepth);
extern struct mars_dent *_mars_find_dent(struct mars_global *global, const char *path);
extern struct mars_dent *mars_find_dent(struct mars_global *global, const char *path);
extern void mars_dent_free(struct mars_dent *dent);
extern void mars_dent_free_all(struct list_head *anchor);
extern struct mars_brick *mars_find_brick(struct mars_global *global, const void *brick_type, const char *path);
extern struct mars_brick *mars_make_brick(struct mars_global *global, const void *_brick_type, const char *path, const char *name);
#define MARS_ERR_ONCE(dent, args...) if (!dent->d_error++) MARS_ERR(args)
/* Kludge: our kernel threads will have no mm context, but need one
* for stuff like ioctx_alloc() / aio_setup_ring() etc
* which expect userspace resources.
* We fake one.
* TODO: factor out the userspace stuff from AIO such that
* this fake is no longer necessary.
* Even better: replace do_mmap() in AIO stuff by something
* more friendly to kernelspace apps.
*/
inline void fake_mm(void)
{
if (!current->mm) {
current->mm = &init_mm;
}
}
/* Cleanup faked mm, otherwise do_exit() will try to destroy
* the wrong thing....
*/
inline void cleanup_mm(void)
{
if (current->mm == &init_mm) {
current->mm = NULL;
}
}
extern int mars_mkdir(const char *path);
extern int mars_symlink(const char *oldpath, const char *newpath, const struct timespec *stamp);
extern int mars_rename(const char *oldpath, const char *newpath);
#endif
#endif