2011-07-22 10:43:40 +00:00
|
|
|
// (c) 2010 Thomas Schoebel-Theuer / 1&1 Internet AG
|
|
|
|
|
|
|
|
//#define BRICK_DEBUGGING
|
|
|
|
//#define MARS_DEBUGGING
|
2011-09-27 08:46:14 +00:00
|
|
|
//#define IO_DEBUGGING
|
2011-07-22 10:43:40 +00:00
|
|
|
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/string.h>
|
|
|
|
#include <linux/uaccess.h>
|
|
|
|
#include <linux/file.h>
|
|
|
|
#include <linux/fs.h>
|
|
|
|
#include <linux/utsname.h>
|
|
|
|
|
|
|
|
#include "strategy.h"
|
|
|
|
|
|
|
|
#include "../mars_client.h"
|
|
|
|
|
|
|
|
#include <linux/syscalls.h>
|
|
|
|
#include <linux/namei.h>
|
|
|
|
#include <linux/kthread.h>
|
2011-11-08 12:33:13 +00:00
|
|
|
#include <linux/statfs.h>
|
2011-07-22 10:43:40 +00:00
|
|
|
|
|
|
|
#define SKIP_BIO false
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// meta descriptions
|
|
|
|
|
|
|
|
const struct meta mars_kstat_meta[] = {
|
|
|
|
META_INI(ino, struct kstat, FIELD_INT),
|
|
|
|
META_INI(mode, struct kstat, FIELD_INT),
|
|
|
|
META_INI(size, struct kstat, FIELD_INT),
|
|
|
|
META_INI_SUB(atime, struct kstat, mars_timespec_meta),
|
|
|
|
META_INI_SUB(mtime, struct kstat, mars_timespec_meta),
|
|
|
|
META_INI_SUB(ctime, struct kstat, mars_timespec_meta),
|
|
|
|
META_INI(blksize, struct kstat, FIELD_INT),
|
|
|
|
{}
|
|
|
|
};
|
|
|
|
EXPORT_SYMBOL_GPL(mars_kstat_meta);
|
|
|
|
|
|
|
|
const struct meta mars_dent_meta[] = {
|
|
|
|
META_INI(d_name, struct mars_dent, FIELD_STRING),
|
|
|
|
META_INI(d_rest, struct mars_dent, FIELD_STRING),
|
|
|
|
META_INI(d_path, struct mars_dent, FIELD_STRING),
|
|
|
|
META_INI(d_namelen, struct mars_dent, FIELD_INT),
|
|
|
|
META_INI(d_pathlen, struct mars_dent, FIELD_INT),
|
|
|
|
META_INI(d_type, struct mars_dent, FIELD_INT),
|
|
|
|
META_INI(d_class, struct mars_dent, FIELD_INT),
|
|
|
|
META_INI(d_serial, struct mars_dent, FIELD_INT),
|
|
|
|
META_INI_SUB(new_stat,struct mars_dent, mars_kstat_meta),
|
|
|
|
META_INI_SUB(old_stat,struct mars_dent, mars_kstat_meta),
|
|
|
|
META_INI(new_link, struct mars_dent, FIELD_STRING),
|
|
|
|
META_INI(old_link, struct mars_dent, FIELD_STRING),
|
|
|
|
META_INI(d_args, struct mars_dent, FIELD_STRING),
|
|
|
|
META_INI(d_argv[0], struct mars_dent, FIELD_STRING),
|
|
|
|
META_INI(d_argv[1], struct mars_dent, FIELD_STRING),
|
|
|
|
META_INI(d_argv[2], struct mars_dent, FIELD_STRING),
|
|
|
|
META_INI(d_argv[3], struct mars_dent, FIELD_STRING),
|
|
|
|
{}
|
|
|
|
};
|
|
|
|
EXPORT_SYMBOL_GPL(mars_dent_meta);
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// some helpers
|
|
|
|
|
|
|
|
int mars_stat(const char *path, struct kstat *stat, bool use_lstat)
|
|
|
|
{
|
|
|
|
mm_segment_t oldfs;
|
|
|
|
int status;
|
|
|
|
|
|
|
|
oldfs = get_fs();
|
|
|
|
set_fs(get_ds());
|
|
|
|
if (use_lstat) {
|
|
|
|
status = vfs_lstat((char*)path, stat);
|
|
|
|
} else {
|
|
|
|
status = vfs_stat((char*)path, stat);
|
|
|
|
}
|
|
|
|
set_fs(oldfs);
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(mars_stat);
|
|
|
|
|
|
|
|
int mars_mkdir(const char *path)
|
|
|
|
{
|
|
|
|
mm_segment_t oldfs;
|
|
|
|
int status;
|
|
|
|
|
|
|
|
oldfs = get_fs();
|
|
|
|
set_fs(get_ds());
|
|
|
|
status = sys_mkdir(path, 0700);
|
|
|
|
set_fs(oldfs);
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(mars_mkdir);
|
|
|
|
|
2011-10-24 10:54:15 +00:00
|
|
|
int mars_unlink(const char *path)
|
|
|
|
{
|
|
|
|
mm_segment_t oldfs;
|
|
|
|
int status;
|
|
|
|
|
|
|
|
oldfs = get_fs();
|
|
|
|
set_fs(get_ds());
|
|
|
|
status = sys_unlink(path);
|
|
|
|
set_fs(oldfs);
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(mars_unlink);
|
|
|
|
|
2011-07-22 10:43:40 +00:00
|
|
|
int mars_symlink(const char *oldpath, const char *newpath, const struct timespec *stamp, uid_t uid)
|
|
|
|
{
|
|
|
|
char *tmp = backskip_replace(newpath, '/', true, "/.tmp-");
|
|
|
|
mm_segment_t oldfs;
|
2012-07-12 13:34:44 +00:00
|
|
|
struct kstat stat = {};
|
2012-12-18 04:50:04 +00:00
|
|
|
struct timespec times[2];
|
2011-07-22 10:43:40 +00:00
|
|
|
int status = -ENOMEM;
|
|
|
|
|
|
|
|
if (unlikely(!tmp))
|
|
|
|
goto done;
|
|
|
|
|
2012-07-12 13:34:44 +00:00
|
|
|
if (stamp)
|
2012-12-18 04:50:04 +00:00
|
|
|
memcpy(×[0], stamp, sizeof(times[0]));
|
|
|
|
else
|
|
|
|
get_lamport(×[0]);
|
|
|
|
|
2011-07-22 10:43:40 +00:00
|
|
|
oldfs = get_fs();
|
|
|
|
set_fs(get_ds());
|
2012-07-12 13:34:44 +00:00
|
|
|
/* Some filesystems have only full second resolution.
|
|
|
|
* Thus it may happen that the new timestamp is not
|
|
|
|
* truly moving forward when called twice shortly.
|
|
|
|
* This is a _workaround_, to be replaced by a better
|
|
|
|
* method somewhen.
|
|
|
|
*/
|
2012-12-18 04:50:04 +00:00
|
|
|
status = vfs_lstat((char*)newpath, &stat);
|
|
|
|
if (status >= 0 &&
|
|
|
|
!stamp &&
|
|
|
|
!stat.mtime.tv_nsec &&
|
|
|
|
times[0].tv_sec == stat.mtime.tv_sec) {
|
|
|
|
MARS_DBG("workaround timestamp tv_sec=%ld\n", stat.mtime.tv_sec);
|
|
|
|
times[0].tv_sec = stat.mtime.tv_sec + 1;
|
|
|
|
times[0].tv_nsec = 0;
|
2012-07-12 13:34:44 +00:00
|
|
|
}
|
|
|
|
|
2011-07-22 10:43:40 +00:00
|
|
|
(void)sys_unlink(tmp);
|
2012-07-12 13:34:44 +00:00
|
|
|
|
2011-07-22 10:43:40 +00:00
|
|
|
status = sys_symlink(oldpath, tmp);
|
|
|
|
|
2012-12-18 04:50:04 +00:00
|
|
|
if (status >= 0) {
|
2011-07-22 10:43:40 +00:00
|
|
|
sys_lchown(tmp, uid, 0);
|
2012-12-18 04:50:04 +00:00
|
|
|
memcpy(×[1], ×[0], sizeof(struct timespec));
|
2011-07-22 10:43:40 +00:00
|
|
|
status = do_utimes(AT_FDCWD, tmp, times, AT_SYMLINK_NOFOLLOW);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (status >= 0) {
|
2012-12-18 04:50:04 +00:00
|
|
|
set_lamport(×[0]);
|
2011-07-22 10:43:40 +00:00
|
|
|
status = mars_rename(tmp, newpath);
|
|
|
|
}
|
|
|
|
set_fs(oldfs);
|
2011-08-12 11:09:48 +00:00
|
|
|
brick_string_free(tmp);
|
2011-07-22 10:43:40 +00:00
|
|
|
|
|
|
|
done:
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(mars_symlink);
|
|
|
|
|
2012-12-12 14:10:26 +00:00
|
|
|
char *mars_readlink(const char *newpath)
|
|
|
|
{
|
|
|
|
char *res = brick_string_alloc(1024);
|
|
|
|
struct path path = {};
|
|
|
|
mm_segment_t oldfs;
|
|
|
|
struct inode *inode;
|
2012-12-17 17:10:22 +00:00
|
|
|
int status = -ENOMEM;
|
2012-12-12 14:10:26 +00:00
|
|
|
|
|
|
|
if (unlikely(!res))
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
oldfs = get_fs();
|
|
|
|
set_fs(get_ds());
|
|
|
|
|
|
|
|
status = user_path_at(AT_FDCWD, newpath, 0, &path);
|
|
|
|
if (unlikely(status < 0)) {
|
|
|
|
MARS_DBG("link '%s' does not exist, status = %d\n", newpath, status);
|
2012-12-17 17:10:22 +00:00
|
|
|
goto done_fs;
|
2012-12-12 14:10:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inode = path.dentry->d_inode;
|
|
|
|
if (unlikely(!inode)) {
|
|
|
|
MARS_ERR("link '%s' has invalid inode\n", newpath);
|
2012-12-17 17:10:22 +00:00
|
|
|
status = -EINVAL;
|
|
|
|
goto done_put;
|
2012-12-12 14:10:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
status = inode->i_op->readlink(path.dentry, res, 1024);
|
|
|
|
if (unlikely(status < 0)) {
|
|
|
|
MARS_ERR("cannot read link '%s', status = %d\n", newpath, status);
|
2012-12-18 04:50:04 +00:00
|
|
|
} else {
|
|
|
|
set_lamport(&inode->i_mtime);
|
2012-12-12 14:10:26 +00:00
|
|
|
}
|
|
|
|
|
2012-12-17 17:10:22 +00:00
|
|
|
done_put:
|
|
|
|
path_put(&path);
|
|
|
|
|
|
|
|
done_fs:
|
2012-12-12 14:10:26 +00:00
|
|
|
set_fs(oldfs);
|
|
|
|
done:
|
2012-12-17 17:10:22 +00:00
|
|
|
if (unlikely(status < 0)) {
|
|
|
|
brick_string_free(res);
|
|
|
|
res = NULL;
|
|
|
|
}
|
2012-12-12 14:10:26 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(mars_readlink);
|
|
|
|
|
2011-07-22 10:43:40 +00:00
|
|
|
int mars_rename(const char *oldpath, const char *newpath)
|
|
|
|
{
|
|
|
|
mm_segment_t oldfs;
|
|
|
|
int status;
|
|
|
|
|
|
|
|
oldfs = get_fs();
|
|
|
|
set_fs(get_ds());
|
|
|
|
status = sys_rename(oldpath, newpath);
|
|
|
|
set_fs(oldfs);
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(mars_rename);
|
|
|
|
|
|
|
|
int mars_chmod(const char *path, mode_t mode)
|
|
|
|
{
|
|
|
|
mm_segment_t oldfs;
|
|
|
|
int status;
|
|
|
|
|
|
|
|
oldfs = get_fs();
|
|
|
|
set_fs(get_ds());
|
|
|
|
status = sys_chmod(path, mode);
|
|
|
|
set_fs(oldfs);
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(mars_chmod);
|
|
|
|
|
|
|
|
int mars_lchown(const char *path, uid_t uid)
|
|
|
|
{
|
|
|
|
mm_segment_t oldfs;
|
|
|
|
int status;
|
|
|
|
|
|
|
|
oldfs = get_fs();
|
|
|
|
set_fs(get_ds());
|
|
|
|
status = sys_lchown(path, uid, 0);
|
|
|
|
set_fs(oldfs);
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(mars_lchown);
|
|
|
|
|
2012-01-23 17:40:15 +00:00
|
|
|
loff_t _compute_space(struct kstatfs *kstatfs, loff_t raw_val)
|
|
|
|
{
|
|
|
|
int fsize = kstatfs->f_frsize;
|
|
|
|
if (fsize <= 0)
|
|
|
|
fsize = kstatfs->f_bsize;
|
|
|
|
|
|
|
|
MARS_INF("fsize = %d raw_val = %lld\n", fsize, raw_val);
|
|
|
|
// illegal values? cannot do anything....
|
|
|
|
if (fsize <= 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
// prevent intermediate integer overflows
|
|
|
|
if (fsize <= 1024)
|
|
|
|
return raw_val / (1024 / fsize);
|
|
|
|
|
|
|
|
return raw_val * (fsize / 1024);
|
|
|
|
}
|
|
|
|
|
|
|
|
void mars_remaining_space(const char *fspath, loff_t *total, loff_t *remaining)
|
2011-11-08 12:33:13 +00:00
|
|
|
{
|
|
|
|
struct path path = {};
|
|
|
|
struct kstatfs kstatfs = {};
|
2012-01-23 17:40:15 +00:00
|
|
|
int res;
|
|
|
|
|
|
|
|
*total = *remaining = 0;
|
2011-11-08 12:33:13 +00:00
|
|
|
|
|
|
|
res = user_path_at(AT_FDCWD, fspath, 0, &path);
|
|
|
|
if (unlikely(res < 0)) {
|
|
|
|
MARS_ERR("cannot get fspath '%s'\n", fspath);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
if (unlikely(!path.dentry)) {
|
|
|
|
MARS_ERR("bad dentry for fspath '%s'\n", fspath);
|
|
|
|
res = -ENXIO;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2012-09-18 06:13:33 +00:00
|
|
|
#ifdef ST_RDONLY
|
|
|
|
res = vfs_statfs(&path, &kstatfs);
|
|
|
|
#else
|
2011-11-08 12:33:13 +00:00
|
|
|
res = vfs_statfs(path.dentry, &kstatfs);
|
2012-09-18 06:13:33 +00:00
|
|
|
#endif
|
2011-11-08 12:33:13 +00:00
|
|
|
if (unlikely(res < 0)) {
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2012-01-23 17:40:15 +00:00
|
|
|
*total = _compute_space(&kstatfs, kstatfs.f_blocks);
|
|
|
|
*remaining = _compute_space(&kstatfs, kstatfs.f_bfree);
|
2011-11-08 12:33:13 +00:00
|
|
|
|
|
|
|
done:
|
|
|
|
path_put(&path);
|
2012-01-23 17:40:15 +00:00
|
|
|
err: ;
|
2011-11-08 12:33:13 +00:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(mars_remaining_space);
|
|
|
|
|
2011-07-22 10:43:40 +00:00
|
|
|
//////////////////////////////////////////////////////////////
|
|
|
|
|
2012-11-14 09:46:36 +00:00
|
|
|
// thread binding
|
|
|
|
|
|
|
|
void bind_to_dent(struct mars_dent *dent, struct say_channel **ch)
|
|
|
|
{
|
|
|
|
if (!dent) {
|
|
|
|
if (*ch) {
|
|
|
|
remove_binding_from(*ch, current);
|
|
|
|
*ch = NULL;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Memoize the channel. This is executed only once for each dent.
|
|
|
|
if (unlikely(!dent->d_say_channel)) {
|
|
|
|
struct mars_dent *test = dent->d_parent;
|
|
|
|
for (;;) {
|
|
|
|
if (!test) {
|
|
|
|
dent->d_say_channel = default_channel;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (test->d_use_channel && test->d_path) {
|
|
|
|
dent->d_say_channel = make_channel(test->d_path);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
test = test->d_parent;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (dent->d_say_channel != *ch) {
|
|
|
|
if (*ch)
|
|
|
|
remove_binding_from(*ch, current);
|
|
|
|
*ch = dent->d_say_channel;
|
|
|
|
if (*ch)
|
|
|
|
bind_to_channel(*ch, current);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(bind_to_dent);
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////
|
|
|
|
|
2011-07-22 10:43:40 +00:00
|
|
|
// infrastructure
|
|
|
|
|
|
|
|
struct mars_global *mars_global = NULL;
|
|
|
|
EXPORT_SYMBOL_GPL(mars_global);
|
|
|
|
|
|
|
|
static
|
|
|
|
void __mars_trigger(void)
|
|
|
|
{
|
|
|
|
if (mars_global) {
|
|
|
|
mars_global->main_trigger = true;
|
2012-01-17 10:48:56 +00:00
|
|
|
wake_up_interruptible_all(&mars_global->main_event);
|
2011-07-22 10:43:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int mars_power_button(struct mars_brick *brick, bool val, bool force_off)
|
|
|
|
{
|
|
|
|
int status = 0;
|
|
|
|
bool oldval = brick->power.button;
|
|
|
|
|
|
|
|
if (force_off && !val)
|
|
|
|
brick->power.force_off = true;
|
|
|
|
|
|
|
|
if (brick->power.force_off)
|
|
|
|
val = false;
|
|
|
|
|
|
|
|
if (val != oldval) {
|
|
|
|
MARS_DBG("brick '%s' type '%s' power button %d -> %d\n", brick->brick_path, brick->type->type_name, oldval, val);
|
|
|
|
|
|
|
|
set_button(&brick->power, val, false);
|
|
|
|
|
|
|
|
if (brick->ops)
|
|
|
|
status = brick->ops->brick_switch(brick);
|
|
|
|
|
|
|
|
mars_trigger();
|
|
|
|
}
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(mars_power_button);
|
|
|
|
|
|
|
|
int mars_power_button_recursive(struct mars_brick *brick, bool val, bool force_off, int timeout)
|
|
|
|
{
|
|
|
|
int status = 0;
|
|
|
|
bool oldval = brick->power.button;
|
|
|
|
|
|
|
|
if (force_off && !val)
|
|
|
|
brick->power.force_off = true;
|
|
|
|
|
|
|
|
if (brick->power.force_off)
|
|
|
|
val = false;
|
|
|
|
|
|
|
|
if (val != oldval) {
|
|
|
|
brick_switch_t mode;
|
|
|
|
mode = (val ? BR_ON_ALL : (force_off ? BR_FREE_ALL : BR_OFF_ALL));
|
|
|
|
|
|
|
|
MARS_DBG("brick '%s' type '%s' power button %d -> %d (mode = %d)\n", brick->brick_path, brick->type->type_name, oldval, val, mode);
|
|
|
|
|
|
|
|
status = set_recursive_button((void*)brick, mode, timeout);
|
|
|
|
}
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(mars_power_button_recursive);
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// strategy layer
|
|
|
|
|
|
|
|
|
|
|
|
struct mars_cookie {
|
|
|
|
struct mars_global *global;
|
2011-10-24 10:54:15 +00:00
|
|
|
mars_dent_checker_fn checker;
|
2011-07-22 10:43:40 +00:00
|
|
|
char *path;
|
|
|
|
struct mars_dent *parent;
|
|
|
|
int pathlen;
|
|
|
|
int allocsize;
|
|
|
|
int depth;
|
|
|
|
};
|
|
|
|
|
|
|
|
static
|
|
|
|
int get_inode(char *newpath, struct mars_dent *dent)
|
|
|
|
{
|
|
|
|
mm_segment_t oldfs;
|
|
|
|
int status;
|
|
|
|
struct kstat tmp = {};
|
|
|
|
|
|
|
|
oldfs = get_fs();
|
|
|
|
set_fs(get_ds());
|
|
|
|
|
|
|
|
status = vfs_lstat(newpath, &tmp);
|
|
|
|
if (status < 0) {
|
2012-12-11 11:32:26 +00:00
|
|
|
MARS_WRN("cannot stat '%s', status = %d\n", newpath, status);
|
2011-07-22 10:43:40 +00:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(&dent->old_stat, &dent->new_stat, sizeof(dent->old_stat));
|
|
|
|
memcpy(&dent->new_stat, &tmp, sizeof(dent->new_stat));
|
|
|
|
|
|
|
|
if (S_ISLNK(dent->new_stat.mode)) {
|
|
|
|
struct path path = {};
|
|
|
|
int len = dent->new_stat.size;
|
|
|
|
struct inode *inode;
|
|
|
|
char *link;
|
|
|
|
|
|
|
|
if (unlikely(len <= 0)) {
|
|
|
|
MARS_ERR("symlink '%s' bad len = %d\n", newpath, len);
|
|
|
|
status = -EINVAL;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
status = user_path_at(AT_FDCWD, newpath, 0, &path);
|
|
|
|
if (unlikely(status < 0)) {
|
|
|
|
MARS_ERR("cannot read link '%s'\n", newpath);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
inode = path.dentry->d_inode;
|
|
|
|
|
|
|
|
status = -ENOMEM;
|
2011-08-25 10:16:32 +00:00
|
|
|
link = brick_string_alloc(0);
|
2011-07-22 10:43:40 +00:00
|
|
|
if (likely(link)) {
|
|
|
|
MARS_IO("len = %d\n", len);
|
|
|
|
status = inode->i_op->readlink(path.dentry, link, len + 1);
|
|
|
|
link[len] = '\0';
|
|
|
|
if (status < 0 ||
|
|
|
|
(dent->new_link && !strncmp(dent->new_link, link, len))) {
|
|
|
|
//MARS_IO("symlink no change '%s' -> '%s' (%s) status = %d\n", newpath, link, dent->new_link ? dent->new_link : "", status);
|
2011-08-12 11:09:48 +00:00
|
|
|
brick_string_free(link);
|
2011-07-22 10:43:40 +00:00
|
|
|
} else {
|
|
|
|
MARS_IO("symlink '%s' -> '%s' (%s) status = %d\n", newpath, link, dent->new_link ? dent->new_link : "", status);
|
2011-08-12 11:09:48 +00:00
|
|
|
brick_string_free(dent->old_link);
|
2011-07-22 10:43:40 +00:00
|
|
|
dent->old_link = dent->new_link;
|
|
|
|
dent->new_link = link;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
path_put(&path);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dent->new_link)
|
|
|
|
MARS_IO("symlink '%s'\n", dent->new_link);
|
|
|
|
|
|
|
|
done:
|
|
|
|
set_fs(oldfs);
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
int mars_filler(void *__buf, const char *name, int namlen, loff_t offset,
|
|
|
|
u64 ino, unsigned int d_type)
|
|
|
|
{
|
|
|
|
struct mars_cookie *cookie = __buf;
|
|
|
|
struct mars_global *global = cookie->global;
|
|
|
|
struct list_head *anchor = &global->dent_anchor;
|
|
|
|
struct mars_dent *dent;
|
|
|
|
struct list_head *tmp;
|
|
|
|
struct mars_dent *best = NULL;
|
|
|
|
char *newpath;
|
|
|
|
int prefix = 0;
|
|
|
|
int pathlen;
|
|
|
|
int class;
|
|
|
|
int serial = 0;
|
2012-11-14 09:46:36 +00:00
|
|
|
bool use_channel = false;
|
2011-07-22 10:43:40 +00:00
|
|
|
|
|
|
|
MARS_IO("ino = %llu len = %d offset = %lld type = %u\n", ino, namlen, offset, d_type);
|
|
|
|
|
|
|
|
if (name[0] == '.') {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-11-14 09:46:36 +00:00
|
|
|
class = cookie->checker(cookie->parent, name, namlen, d_type, &prefix, &serial, &use_channel);
|
2011-07-22 10:43:40 +00:00
|
|
|
if (class < 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
pathlen = cookie->pathlen;
|
2011-10-12 14:57:53 +00:00
|
|
|
newpath = brick_string_alloc(pathlen + namlen + 2);
|
2011-07-22 10:43:40 +00:00
|
|
|
if (unlikely(!newpath))
|
|
|
|
goto err_mem0;
|
|
|
|
memcpy(newpath, cookie->path, pathlen);
|
|
|
|
newpath[pathlen++] = '/';
|
|
|
|
memcpy(newpath + pathlen, name, namlen);
|
|
|
|
pathlen += namlen;
|
|
|
|
newpath[pathlen] = '\0';
|
|
|
|
|
|
|
|
MARS_IO("path = '%s'\n", newpath);
|
|
|
|
|
|
|
|
for (tmp = anchor->next; tmp != anchor; tmp = tmp->next) {
|
|
|
|
int cmp;
|
|
|
|
dent = container_of(tmp, struct mars_dent, dent_link);
|
|
|
|
cmp = strcmp(dent->d_path, newpath);
|
|
|
|
if (!cmp) {
|
|
|
|
goto found;
|
|
|
|
}
|
|
|
|
// keep the list sorted. find the next smallest member.
|
|
|
|
if ((dent->d_class < class ||
|
|
|
|
(dent->d_class == class &&
|
|
|
|
(dent->d_serial < serial ||
|
|
|
|
(dent->d_serial == serial &&
|
|
|
|
cmp < 0))))
|
|
|
|
&&
|
|
|
|
(!best ||
|
|
|
|
best->d_class < dent->d_class ||
|
|
|
|
(best->d_class == dent->d_class &&
|
|
|
|
(best->d_serial < dent->d_serial ||
|
|
|
|
(best->d_serial == dent->d_serial &&
|
|
|
|
strcmp(best->d_path, dent->d_path) < 0))))) {
|
|
|
|
best = dent;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-12 11:09:48 +00:00
|
|
|
dent = brick_zmem_alloc(cookie->allocsize);
|
2011-07-22 10:43:40 +00:00
|
|
|
if (unlikely(!dent))
|
|
|
|
goto err_mem1;
|
|
|
|
|
2011-08-25 10:16:32 +00:00
|
|
|
dent->d_name = brick_string_alloc(namlen + 1);
|
2011-07-22 10:43:40 +00:00
|
|
|
if (unlikely(!dent->d_name))
|
|
|
|
goto err_mem2;
|
|
|
|
memcpy(dent->d_name, name, namlen);
|
|
|
|
dent->d_name[namlen] = '\0';
|
|
|
|
dent->d_namelen = namlen;
|
2011-08-25 10:16:32 +00:00
|
|
|
dent->d_rest = brick_strdup(dent->d_name + prefix);
|
|
|
|
if (unlikely(!dent->d_rest))
|
|
|
|
goto err_mem3;
|
2011-07-22 10:43:40 +00:00
|
|
|
|
|
|
|
dent->d_path = newpath;
|
|
|
|
newpath = NULL;
|
|
|
|
dent->d_pathlen = pathlen;
|
|
|
|
|
|
|
|
INIT_LIST_HEAD(&dent->brick_list);
|
|
|
|
|
|
|
|
if (best) {
|
|
|
|
list_add(&dent->dent_link, &best->dent_link);
|
|
|
|
} else {
|
|
|
|
list_add_tail(&dent->dent_link, anchor);
|
|
|
|
}
|
|
|
|
|
|
|
|
found:
|
|
|
|
dent->d_type = d_type;
|
|
|
|
dent->d_class = class;
|
|
|
|
dent->d_serial = serial;
|
|
|
|
dent->d_parent = cookie->parent;
|
|
|
|
dent->d_depth = cookie->depth;
|
|
|
|
dent->d_global = global;
|
|
|
|
dent->d_killme = false;
|
2012-11-14 09:46:36 +00:00
|
|
|
dent->d_use_channel = use_channel;
|
2011-08-12 11:09:48 +00:00
|
|
|
brick_string_free(newpath);
|
2011-07-22 10:43:40 +00:00
|
|
|
return 0;
|
|
|
|
|
2011-08-25 10:16:32 +00:00
|
|
|
err_mem3:
|
|
|
|
brick_mem_free(dent->d_name);
|
2011-07-22 10:43:40 +00:00
|
|
|
err_mem2:
|
2011-08-12 11:09:48 +00:00
|
|
|
brick_mem_free(dent);
|
2011-07-22 10:43:40 +00:00
|
|
|
err_mem1:
|
2011-08-12 11:09:48 +00:00
|
|
|
brick_string_free(newpath);
|
2011-07-22 10:43:40 +00:00
|
|
|
err_mem0:
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int _mars_readdir(struct mars_cookie *cookie)
|
|
|
|
{
|
|
|
|
struct file *f;
|
|
|
|
mm_segment_t oldfs;
|
|
|
|
int status = 0;
|
|
|
|
|
|
|
|
oldfs = get_fs();
|
|
|
|
set_fs(get_ds());
|
|
|
|
f = filp_open(cookie->path, O_DIRECTORY | O_RDONLY, 0);
|
|
|
|
set_fs(oldfs);
|
|
|
|
if (unlikely(IS_ERR(f))) {
|
|
|
|
return PTR_ERR(f);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
status = vfs_readdir(f, mars_filler, cookie);
|
|
|
|
MARS_IO("vfs_readdir() status = %d\n", status);
|
|
|
|
if (status <= 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
filp_close(f, NULL);
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2011-10-24 10:54:15 +00:00
|
|
|
int mars_dent_work(struct mars_global *global, char *dirname, int allocsize, mars_dent_checker_fn checker, mars_dent_worker_fn worker, void *buf, int maxdepth)
|
2011-07-22 10:43:40 +00:00
|
|
|
{
|
|
|
|
static int version = 0;
|
|
|
|
struct mars_cookie cookie = {
|
|
|
|
.global = global,
|
|
|
|
.checker = checker,
|
|
|
|
.path = dirname,
|
|
|
|
.pathlen = strlen(dirname),
|
2012-08-06 07:38:21 +00:00
|
|
|
.parent = NULL,
|
2011-07-22 10:43:40 +00:00
|
|
|
.allocsize = allocsize,
|
|
|
|
.depth = 0,
|
|
|
|
};
|
2012-11-14 09:46:36 +00:00
|
|
|
struct say_channel *say_channel = NULL;
|
2011-07-22 10:43:40 +00:00
|
|
|
struct list_head *tmp;
|
|
|
|
struct list_head *next;
|
|
|
|
int rounds = 0;
|
|
|
|
int status;
|
|
|
|
int total_status = 0;
|
|
|
|
bool found_dir;
|
|
|
|
|
|
|
|
/* Initialize the flat dent list
|
|
|
|
*/
|
|
|
|
version++;
|
2011-10-10 13:03:10 +00:00
|
|
|
global->global_version = version;
|
2011-07-22 10:43:40 +00:00
|
|
|
total_status = _mars_readdir(&cookie);
|
|
|
|
|
|
|
|
if (total_status || !worker) {
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
down_write(&global->dent_mutex);
|
|
|
|
|
|
|
|
restart:
|
2012-01-17 10:48:56 +00:00
|
|
|
MARS_IO("at restart\n");
|
2011-07-22 10:43:40 +00:00
|
|
|
found_dir = false;
|
|
|
|
|
|
|
|
/* First, get all the inode information in a separate pass
|
|
|
|
* before starting work.
|
|
|
|
* The separate pass is necessary because some dents may
|
|
|
|
* forward-reference other dents, and it would be a pity if
|
|
|
|
* some inodes were not available or were outdated.
|
|
|
|
*/
|
|
|
|
for (tmp = global->dent_anchor.next; tmp != &global->dent_anchor; tmp = tmp->next) {
|
|
|
|
struct mars_dent *dent = container_of(tmp, struct mars_dent, dent_link);
|
|
|
|
// treat any member only once during this invocation
|
|
|
|
if (dent->d_version == version)
|
|
|
|
continue;
|
|
|
|
dent->d_version = version;
|
|
|
|
|
2012-11-14 09:46:36 +00:00
|
|
|
bind_to_dent(dent, &say_channel);
|
|
|
|
|
2012-01-17 10:48:56 +00:00
|
|
|
//MARS_IO("reading inode '%s'\n", dent->d_path);
|
2011-07-22 10:43:40 +00:00
|
|
|
status = get_inode(dent->d_path, dent);
|
|
|
|
total_status |= status;
|
|
|
|
|
|
|
|
// recurse into subdirectories by inserting into the flat list
|
|
|
|
if (S_ISDIR(dent->new_stat.mode) && dent->d_depth <= maxdepth) {
|
|
|
|
struct mars_cookie sub_cookie = {
|
|
|
|
.global = global,
|
|
|
|
.checker = checker,
|
|
|
|
.path = dent->d_path,
|
|
|
|
.pathlen = dent->d_pathlen,
|
|
|
|
.allocsize = allocsize,
|
|
|
|
.parent = dent,
|
|
|
|
.depth = dent->d_depth + 1,
|
|
|
|
};
|
|
|
|
found_dir = true;
|
|
|
|
status = _mars_readdir(&sub_cookie);
|
|
|
|
total_status |= status;
|
|
|
|
if (status < 0) {
|
2012-07-17 15:51:40 +00:00
|
|
|
MARS_INF("forward: status %d on '%s'\n", status, dent->d_path);
|
2011-07-22 10:43:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-11-14 09:46:36 +00:00
|
|
|
bind_to_dent(NULL, &say_channel);
|
2011-07-22 10:43:40 +00:00
|
|
|
|
|
|
|
if (found_dir && ++rounds < 10) {
|
2012-12-14 11:44:54 +00:00
|
|
|
brick_yield();
|
2011-07-22 10:43:40 +00:00
|
|
|
goto restart;
|
|
|
|
}
|
|
|
|
|
2011-10-24 10:54:15 +00:00
|
|
|
/* Preparation pass.
|
|
|
|
* Here is a chance to mark some dents for removal
|
|
|
|
* (or other types of non-destructive operations)
|
|
|
|
*/
|
2012-01-17 10:48:56 +00:00
|
|
|
MARS_IO("prep pass\n");
|
2011-10-24 10:54:15 +00:00
|
|
|
for (tmp = global->dent_anchor.next, next = tmp->next; tmp != &global->dent_anchor; tmp = next, next = next->next) {
|
|
|
|
struct mars_dent *dent = container_of(tmp, struct mars_dent, dent_link);
|
2012-11-13 11:50:48 +00:00
|
|
|
|
2012-12-14 11:44:54 +00:00
|
|
|
brick_yield();
|
2012-11-13 11:50:48 +00:00
|
|
|
|
2012-11-14 09:46:36 +00:00
|
|
|
bind_to_dent(dent, &say_channel);
|
|
|
|
|
2012-01-17 10:48:56 +00:00
|
|
|
//MARS_IO("forward prepare '%s'\n", dent->d_path);
|
2011-10-24 10:54:15 +00:00
|
|
|
status = worker(buf, dent, true, false);
|
2012-01-17 10:48:56 +00:00
|
|
|
if (status) {
|
|
|
|
//MARS_IO("forward treat '%s' status = %d\n", dent->d_path, status);
|
|
|
|
}
|
2011-10-24 10:54:15 +00:00
|
|
|
total_status |= status;
|
|
|
|
}
|
2012-11-14 09:46:36 +00:00
|
|
|
bind_to_dent(NULL, &say_channel);
|
2011-10-24 10:54:15 +00:00
|
|
|
|
2011-07-22 10:43:40 +00:00
|
|
|
/* Remove all dents marked for removal.
|
|
|
|
*/
|
2012-01-17 10:48:56 +00:00
|
|
|
MARS_IO("removal pass\n");
|
2011-07-22 10:43:40 +00:00
|
|
|
for (tmp = global->dent_anchor.next, next = tmp->next; tmp != &global->dent_anchor; tmp = next, next = next->next) {
|
|
|
|
struct mars_dent *dent = container_of(tmp, struct mars_dent, dent_link);
|
|
|
|
if (!dent->d_killme)
|
|
|
|
continue;
|
2012-11-14 09:46:36 +00:00
|
|
|
|
|
|
|
bind_to_dent(dent, &say_channel);
|
|
|
|
|
2011-07-22 10:43:40 +00:00
|
|
|
MARS_DBG("killing dent '%s'\n", dent->d_path);
|
|
|
|
list_del_init(tmp);
|
2011-09-27 08:46:14 +00:00
|
|
|
mars_free_dent(dent);
|
2011-07-22 10:43:40 +00:00
|
|
|
}
|
2012-11-14 09:46:36 +00:00
|
|
|
bind_to_dent(NULL, &say_channel);
|
2011-07-22 10:43:40 +00:00
|
|
|
|
|
|
|
up_write(&global->dent_mutex);
|
|
|
|
|
|
|
|
/* Forward pass.
|
|
|
|
*/
|
|
|
|
down_read(&global->dent_mutex);
|
2012-01-17 10:48:56 +00:00
|
|
|
MARS_IO("forward pass\n");
|
2011-07-22 10:43:40 +00:00
|
|
|
for (tmp = global->dent_anchor.next, next = tmp->next; tmp != &global->dent_anchor; tmp = next, next = next->next) {
|
|
|
|
struct mars_dent *dent = container_of(tmp, struct mars_dent, dent_link);
|
2011-09-27 08:46:14 +00:00
|
|
|
up_read(&global->dent_mutex);
|
2012-11-13 11:50:48 +00:00
|
|
|
|
2012-12-14 11:44:54 +00:00
|
|
|
brick_yield();
|
2012-11-13 11:50:48 +00:00
|
|
|
|
2012-11-14 09:46:36 +00:00
|
|
|
bind_to_dent(dent, &say_channel);
|
|
|
|
|
2012-01-17 10:48:56 +00:00
|
|
|
//MARS_IO("forward treat '%s'\n", dent->d_path);
|
2011-10-24 10:54:15 +00:00
|
|
|
status = worker(buf, dent, false, false);
|
2012-01-17 10:48:56 +00:00
|
|
|
if (status) {
|
|
|
|
//MARS_IO("forward treat '%s' status = %d\n", dent->d_path, status);
|
|
|
|
}
|
2011-09-27 08:46:14 +00:00
|
|
|
down_read(&global->dent_mutex);
|
2011-07-22 10:43:40 +00:00
|
|
|
total_status |= status;
|
|
|
|
}
|
2012-11-14 09:46:36 +00:00
|
|
|
bind_to_dent(NULL, &say_channel);
|
2011-07-22 10:43:40 +00:00
|
|
|
|
|
|
|
/* Backward pass.
|
|
|
|
*/
|
2012-01-17 10:48:56 +00:00
|
|
|
MARS_IO("forward pass\n");
|
2011-09-27 08:46:14 +00:00
|
|
|
for (tmp = global->dent_anchor.prev, next = tmp->prev; tmp != &global->dent_anchor; tmp = next, next = next->prev) {
|
2011-07-22 10:43:40 +00:00
|
|
|
struct mars_dent *dent = container_of(tmp, struct mars_dent, dent_link);
|
2011-09-27 08:46:14 +00:00
|
|
|
up_read(&global->dent_mutex);
|
2012-11-13 11:50:48 +00:00
|
|
|
|
2012-12-14 11:44:54 +00:00
|
|
|
brick_yield();
|
2012-11-13 11:50:48 +00:00
|
|
|
|
2012-11-14 09:46:36 +00:00
|
|
|
bind_to_dent(dent, &say_channel);
|
|
|
|
|
2012-01-17 10:48:56 +00:00
|
|
|
//MARS_IO("backward treat '%s'\n", dent->d_path);
|
2011-10-24 10:54:15 +00:00
|
|
|
status = worker(buf, dent, false, true);
|
2012-01-17 10:48:56 +00:00
|
|
|
if (status) {
|
|
|
|
//MARS_IO("backward treat '%s' status = %d\n", dent->d_path, status);
|
|
|
|
}
|
2011-09-27 08:46:14 +00:00
|
|
|
down_read(&global->dent_mutex);
|
2011-07-22 10:43:40 +00:00
|
|
|
total_status |= status;
|
|
|
|
if (status < 0) {
|
2012-07-17 15:51:40 +00:00
|
|
|
MARS_INF("backwards: status %d on '%s'\n", status, dent->d_path);
|
2011-07-22 10:43:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
up_read(&global->dent_mutex);
|
2012-11-14 09:46:36 +00:00
|
|
|
bind_to_dent(NULL, &say_channel);
|
2011-07-22 10:43:40 +00:00
|
|
|
|
|
|
|
done:
|
2011-09-27 08:46:14 +00:00
|
|
|
MARS_IO("total_status = %d\n", total_status);
|
2011-07-22 10:43:40 +00:00
|
|
|
return total_status;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(mars_dent_work);
|
|
|
|
|
|
|
|
struct mars_dent *_mars_find_dent(struct mars_global *global, const char *path)
|
|
|
|
{
|
|
|
|
struct mars_dent *res = NULL;
|
|
|
|
struct list_head *tmp;
|
|
|
|
|
|
|
|
if (!rwsem_is_locked(&global->dent_mutex)) {
|
|
|
|
MARS_ERR("dent_mutex not held!\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
for (tmp = global->dent_anchor.next; tmp != &global->dent_anchor; tmp = tmp->next) {
|
|
|
|
struct mars_dent *tmp_dent = container_of(tmp, struct mars_dent, dent_link);
|
|
|
|
if (!strcmp(tmp_dent->d_path, path)) {
|
|
|
|
res = tmp_dent;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
2011-10-24 10:54:15 +00:00
|
|
|
EXPORT_SYMBOL_GPL(_mars_find_dent);
|
2011-07-22 10:43:40 +00:00
|
|
|
|
|
|
|
struct mars_dent *mars_find_dent(struct mars_global *global, const char *path)
|
|
|
|
{
|
|
|
|
struct mars_dent *res;
|
2011-09-23 14:08:17 +00:00
|
|
|
if (!global)
|
|
|
|
return NULL;
|
|
|
|
down_read(&global->dent_mutex);
|
2011-07-22 10:43:40 +00:00
|
|
|
res = _mars_find_dent(global, path);
|
2011-09-23 14:08:17 +00:00
|
|
|
up_read(&global->dent_mutex);
|
2011-07-22 10:43:40 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(mars_find_dent);
|
|
|
|
|
2011-09-27 08:46:14 +00:00
|
|
|
int mars_find_dent_all(struct mars_global *global, char *prefix, struct mars_dent ***table)
|
|
|
|
{
|
|
|
|
int max = 1024; // provisionary
|
|
|
|
int count = 0;
|
|
|
|
struct list_head *tmp;
|
|
|
|
struct mars_dent **res = brick_zmem_alloc(max * sizeof(void*));
|
|
|
|
int prefix_len = strlen(prefix);
|
|
|
|
|
|
|
|
*table = res;
|
|
|
|
if (unlikely(!res || !global))
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
down_read(&global->dent_mutex);
|
|
|
|
for (tmp = global->dent_anchor.next; tmp != &global->dent_anchor; tmp = tmp->next) {
|
|
|
|
struct mars_dent *tmp_dent = container_of(tmp, struct mars_dent, dent_link);
|
|
|
|
int this_len;
|
|
|
|
if (!tmp_dent->d_path) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
this_len = strlen(tmp_dent->d_path);
|
|
|
|
if (this_len < prefix_len || strncmp(tmp_dent->d_path, prefix, prefix_len)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
res[count++] = tmp_dent;
|
|
|
|
if (count >= max)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
up_read(&global->dent_mutex);
|
|
|
|
|
|
|
|
done:
|
|
|
|
return count;
|
|
|
|
}
|
2011-10-04 11:45:09 +00:00
|
|
|
EXPORT_SYMBOL_GPL(mars_find_dent_all);
|
2011-09-27 08:46:14 +00:00
|
|
|
|
2011-07-22 10:43:40 +00:00
|
|
|
void mars_kill_dent(struct mars_dent *dent)
|
|
|
|
{
|
|
|
|
dent->d_killme = true;
|
2011-08-31 11:42:04 +00:00
|
|
|
mars_kill_brick_all(NULL, &dent->brick_list, true);
|
2011-07-22 10:43:40 +00:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(mars_kill_dent);
|
|
|
|
|
|
|
|
void mars_free_dent(struct mars_dent *dent)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2011-10-10 13:03:10 +00:00
|
|
|
MARS_IO("%p path='%s'\n", dent, dent->d_path);
|
2011-09-27 08:46:14 +00:00
|
|
|
|
2011-07-22 10:43:40 +00:00
|
|
|
mars_kill_dent(dent);
|
|
|
|
|
|
|
|
CHECK_HEAD_EMPTY(&dent->dent_link);
|
|
|
|
CHECK_HEAD_EMPTY(&dent->brick_list);
|
|
|
|
|
|
|
|
for (i = 0; i < MARS_ARGV_MAX; i++) {
|
2011-08-12 11:09:48 +00:00
|
|
|
brick_string_free(dent->d_argv[i]);
|
2011-07-22 10:43:40 +00:00
|
|
|
}
|
2011-08-12 11:09:48 +00:00
|
|
|
brick_string_free(dent->d_args);
|
2011-08-19 10:49:16 +00:00
|
|
|
brick_string_free(dent->d_name);
|
2011-08-25 10:16:32 +00:00
|
|
|
brick_string_free(dent->d_rest);
|
2011-08-19 10:49:16 +00:00
|
|
|
brick_string_free(dent->d_path);
|
2011-08-12 11:09:48 +00:00
|
|
|
brick_string_free(dent->old_link);
|
|
|
|
brick_string_free(dent->new_link);
|
2012-11-19 10:55:38 +00:00
|
|
|
if (dent->d_private_destruct) {
|
|
|
|
dent->d_private_destruct(dent->d_private);
|
|
|
|
}
|
2011-08-31 11:42:04 +00:00
|
|
|
brick_mem_free(dent->d_private);
|
2011-08-12 11:09:48 +00:00
|
|
|
brick_mem_free(dent);
|
2011-07-22 10:43:40 +00:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(mars_free_dent);
|
|
|
|
|
2011-09-27 08:46:14 +00:00
|
|
|
void mars_free_dent_all(struct mars_global *global, struct list_head *anchor)
|
2011-07-22 10:43:40 +00:00
|
|
|
{
|
2011-09-27 08:46:14 +00:00
|
|
|
LIST_HEAD(tmp_list);
|
|
|
|
if (global)
|
|
|
|
down_write(&global->dent_mutex);
|
|
|
|
list_replace_init(anchor, &tmp_list);
|
|
|
|
if (global)
|
|
|
|
up_write(&global->dent_mutex);
|
|
|
|
MARS_DBG("is_empty=%d\n", list_empty(&tmp_list));
|
|
|
|
while (!list_empty(&tmp_list)) {
|
2011-07-22 10:43:40 +00:00
|
|
|
struct mars_dent *dent;
|
2011-09-27 08:46:14 +00:00
|
|
|
dent = container_of(tmp_list.prev, struct mars_dent, dent_link);
|
2011-08-19 10:49:16 +00:00
|
|
|
list_del_init(&dent->dent_link);
|
2011-09-27 08:46:14 +00:00
|
|
|
MARS_IO("freeing dent %p\n", dent);
|
2011-07-22 10:43:40 +00:00
|
|
|
mars_free_dent(dent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(mars_free_dent_all);
|
|
|
|
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// low-level brick instantiation
|
|
|
|
|
|
|
|
struct mars_brick *mars_find_brick(struct mars_global *global, const void *brick_type, const char *path)
|
|
|
|
{
|
|
|
|
struct list_head *tmp;
|
|
|
|
|
|
|
|
if (!global || !path)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
down_read(&global->brick_mutex);
|
|
|
|
|
|
|
|
for (tmp = global->brick_anchor.next; tmp != &global->brick_anchor; tmp = tmp->next) {
|
|
|
|
struct mars_brick *test = container_of(tmp, struct mars_brick, global_brick_link);
|
|
|
|
if (!strcmp(test->brick_path, path)) {
|
|
|
|
up_read(&global->brick_mutex);
|
|
|
|
if (brick_type && test->type != brick_type) {
|
|
|
|
MARS_ERR("bad brick type\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return test;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
up_read(&global->brick_mutex);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(mars_find_brick);
|
|
|
|
|
|
|
|
int mars_free_brick(struct mars_brick *brick)
|
|
|
|
{
|
|
|
|
struct mars_global *global;
|
|
|
|
int i;
|
2012-02-02 15:25:43 +00:00
|
|
|
int count;
|
2011-07-22 10:43:40 +00:00
|
|
|
int status;
|
|
|
|
|
|
|
|
if (!brick) {
|
|
|
|
MARS_ERR("bad brick parameter\n");
|
|
|
|
status = -EINVAL;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!brick->power.force_off || !brick->power.led_off) {
|
2012-02-02 15:25:43 +00:00
|
|
|
MARS_WRN("brick '%s' is not freeable\n", brick->brick_path);
|
2011-07-22 10:43:40 +00:00
|
|
|
status = -ETXTBSY;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
// first check whether the brick is in use somewhere
|
2011-10-05 14:59:29 +00:00
|
|
|
for (i = 0; i < brick->type->max_outputs; i++) {
|
|
|
|
struct mars_output *output = brick->outputs[i];
|
|
|
|
if (output && output->nr_connected > 0) {
|
2012-02-02 15:25:43 +00:00
|
|
|
MARS_WRN("brick '%s' not freeable, output %i is used\n", brick->brick_path, i);
|
2011-07-22 10:43:40 +00:00
|
|
|
status = -EEXIST;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-02 15:25:43 +00:00
|
|
|
count = atomic_read(&brick->mref_object_layout.alloc_count);
|
|
|
|
if (count > 0) {
|
|
|
|
MARS_ERR("MEMLEAK: brick '%s' has %d mrefs allocated (total = %d)\n", brick->brick_path, count, atomic_read(&brick->mref_object_layout.total_alloc_count));
|
|
|
|
}
|
|
|
|
|
2011-08-25 10:16:32 +00:00
|
|
|
MARS_DBG("===> freeing brick name = '%s' path = '%s'\n", brick->brick_name, brick->brick_path);
|
2011-07-22 10:43:40 +00:00
|
|
|
|
|
|
|
global = brick->global;
|
|
|
|
if (global) {
|
|
|
|
down_write(&global->brick_mutex);
|
|
|
|
list_del_init(&brick->global_brick_link);
|
|
|
|
list_del_init(&brick->dent_brick_link);
|
|
|
|
up_write(&global->brick_mutex);
|
|
|
|
}
|
|
|
|
|
2011-10-05 14:59:29 +00:00
|
|
|
for (i = 0; i < brick->type->max_inputs; i++) {
|
2011-08-25 10:16:32 +00:00
|
|
|
struct mars_input *input = brick->inputs[i];
|
|
|
|
if (input) {
|
|
|
|
MARS_DBG("disconnecting input %i\n", i);
|
|
|
|
generic_disconnect((void*)input);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-31 11:42:04 +00:00
|
|
|
MARS_DBG("deallocate name = '%s' path = '%s'\n", SAFE_STR(brick->brick_name), SAFE_STR(brick->brick_path));
|
|
|
|
brick_string_free(brick->brick_name);
|
|
|
|
brick_string_free(brick->brick_path);
|
|
|
|
|
2011-07-22 10:43:40 +00:00
|
|
|
status = generic_brick_exit_full((void*)brick);
|
|
|
|
|
|
|
|
if (status >= 0) {
|
2011-08-12 11:09:48 +00:00
|
|
|
brick_mem_free(brick);
|
2011-07-22 10:43:40 +00:00
|
|
|
mars_trigger();
|
|
|
|
} else {
|
|
|
|
MARS_ERR("error freeing brick, status = %d\n", status);
|
|
|
|
}
|
|
|
|
|
|
|
|
done:
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(mars_free_brick);
|
|
|
|
|
2011-09-02 12:17:40 +00:00
|
|
|
struct mars_brick *mars_make_brick(struct mars_global *global, struct mars_dent *belongs, bool is_server, const void *_brick_type, const char *path, const char *_name)
|
2011-07-22 10:43:40 +00:00
|
|
|
{
|
2011-08-12 11:09:48 +00:00
|
|
|
const char *name = brick_strdup(_name);
|
2011-08-31 11:42:04 +00:00
|
|
|
const char *names[] = { name, NULL };
|
2011-07-22 10:43:40 +00:00
|
|
|
const struct generic_brick_type *brick_type = _brick_type;
|
|
|
|
const struct generic_input_type **input_types;
|
|
|
|
const struct generic_output_type **output_types;
|
|
|
|
struct mars_brick *res;
|
|
|
|
int size;
|
|
|
|
int i;
|
|
|
|
int status;
|
|
|
|
|
|
|
|
if (!name) {
|
|
|
|
MARS_ERR("cannot allocate space for name\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
size = brick_type->brick_size +
|
|
|
|
(brick_type->max_inputs + brick_type->max_outputs) * sizeof(void*);
|
|
|
|
input_types = brick_type->default_input_types;
|
|
|
|
for (i = 0; i < brick_type->max_inputs; i++) {
|
|
|
|
const struct generic_input_type *type = *input_types++;
|
|
|
|
if (unlikely(!type)) {
|
|
|
|
MARS_ERR("input_type %d is missing\n", i);
|
|
|
|
goto err_name;
|
|
|
|
}
|
|
|
|
if (unlikely(type->input_size <= 0)) {
|
|
|
|
MARS_ERR("bad input_size at %d\n", i);
|
|
|
|
goto err_name;
|
|
|
|
}
|
|
|
|
size += type->input_size;
|
|
|
|
}
|
|
|
|
output_types = brick_type->default_output_types;
|
|
|
|
for (i = 0; i < brick_type->max_outputs; i++) {
|
|
|
|
const struct generic_output_type *type = *output_types++;
|
|
|
|
if (unlikely(!type)) {
|
|
|
|
MARS_ERR("output_type %d is missing\n", i);
|
|
|
|
goto err_name;
|
|
|
|
}
|
|
|
|
if (unlikely(type->output_size <= 0)) {
|
|
|
|
MARS_ERR("bad output_size at %d\n", i);
|
|
|
|
goto err_name;
|
|
|
|
}
|
|
|
|
size += type->output_size;
|
|
|
|
}
|
|
|
|
|
2011-08-12 11:09:48 +00:00
|
|
|
res = brick_zmem_alloc(size);
|
2011-07-22 10:43:40 +00:00
|
|
|
if (!res) {
|
|
|
|
MARS_ERR("cannot grab %d bytes for brick type '%s'\n", size, brick_type->type_name);
|
|
|
|
goto err_name;
|
|
|
|
}
|
|
|
|
res->global = global;
|
|
|
|
INIT_LIST_HEAD(&res->dent_brick_link);
|
2011-08-12 11:09:48 +00:00
|
|
|
res->brick_path = brick_strdup(path);
|
2011-07-22 10:43:40 +00:00
|
|
|
if (!res->brick_path) {
|
|
|
|
MARS_ERR("cannot grab memory for path '%s'\n", path);
|
|
|
|
goto err_res;
|
|
|
|
}
|
|
|
|
|
|
|
|
status = generic_brick_init_full(res, size, brick_type, NULL, NULL, names);
|
|
|
|
MARS_DBG("brick '%s' init '%s' '%s' (status=%d)\n", brick_type->type_name, path, name, status);
|
|
|
|
if (status < 0) {
|
|
|
|
MARS_ERR("cannot init brick %s\n", brick_type->type_name);
|
|
|
|
goto err_path;
|
|
|
|
}
|
|
|
|
res->free = mars_free_brick;
|
|
|
|
|
|
|
|
/* Immediately make it visible, regardless of internal state.
|
|
|
|
* Switching on / etc must be done separately.
|
|
|
|
*/
|
|
|
|
down_write(&global->brick_mutex);
|
2011-09-02 12:17:40 +00:00
|
|
|
if (!is_server) {
|
2011-08-31 11:42:04 +00:00
|
|
|
list_add(&res->global_brick_link, &global->brick_anchor);
|
2011-09-02 12:17:40 +00:00
|
|
|
if (belongs) {
|
|
|
|
list_add_tail(&res->dent_brick_link, &belongs->brick_list);
|
|
|
|
}
|
2011-08-31 11:42:04 +00:00
|
|
|
} else {
|
2011-09-02 12:17:40 +00:00
|
|
|
list_add_tail(&res->global_brick_link, &global->server_anchor);
|
2011-07-22 10:43:40 +00:00
|
|
|
}
|
|
|
|
up_write(&global->brick_mutex);
|
|
|
|
|
|
|
|
return res;
|
|
|
|
|
|
|
|
err_path:
|
2011-08-12 11:09:48 +00:00
|
|
|
brick_string_free(res->brick_path);
|
2011-07-22 10:43:40 +00:00
|
|
|
err_res:
|
2011-08-12 11:09:48 +00:00
|
|
|
brick_mem_free(res);
|
2011-07-22 10:43:40 +00:00
|
|
|
err_name:
|
2011-08-12 11:09:48 +00:00
|
|
|
brick_string_free(name);
|
2011-07-22 10:43:40 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(mars_make_brick);
|
|
|
|
|
|
|
|
int mars_kill_brick(struct mars_brick *brick)
|
|
|
|
{
|
|
|
|
struct mars_global *global;
|
|
|
|
int status = -EINVAL;
|
|
|
|
|
|
|
|
CHECK_PTR(brick, done);
|
|
|
|
global = brick->global;
|
|
|
|
|
2012-01-30 12:47:45 +00:00
|
|
|
MARS_DBG("===> killing brick %s path = '%s' name = '%s'\n", brick->type ? SAFE_STR(brick->type->type_name) : "undef", SAFE_STR(brick->brick_path), SAFE_STR(brick->brick_name));
|
2011-07-22 10:43:40 +00:00
|
|
|
|
2013-01-05 05:11:07 +00:00
|
|
|
if (unlikely(brick->nr_outputs > 0 && brick->outputs[0] && brick->outputs[0]->nr_connected)) {
|
|
|
|
MARS_ERR("sorry, output is in use '%s'\n", SAFE_STR(brick->brick_path));
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2011-08-31 11:42:04 +00:00
|
|
|
if (global) {
|
|
|
|
down_write(&global->brick_mutex);
|
|
|
|
list_del_init(&brick->global_brick_link);
|
|
|
|
list_del_init(&brick->dent_brick_link);
|
|
|
|
up_write(&global->brick_mutex);
|
|
|
|
}
|
2011-08-25 10:16:32 +00:00
|
|
|
|
2011-09-27 11:57:04 +00:00
|
|
|
if (brick->show_status) {
|
|
|
|
brick->show_status(brick, true);
|
|
|
|
}
|
|
|
|
|
2011-07-22 10:43:40 +00:00
|
|
|
// start shutdown
|
2012-11-30 12:32:22 +00:00
|
|
|
status = set_recursive_button((void*)brick, BR_FREE_ONE, 0);
|
2011-07-22 10:43:40 +00:00
|
|
|
|
|
|
|
done:
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(mars_kill_brick);
|
|
|
|
|
2011-08-31 11:42:04 +00:00
|
|
|
int mars_kill_brick_all(struct mars_global *global, struct list_head *anchor, bool use_dent_link)
|
|
|
|
{
|
|
|
|
int status = 0;
|
2012-11-29 14:35:41 +00:00
|
|
|
if (!anchor || !anchor->next)
|
|
|
|
goto done;
|
2011-08-31 11:42:04 +00:00
|
|
|
if (global) {
|
|
|
|
down_write(&global->brick_mutex);
|
|
|
|
}
|
|
|
|
while (!list_empty(anchor)) {
|
|
|
|
struct list_head *tmp = anchor->next;
|
|
|
|
struct mars_brick *brick;
|
|
|
|
if (use_dent_link) {
|
|
|
|
brick = container_of(tmp, struct mars_brick, dent_brick_link);
|
|
|
|
} else {
|
|
|
|
brick = container_of(tmp, struct mars_brick, global_brick_link);
|
|
|
|
}
|
|
|
|
list_del_init(tmp);
|
|
|
|
if (global) {
|
|
|
|
up_write(&global->brick_mutex);
|
|
|
|
}
|
|
|
|
status |= mars_kill_brick(brick);
|
|
|
|
if (global) {
|
|
|
|
down_write(&global->brick_mutex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (global) {
|
|
|
|
up_write(&global->brick_mutex);
|
|
|
|
}
|
2012-11-29 14:35:41 +00:00
|
|
|
done:
|
2011-08-31 11:42:04 +00:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(mars_kill_brick_all);
|
|
|
|
|
2011-11-04 15:43:23 +00:00
|
|
|
int mars_kill_brick_when_possible(struct mars_global *global, struct list_head *anchor, bool use_dent_link, const struct mars_brick_type *type, bool only_off)
|
2011-10-10 11:38:55 +00:00
|
|
|
{
|
|
|
|
int return_status = 0;
|
|
|
|
struct list_head *tmp;
|
2012-02-14 13:14:58 +00:00
|
|
|
|
|
|
|
restart:
|
2011-10-10 11:38:55 +00:00
|
|
|
if (global) {
|
|
|
|
down_write(&global->brick_mutex);
|
|
|
|
}
|
2012-02-14 13:14:58 +00:00
|
|
|
for (tmp = anchor->next; tmp != anchor; tmp = tmp->next) {
|
2011-10-10 11:38:55 +00:00
|
|
|
struct mars_brick *brick;
|
|
|
|
int status;
|
|
|
|
if (use_dent_link) {
|
|
|
|
brick = container_of(tmp, struct mars_brick, dent_brick_link);
|
|
|
|
} else {
|
|
|
|
brick = container_of(tmp, struct mars_brick, global_brick_link);
|
|
|
|
}
|
2011-10-10 13:03:10 +00:00
|
|
|
// only kill the right brick types
|
2011-10-10 11:38:55 +00:00
|
|
|
if (brick->type != type) {
|
|
|
|
continue;
|
|
|
|
}
|
2011-10-10 13:03:10 +00:00
|
|
|
// only kill unconnected bricks
|
2011-11-04 15:43:23 +00:00
|
|
|
if (brick->outputs[0] && brick->outputs[0]->nr_connected > 0) {
|
2011-10-10 11:38:55 +00:00
|
|
|
continue;
|
|
|
|
}
|
2011-11-04 15:43:23 +00:00
|
|
|
if (only_off) {
|
2012-02-14 13:14:58 +00:00
|
|
|
if (brick->power.led_off) { // already off
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (global) {
|
|
|
|
up_write(&global->brick_mutex);
|
|
|
|
}
|
2011-11-04 15:43:23 +00:00
|
|
|
MARS_DBG("POWER OFF '%s'\n", brick->brick_name);
|
|
|
|
status = mars_power_button(brick, false, false);
|
|
|
|
goto success;
|
|
|
|
}
|
2011-10-10 13:03:10 +00:00
|
|
|
/* optimization:
|
|
|
|
* only kill bricks which have not been touched during the current mars_dent_work() round.
|
|
|
|
* disable this for stress-testing the allocation/deallocation logic.
|
|
|
|
*/
|
|
|
|
if (global && global->global_version == brick->brick_version) {
|
|
|
|
continue;
|
|
|
|
}
|
2012-02-14 13:14:58 +00:00
|
|
|
|
2011-10-10 11:38:55 +00:00
|
|
|
list_del_init(tmp);
|
|
|
|
if (global) {
|
|
|
|
up_write(&global->brick_mutex);
|
|
|
|
}
|
2012-02-14 13:14:58 +00:00
|
|
|
|
2011-11-04 15:43:23 +00:00
|
|
|
MARS_DBG("KILLING '%s'\n", brick->brick_name);
|
2011-10-10 11:38:55 +00:00
|
|
|
status = mars_kill_brick(brick);
|
2012-02-14 13:14:58 +00:00
|
|
|
|
2011-11-04 15:43:23 +00:00
|
|
|
success:
|
2011-10-10 11:38:55 +00:00
|
|
|
if (status >= 0) {
|
|
|
|
return_status++;
|
|
|
|
} else {
|
2012-02-14 13:14:58 +00:00
|
|
|
return status;
|
2011-10-10 11:38:55 +00:00
|
|
|
}
|
2012-02-14 13:14:58 +00:00
|
|
|
/* The list may have changed during the open lock
|
|
|
|
* in unpredictable ways.
|
|
|
|
*/
|
|
|
|
goto restart;
|
2011-10-10 11:38:55 +00:00
|
|
|
}
|
|
|
|
if (global) {
|
|
|
|
up_write(&global->brick_mutex);
|
|
|
|
}
|
|
|
|
return return_status;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(mars_kill_brick_when_possible);
|
|
|
|
|
2011-07-22 10:43:40 +00:00
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// mid-level brick instantiation (identity is based on path strings)
|
|
|
|
|
2012-11-19 10:31:55 +00:00
|
|
|
char *_vpath_make(int line, const char *fmt, va_list *args)
|
2011-07-22 10:43:40 +00:00
|
|
|
{
|
2012-11-19 10:31:55 +00:00
|
|
|
char *res = _brick_string_alloc(MARS_PATH_MAX, line);
|
2011-07-22 10:43:40 +00:00
|
|
|
|
|
|
|
if (likely(res)) {
|
|
|
|
vsnprintf(res, MARS_PATH_MAX, fmt, *args);
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
2012-11-19 10:31:55 +00:00
|
|
|
EXPORT_SYMBOL_GPL(_vpath_make);
|
2011-07-22 10:43:40 +00:00
|
|
|
|
2012-11-19 10:31:55 +00:00
|
|
|
char *_path_make(int line, const char *fmt, ...)
|
2011-07-22 10:43:40 +00:00
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
char *res;
|
|
|
|
va_start(args, fmt);
|
2012-11-19 10:31:55 +00:00
|
|
|
res = _vpath_make(line, fmt, &args);
|
2011-07-22 10:43:40 +00:00
|
|
|
va_end(args);
|
|
|
|
return res;
|
|
|
|
}
|
2012-11-19 10:31:55 +00:00
|
|
|
EXPORT_SYMBOL_GPL(_path_make);
|
2011-07-22 10:43:40 +00:00
|
|
|
|
2012-11-19 10:31:55 +00:00
|
|
|
char *_backskip_replace(int line, const char *path, char delim, bool insert, const char *fmt, ...)
|
2011-07-22 10:43:40 +00:00
|
|
|
{
|
|
|
|
int path_len = strlen(path);
|
|
|
|
int total_len = strlen(fmt) + path_len + MARS_PATH_MAX;
|
2012-11-19 10:31:55 +00:00
|
|
|
char *res = _brick_string_alloc(total_len + 1, line);
|
2011-07-22 10:43:40 +00:00
|
|
|
if (likely(res)) {
|
|
|
|
va_list args;
|
|
|
|
int pos = path_len;
|
|
|
|
int plus;
|
|
|
|
|
|
|
|
while (pos > 0 && path[pos] != '/') {
|
|
|
|
pos--;
|
|
|
|
}
|
|
|
|
if (delim != '/') {
|
|
|
|
while (pos < path_len && path[pos] != delim) {
|
|
|
|
pos++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
memcpy(res, path, pos);
|
|
|
|
|
|
|
|
va_start(args, fmt);
|
|
|
|
plus = vsnprintf(res + pos, total_len - pos, fmt, args);
|
|
|
|
va_end(args);
|
|
|
|
|
|
|
|
if (insert) {
|
|
|
|
strncpy(res + pos + plus, path + pos + 1, total_len - pos - plus);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
2012-11-19 10:31:55 +00:00
|
|
|
EXPORT_SYMBOL_GPL(_backskip_replace);
|
2011-07-22 10:43:40 +00:00
|
|
|
|
|
|
|
struct mars_brick *path_find_brick(struct mars_global *global, const void *brick_type, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
char *fullpath;
|
|
|
|
struct mars_brick *res;
|
|
|
|
|
|
|
|
va_start(args, fmt);
|
|
|
|
fullpath = vpath_make(fmt, &args);
|
|
|
|
va_end(args);
|
|
|
|
|
|
|
|
if (!fullpath) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
res = mars_find_brick(global, brick_type, fullpath);
|
2011-08-12 11:09:48 +00:00
|
|
|
brick_string_free(fullpath);
|
2011-07-22 10:43:40 +00:00
|
|
|
MARS_IO("search for '%s' found = %p\n", fullpath, res);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(path_find_brick);
|
|
|
|
|
|
|
|
const struct generic_brick_type *_client_brick_type = NULL;
|
|
|
|
EXPORT_SYMBOL_GPL(_client_brick_type);
|
|
|
|
const struct generic_brick_type *_bio_brick_type = NULL;
|
|
|
|
EXPORT_SYMBOL_GPL(_bio_brick_type);
|
|
|
|
const struct generic_brick_type *_aio_brick_type = NULL;
|
|
|
|
EXPORT_SYMBOL_GPL(_aio_brick_type);
|
2011-11-14 13:12:33 +00:00
|
|
|
const struct generic_brick_type *_sio_brick_type = NULL;
|
|
|
|
EXPORT_SYMBOL_GPL(_sio_brick_type);
|
2011-07-22 10:43:40 +00:00
|
|
|
|
|
|
|
struct mars_brick *make_brick_all(
|
|
|
|
struct mars_global *global,
|
|
|
|
struct mars_dent *belongs,
|
2011-09-02 12:17:40 +00:00
|
|
|
bool is_server,
|
2011-08-25 10:16:32 +00:00
|
|
|
int (*setup_fn)(struct mars_brick *brick, void *private),
|
2011-07-22 10:43:40 +00:00
|
|
|
void *private,
|
|
|
|
int timeout,
|
|
|
|
const char *new_name,
|
|
|
|
const struct generic_brick_type *new_brick_type,
|
|
|
|
const struct generic_brick_type *prev_brick_type[],
|
|
|
|
const char *switch_fmt,
|
2011-11-03 11:31:47 +00:00
|
|
|
int switch_override, // -1 = off, +1 = on, 0 = let switch decide
|
2011-07-22 10:43:40 +00:00
|
|
|
const char *new_fmt,
|
|
|
|
const char *prev_fmt[],
|
|
|
|
int prev_count,
|
|
|
|
...
|
|
|
|
)
|
|
|
|
{
|
|
|
|
va_list args;
|
2011-08-12 11:09:48 +00:00
|
|
|
char *switch_path = NULL;
|
2011-07-22 10:43:40 +00:00
|
|
|
const char *new_path;
|
2011-08-12 11:09:48 +00:00
|
|
|
char *_new_path = NULL;
|
2011-07-22 10:43:40 +00:00
|
|
|
struct mars_brick *brick = NULL;
|
|
|
|
char *paths[prev_count];
|
|
|
|
struct mars_brick *prev[prev_count];
|
|
|
|
int switch_state = true;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
// treat variable arguments
|
|
|
|
va_start(args, prev_count);
|
|
|
|
if (switch_fmt) {
|
|
|
|
switch_state = false;
|
2011-10-18 09:07:53 +00:00
|
|
|
if (switch_fmt[0] && global && global->global_power.button) {
|
2011-07-22 10:43:40 +00:00
|
|
|
switch_path = vpath_make(switch_fmt, &args);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (new_fmt) {
|
|
|
|
new_path = _new_path = vpath_make(new_fmt, &args);
|
|
|
|
} else {
|
|
|
|
new_path = new_name;
|
|
|
|
}
|
|
|
|
for (i = 0; i < prev_count; i++) {
|
|
|
|
paths[i] = vpath_make(prev_fmt[i], &args);
|
|
|
|
}
|
|
|
|
va_end(args);
|
|
|
|
|
|
|
|
if (!new_path) {
|
|
|
|
MARS_ERR("could not create new path\n");
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
if (switch_path) {
|
|
|
|
struct mars_dent *test = mars_find_dent(global, switch_path);
|
|
|
|
if (test && test->new_link) {
|
|
|
|
sscanf(test->new_link, "%d", &switch_state);
|
|
|
|
}
|
|
|
|
}
|
2011-11-03 11:31:47 +00:00
|
|
|
if (switch_override > 0)
|
|
|
|
switch_state = true;
|
|
|
|
else if (switch_override < 0)
|
|
|
|
switch_state = false;
|
2011-09-02 12:17:40 +00:00
|
|
|
if (global && !global->global_power.button) {
|
|
|
|
switch_state = false;
|
|
|
|
}
|
2011-07-22 10:43:40 +00:00
|
|
|
|
|
|
|
// brick already existing?
|
2011-09-02 12:17:40 +00:00
|
|
|
brick = mars_find_brick(global, NULL, new_path);
|
2011-07-22 10:43:40 +00:00
|
|
|
if (brick) {
|
|
|
|
// just switch the power state
|
|
|
|
MARS_DBG("found existing brick '%s'\n", new_path);
|
|
|
|
goto do_switch;
|
|
|
|
}
|
|
|
|
if (!switch_state) { // don't start => also don't create
|
|
|
|
MARS_DBG("no need for brick '%s'\n", new_path);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
MARS_DBG("make new brick '%s'\n", new_path);
|
|
|
|
if (!new_name)
|
|
|
|
new_name = new_path;
|
|
|
|
|
|
|
|
MARS_DBG("----> new brick type = '%s' path = '%s' name = '%s'\n", new_brick_type->type_name, new_path, new_name);
|
|
|
|
|
|
|
|
// get all predecessor bricks
|
|
|
|
for (i = 0; i < prev_count; i++) {
|
|
|
|
char *path = paths[i];
|
|
|
|
|
|
|
|
if (!path) {
|
|
|
|
MARS_ERR("could not build path %d\n", i);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2011-09-02 12:17:40 +00:00
|
|
|
prev[i] = mars_find_brick(global, NULL, path);
|
2011-07-22 10:43:40 +00:00
|
|
|
|
|
|
|
if (!prev[i]) {
|
2011-08-31 11:42:04 +00:00
|
|
|
MARS_WRN("prev brick '%s' does not exist\n", path);
|
2011-07-22 10:43:40 +00:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
MARS_DBG("------> predecessor %d path = '%s'\n", i, path);
|
2011-11-03 11:31:47 +00:00
|
|
|
if (!prev[i]->power.led_on) {
|
|
|
|
switch_state = false;
|
|
|
|
MARS_DBG("predecessor power is not on\n");
|
|
|
|
}
|
2011-07-22 10:43:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// some generic brick replacements (better performance / network functionality)
|
|
|
|
brick = NULL;
|
|
|
|
if ((new_brick_type == _bio_brick_type || new_brick_type == _aio_brick_type)
|
|
|
|
&& _client_brick_type != NULL) {
|
|
|
|
char *remote = strchr(new_name, '@');
|
|
|
|
if (remote) {
|
|
|
|
remote++;
|
|
|
|
MARS_DBG("substitute by remote brick '%s' on peer '%s'\n", new_name, remote);
|
|
|
|
|
2011-09-02 12:17:40 +00:00
|
|
|
brick = mars_make_brick(global, belongs, is_server, _client_brick_type, new_path, new_name);
|
2011-07-22 10:43:40 +00:00
|
|
|
if (brick) {
|
|
|
|
struct client_brick *_brick = (void*)brick;
|
|
|
|
_brick->max_flying = 10000;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!brick && new_brick_type == _bio_brick_type && _aio_brick_type) {
|
|
|
|
struct kstat test = {};
|
|
|
|
int status = mars_stat(new_path, &test, false);
|
|
|
|
if (SKIP_BIO || status < 0 || !S_ISBLK(test.mode)) {
|
|
|
|
new_brick_type = _aio_brick_type;
|
|
|
|
MARS_DBG("substitute bio by aio\n");
|
|
|
|
}
|
|
|
|
}
|
2011-11-14 13:12:33 +00:00
|
|
|
#ifdef CONFIG_MARS_PREFER_SIO
|
|
|
|
if (!brick && new_brick_type == _aio_brick_type && _sio_brick_type) {
|
|
|
|
new_brick_type = _sio_brick_type;
|
|
|
|
MARS_DBG("substitute aio by sio\n");
|
|
|
|
}
|
|
|
|
#endif
|
2011-07-22 10:43:40 +00:00
|
|
|
|
|
|
|
// create it...
|
|
|
|
if (!brick)
|
2011-09-02 12:17:40 +00:00
|
|
|
brick = mars_make_brick(global, belongs, is_server, new_brick_type, new_path, new_name);
|
2011-07-22 10:43:40 +00:00
|
|
|
if (unlikely(!brick)) {
|
|
|
|
MARS_ERR("creation failed '%s' '%s'\n", new_path, new_name);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
if (unlikely(brick->nr_inputs < prev_count)) {
|
2011-08-19 10:49:16 +00:00
|
|
|
MARS_ERR("'%s' wrong number of arguments: %d < %d\n", new_path, brick->nr_inputs, prev_count);
|
2011-07-22 10:43:40 +00:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
// connect the wires
|
|
|
|
for (i = 0; i < prev_count; i++) {
|
|
|
|
int status;
|
|
|
|
status = generic_connect((void*)brick->inputs[i], (void*)prev[i]->outputs[0]);
|
|
|
|
if (unlikely(status < 0)) {
|
|
|
|
MARS_ERR("'%s' '%s' cannot connect input %d\n", new_path, new_name, i);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-25 10:16:32 +00:00
|
|
|
do_switch:
|
2011-10-10 13:03:10 +00:00
|
|
|
if (global) {
|
|
|
|
brick->brick_version = global->global_version;
|
|
|
|
}
|
2011-07-22 10:43:40 +00:00
|
|
|
// call setup function
|
|
|
|
if (setup_fn) {
|
2011-08-25 10:16:32 +00:00
|
|
|
int setup_status = setup_fn(brick, private);
|
|
|
|
if (setup_status <= 0) {
|
|
|
|
switch_state = 0;
|
|
|
|
}
|
2011-07-22 10:43:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// switch on/off (may fail silently, but responsibility is at the workers)
|
|
|
|
if (timeout > 0 || !switch_state) {
|
|
|
|
int status;
|
2011-08-19 10:49:16 +00:00
|
|
|
if (switch_state) {
|
|
|
|
status = mars_power_button_recursive((void*)brick, switch_state, false, timeout);
|
|
|
|
} else {
|
|
|
|
status = mars_power_button((void*)brick, switch_state, false);
|
|
|
|
}
|
|
|
|
MARS_DBG("switch '%s' timeout=%d to %d status = %d\n", new_path, timeout, switch_state, status);
|
2011-07-22 10:43:40 +00:00
|
|
|
#if 0 // TODO: need cleanup_fn() here FIXME: interferes with logic needing the switched-off brick!
|
|
|
|
if (!switch_state && status >= 0 && !brick->power.button && brick->power.led_off) {
|
|
|
|
mars_kill_brick(brick);
|
|
|
|
brick = NULL;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
err:
|
|
|
|
if (brick) {
|
|
|
|
mars_kill_brick(brick);
|
|
|
|
}
|
|
|
|
brick = NULL;
|
|
|
|
done:
|
|
|
|
for (i = 0; i < prev_count; i++) {
|
|
|
|
if (paths[i]) {
|
2011-08-12 11:09:48 +00:00
|
|
|
brick_string_free(paths[i]);
|
2011-07-22 10:43:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (_new_path)
|
2011-08-12 11:09:48 +00:00
|
|
|
brick_string_free(_new_path);
|
2011-07-22 10:43:40 +00:00
|
|
|
if (switch_path)
|
2011-08-12 11:09:48 +00:00
|
|
|
brick_string_free(switch_path);
|
2011-07-22 10:43:40 +00:00
|
|
|
|
|
|
|
return brick;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(make_brick_all);
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// init stuff
|
|
|
|
|
2011-08-25 10:16:32 +00:00
|
|
|
int __init init_sy(void)
|
2011-07-22 10:43:40 +00:00
|
|
|
{
|
|
|
|
MARS_INF("init_sy()\n");
|
|
|
|
|
|
|
|
_mars_trigger = __mars_trigger;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-08-25 10:16:32 +00:00
|
|
|
void __exit exit_sy(void)
|
2011-07-22 10:43:40 +00:00
|
|
|
{
|
|
|
|
MARS_INF("exit_sy()\n");
|
|
|
|
}
|
|
|
|
|
2011-08-25 10:16:32 +00:00
|
|
|
#ifndef CONFIG_MARS_HAVE_BIGMODULE
|
2011-07-22 10:43:40 +00:00
|
|
|
MODULE_DESCRIPTION("MARS block storage");
|
|
|
|
MODULE_AUTHOR("Thomas Schoebel-Theuer <tst@1und1.de>");
|
|
|
|
MODULE_LICENSE("GPL");
|
|
|
|
|
|
|
|
module_init(init_sy);
|
|
|
|
module_exit(exit_sy);
|
2011-08-25 10:16:32 +00:00
|
|
|
#endif
|