mars/kernel/mars_sio.c

784 lines
17 KiB
C
Raw Permalink Normal View History

2014-11-21 10:51:34 +00:00
/*
* MARS Long Distance Replication Software
*
* This file is part of MARS project: http://schoebel.github.io/mars/
*
* Copyright (C) 2010-2014 Thomas Schoebel-Theuer
* Copyright (C) 2011-2014 1&1 Internet AG
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
2010-06-15 18:31:06 +00:00
2010-08-04 17:32:04 +00:00
//#define BRICK_DEBUGGING
2010-07-30 05:46:22 +00:00
//#define MARS_DEBUGGING
//#define IO_DEBUGGING
#define USE_VFS_READ
#define USE_VFS_WRITE
2010-07-30 05:46:22 +00:00
2010-06-15 18:31:06 +00:00
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/version.h>
2010-06-15 18:31:06 +00:00
#include <linux/string.h>
2010-06-17 16:57:10 +00:00
#include <linux/list.h>
2010-06-16 13:21:30 +00:00
#include <linux/types.h>
#include <linux/blkdev.h>
#include <linux/highmem.h>
2010-06-17 16:57:10 +00:00
#include <linux/spinlock.h>
#include <linux/wait.h>
2010-06-18 11:44:14 +00:00
#include <linux/splice.h>
2010-06-15 18:31:06 +00:00
2022-08-23 14:48:02 +00:00
#include "brick_wait.h"
2010-06-15 18:31:06 +00:00
#include "mars.h"
///////////////////////// own type definitions ////////////////////////
2011-03-07 10:27:38 +00:00
#include "mars_sio.h"
2010-06-15 18:31:06 +00:00
////////////////// own brick / input / output operations //////////////////
2011-03-07 10:27:38 +00:00
static int sio_ref_get(struct sio_output *output, struct mref_object *mref)
2010-11-26 13:45:10 +00:00
{
struct file *file;
2015-07-15 06:17:31 +00:00
if (unlikely(!output->brick->power.led_on))
return -EBADFD;
if (mref->ref_initialized) {
_mref_get(mref);
return mref->ref_len;
}
file = output->mf->mf_filp;
if (file) {
loff_t total_size = i_size_read(file->f_mapping->host);
mref->ref_total_size = total_size;
/* Only check reads.
* Writes behind EOF are always allowed (sparse files)
*/
if (!(mref->ref_flags & MREF_MAY_WRITE)) {
loff_t len = total_size - mref->ref_pos;
if (unlikely(len <= 0)) {
/* Special case: allow reads starting _exactly_ at EOF when a timeout is specified.
*/
if (len < 0 || mref->ref_timeout <= 0) {
MARS_DBG("ENODATA %lld\n", len);
return -ENODATA;
}
}
// Shorten below EOF, but allow special case
if (mref->ref_len > len && len > 0) {
mref->ref_len = len;
}
}
}
/* Buffered IO.
2010-11-26 13:45:10 +00:00
*/
if (!mref->ref_data) {
struct sio_mref_aspect *mref_a = sio_mref_get_aspect(output->brick, mref);
if (unlikely(!mref_a))
return -EILSEQ;
if (unlikely(mref->ref_len <= 0)) {
MARS_ERR("bad ref_len = %d\n", mref->ref_len);
2022-07-27 07:40:45 +00:00
return -EBADR;
}
2023-02-19 22:39:32 +00:00
mref_a->alloc_len = mref->ref_len;
mref->ref_data = brick_block_alloc(mref->ref_pos, mref->ref_len);
mref_a->do_dealloc = true;
//atomic_inc(&output->total_alloc_count);
//atomic_inc(&output->alloc_count);
}
2010-11-26 13:45:10 +00:00
_mref_get_first(mref);
return mref->ref_len;
2010-11-26 13:45:10 +00:00
}
2011-03-07 10:27:38 +00:00
static void sio_ref_put(struct sio_output *output, struct mref_object *mref)
2010-11-26 13:45:10 +00:00
{
struct file *file;
struct sio_mref_aspect *mref_a;
if (!_mref_put(mref))
2010-11-26 13:45:10 +00:00
return;
file = output->mf->mf_filp;
mref->ref_total_size = i_size_read(file->f_mapping->host);
mref_a = sio_mref_get_aspect(output->brick, mref);
if (mref_a && mref_a->do_dealloc) {
brick_block_free(mref->ref_data, mref_a->alloc_len);
//atomic_dec(&output->alloc_count);
}
2011-03-07 10:27:38 +00:00
sio_free_mref(mref);
2010-11-26 13:45:10 +00:00
}
2019-02-21 12:54:36 +00:00
#ifndef MARS_HAS_KERNEL_READ
2010-06-18 11:44:14 +00:00
// some code borrowed from the loopback driver
2010-06-17 16:57:10 +00:00
static int transfer_none(int cmd,
struct page *raw_page, unsigned raw_off,
2010-11-26 13:45:10 +00:00
void *loop_buf,
2010-06-17 16:57:10 +00:00
int size)
{
2013-10-15 15:57:52 +00:00
void *raw_buf;
2010-06-17 16:57:10 +00:00
2013-10-15 15:57:52 +00:00
if (unlikely(!raw_page || !loop_buf)) {
2010-07-30 05:46:22 +00:00
MARS_ERR("transfer NULL: %p %p\n", raw_buf, loop_buf);
return -EFAULT;
}
2010-11-26 13:45:10 +00:00
2013-10-15 15:57:52 +00:00
raw_buf = page_address(raw_page) + raw_off;
2010-06-17 16:57:10 +00:00
if (cmd == READ)
memcpy(loop_buf, raw_buf, size);
else
memcpy(raw_buf, loop_buf, size);
2010-11-26 13:45:10 +00:00
2020-06-12 07:46:58 +00:00
brick_yield();
2010-06-17 16:57:10 +00:00
return 0;
}
2019-02-21 12:54:36 +00:00
#endif
2010-06-17 16:57:10 +00:00
static
int write_aops(struct sio_output *output, struct mref_object *mref)
2010-06-17 16:57:10 +00:00
{
struct file *file = output->mf->mf_filp;
2010-11-26 13:45:10 +00:00
loff_t pos = mref->ref_pos;
void *data = mref->ref_data;
int len = mref->ref_len;
int ret = 0;
2019-02-21 12:54:36 +00:00
#ifdef MARS_HAS_KERNEL_READ
ret = kernel_write(file,
data,
len,
&pos);
#else
#ifdef USE_VFS_WRITE
mm_segment_t oldfs;
oldfs = get_fs();
set_fs(KERNEL_DS);
ret = vfs_write(file, data, len, &pos);
set_fs(oldfs);
#else
2010-11-26 13:45:10 +00:00
unsigned offset;
2010-08-20 10:58:24 +00:00
struct address_space *mapping;
2010-07-30 05:46:22 +00:00
2010-08-20 10:58:24 +00:00
if (unlikely(!file)) {
MARS_FAT("No FILE\n");
return -ENXIO;
2010-08-20 10:58:24 +00:00
}
mapping = file->f_mapping;
2010-06-17 16:57:10 +00:00
mutex_lock(&mapping->host->i_mutex);
2010-11-26 13:45:10 +00:00
offset = pos & ((pgoff_t)PAGE_CACHE_SIZE - 1);
while (len > 0) {
int transfer_result;
unsigned size, copied;
struct page *page = NULL;
void *fsdata;
size = PAGE_CACHE_SIZE - offset;
if (size > len)
size = len;
ret = pagecache_write_begin(file, mapping, pos, size, 0,
&page, &fsdata);
if (ret) {
MARS_ERR("cannot start pagecache_write_begin() error=%d\n", ret);
if (ret >= 0)
ret = -EINVAL;
2010-11-26 13:45:10 +00:00
goto fail;
}
2010-06-17 16:57:10 +00:00
2010-11-26 13:45:10 +00:00
//file_update_time(file);
2010-06-17 16:57:10 +00:00
2010-11-26 13:45:10 +00:00
transfer_result = transfer_none(WRITE, page, offset, data, size);
2010-06-17 16:57:10 +00:00
2010-11-26 13:45:10 +00:00
copied = size;
if (transfer_result) {
MARS_ERR("transfer error %d\n", transfer_result);
copied = 0;
}
2010-06-17 16:57:10 +00:00
2010-11-26 13:45:10 +00:00
ret = pagecache_write_end(file, mapping, pos, size, copied,
page, fsdata);
if (ret < 0 || ret != copied || transfer_result) {
MARS_ERR("write error %d\n", ret);
if (ret >= 0)
ret = -EINVAL;
2010-11-26 13:45:10 +00:00
goto fail;
2010-06-17 16:57:10 +00:00
}
2010-11-26 13:45:10 +00:00
len -= copied;
offset = 0;
pos += copied;
data += copied;
2010-06-17 16:57:10 +00:00
}
2010-11-26 13:45:10 +00:00
ret = 0;
2010-06-17 16:57:10 +00:00
2010-07-30 05:46:22 +00:00
fail:
2010-06-17 16:57:10 +00:00
mutex_unlock(&mapping->host->i_mutex);
2010-06-18 11:44:14 +00:00
#if 1
2010-06-17 16:57:10 +00:00
blk_run_address_space(mapping);
2010-06-18 11:44:14 +00:00
#endif
2019-02-21 12:54:36 +00:00
#endif
#endif
return ret;
2010-06-18 11:44:14 +00:00
}
struct cookie_data {
2011-03-07 10:27:38 +00:00
struct sio_output *output;
2010-12-15 12:13:18 +00:00
struct mref_object *mref;
2010-06-18 11:44:14 +00:00
};
2010-06-17 16:57:10 +00:00
2019-02-21 12:54:36 +00:00
#ifndef MARS_HAS_KERNEL_READ
2010-06-18 11:44:14 +00:00
static int
2011-03-07 10:27:38 +00:00
sio_splice_actor(struct pipe_inode_info *pipe,
2010-06-18 11:44:14 +00:00
struct pipe_buffer *buf,
struct splice_desc *sd)
{
struct cookie_data *cookie = sd->u.data;
struct mref_object *mref = cookie->mref;
2010-06-18 11:44:14 +00:00
struct page *page = buf->page;
void *data;
2010-06-18 11:44:14 +00:00
int size, ret;
ret = buf->ops->confirm(pipe, buf);
if (unlikely(ret))
return ret;
size = sd->len;
if (size > mref->ref_len)
size = mref->ref_len;
2010-06-18 11:44:14 +00:00
data = mref->ref_data;
if (transfer_none(READ, page, buf->offset, data, size)) {
2010-11-26 13:45:10 +00:00
MARS_ERR("transfer error\n");
2010-06-18 11:44:14 +00:00
size = -EINVAL;
}
2010-11-26 13:45:10 +00:00
//flush_dcache_page(p->bvec->bv_page);
2010-06-18 11:44:14 +00:00
return size;
}
static int
2011-03-07 10:27:38 +00:00
sio_direct_splice_actor(struct pipe_inode_info *pipe, struct splice_desc *sd)
2010-06-18 11:44:14 +00:00
{
2011-03-07 10:27:38 +00:00
return __splice_from_pipe(pipe, sd, sio_splice_actor);
2010-06-18 11:44:14 +00:00
}
2019-02-21 12:54:36 +00:00
#endif
2010-06-18 11:44:14 +00:00
static
int read_aops(struct sio_output *output, struct mref_object *mref)
2010-06-18 11:44:14 +00:00
{
2010-11-26 13:45:10 +00:00
loff_t pos = mref->ref_pos;
int len = mref->ref_len;
int ret;
2010-06-18 11:44:14 +00:00
2019-02-21 12:54:36 +00:00
#ifdef MARS_HAS_KERNEL_READ
ret = kernel_read(output->mf->mf_filp,
mref->ref_data,
len,
&pos);
#else
#ifdef USE_VFS_READ
mm_segment_t oldfs;
(void) sio_direct_splice_actor; // shut up gcc
oldfs = get_fs();
set_fs(KERNEL_DS);
ret = vfs_read(output->mf->mf_filp, mref->ref_data, len, &pos);
set_fs(oldfs);
#else
2010-11-26 13:45:10 +00:00
struct cookie_data cookie = {
.output = output,
.mref = mref,
};
struct splice_desc sd = {
.len = 0,
.total_len = len,
2010-11-26 13:45:10 +00:00
.flags = 0,
.pos = pos,
.u.data = &cookie,
};
ret = splice_direct_to_actor(output->mf->mf_filp, &sd, sio_direct_splice_actor);
2019-02-21 12:54:36 +00:00
#endif
#endif
2010-11-26 13:45:10 +00:00
if (unlikely(ret < 0)) {
MARS_ERR("%p %p status=%d\n", output, mref, ret);
2010-07-30 05:46:22 +00:00
}
return ret;
2010-06-17 16:57:10 +00:00
}
2011-03-07 10:27:38 +00:00
static void sync_file(struct sio_output *output)
2010-06-18 11:44:14 +00:00
{
struct file *file = output->mf->mf_filp;
2010-06-18 11:44:14 +00:00
int ret;
#if defined(S_BIAS) || (defined(RHEL_MAJOR) && (RHEL_MAJOR < 7))
2010-11-26 13:45:10 +00:00
ret = vfs_fsync(file, file->f_path.dentry, 1);
2012-09-18 06:13:33 +00:00
#else
ret = vfs_fsync(file, 1);
#endif
2010-06-18 11:44:14 +00:00
if (unlikely(ret)) {
MARS_ERR("syncing pages failed: %d\n", ret);
}
return;
}
static
void _complete(struct sio_output *output, struct mref_object *mref, int err)
{
_mref_check(mref);
mars_trace(mref, "sio_endio");
if (err < 0) {
MARS_ERR("IO error %d at pos=%lld len=%d (mref=%p ref_data=%p)\n", err, mref->ref_pos, mref->ref_len, mref, mref->ref_data);
} else {
2012-08-01 10:09:49 +00:00
mref_checksum(mref);
mref->ref_flags |= MREF_UPTODATE;
}
2016-08-08 11:47:25 +00:00
#ifdef CONFIG_MARS_DEBUG
while (mars_hang_mode & 1)
brick_msleep(100);
#endif
CHECKED_CALLBACK(mref, err, err_found);
done:
#if 0
if (mref->ref_flags & MREF_WRITE) {
atomic_dec(&output->write_count);
} else {
atomic_dec(&output->read_count);
}
#endif
sio_ref_put(output, mref);
2015-07-15 06:17:31 +00:00
atomic_dec(&output->work_count);
atomic_dec(&mars_global_io_flying);
return;
err_found:
MARS_FAT("giving up...\n");
goto done;
}
/* This is called by the threads
*/
static
2012-02-07 11:24:49 +00:00
void _sio_ref_io(struct sio_threadinfo *tinfo, struct mref_object *mref)
2010-06-15 18:31:06 +00:00
{
2012-02-07 11:24:49 +00:00
struct sio_output *output = tinfo->output;
2010-11-26 13:45:10 +00:00
bool barrier = false;
int status;
2010-08-20 10:58:24 +00:00
_mref_check(mref);
atomic_inc(&tinfo->fly_count);
if (unlikely(!output->mf || !output->mf->mf_filp)) {
status = -EINVAL;
2010-10-22 12:00:08 +00:00
goto done;
}
2010-06-17 16:57:10 +00:00
if (barrier) {
MARS_INF("got barrier request\n");
2010-06-18 11:44:14 +00:00
sync_file(output);
2010-06-17 16:57:10 +00:00
}
if (!(mref->ref_flags & MREF_WRITE)) {
status = read_aops(output, mref);
2010-06-18 11:44:14 +00:00
} else {
2022-06-05 08:04:49 +00:00
mf_dirty_append(output->mf, DIRTY_SUBMITTED,
mref->ref_pos + mref->ref_len);
status = write_aops(output, mref);
2017-12-10 09:57:53 +00:00
if (status >= 0) {
if (barrier || output->brick->o_fdsync)
sync_file(output);
2022-06-05 08:04:49 +00:00
mf_dirty_append(output->mf, DIRTY_COMPLETING,
mref->ref_pos + mref->ref_len);
2017-12-10 09:57:53 +00:00
}
2010-06-17 16:57:10 +00:00
}
2010-06-16 13:21:30 +00:00
mapfree_set(output->mf, mref->ref_pos, mref->ref_pos + mref->ref_len);
2012-02-07 11:24:49 +00:00
2010-06-16 13:21:30 +00:00
done:
2020-04-04 07:46:00 +00:00
if ((mref->ref_flags & MREF_WRITE) && status >= 0)
mf_dirty_append(output->mf,
DIRTY_FINISHED,
mref->ref_pos + mref->ref_len);
_complete(output, mref, status);
atomic_dec(&tinfo->fly_count);
2010-06-15 18:31:06 +00:00
}
/* This is called from outside
*/
static
void sio_ref_io(struct sio_output *output, struct mref_object *mref)
2010-06-17 16:57:10 +00:00
{
int index;
2010-06-18 11:44:14 +00:00
struct sio_threadinfo *tinfo;
2011-03-07 10:27:38 +00:00
struct sio_mref_aspect *mref_a;
2010-07-30 05:46:22 +00:00
unsigned long flags;
_mref_check(mref);
mref_a = sio_mref_get_aspect(output->brick, mref);
2010-08-05 15:54:48 +00:00
if (unlikely(!mref_a)) {
2010-08-04 17:32:04 +00:00
MARS_FAT("cannot get aspect\n");
SIMPLE_CALLBACK(mref, -EINVAL);
2010-08-04 17:32:04 +00:00
return;
}
2011-02-23 20:48:06 +00:00
2015-07-15 06:17:31 +00:00
if (unlikely(!output->brick->power.led_on)) {
SIMPLE_CALLBACK(mref, -EBADFD);
return;
}
atomic_inc(&mars_global_io_flying);
2015-07-15 06:17:31 +00:00
atomic_inc(&output->work_count);
_mref_get(mref);
2011-02-23 20:48:06 +00:00
mapfree_set(output->mf, mref->ref_pos, -1);
index = 0;
if (!(mref->ref_flags & MREF_WRITE)) {
traced_lock(&output->g_lock, flags);
index = output->index++;
traced_unlock(&output->g_lock, flags);
index = (index % WITH_THREAD) + 1;
}
2010-06-18 11:44:14 +00:00
tinfo = &output->tinfo[index];
MARS_IO("queueing %p on %d\n", mref, index);
2010-08-04 17:32:04 +00:00
2012-02-07 11:24:49 +00:00
atomic_inc(&tinfo->total_count);
atomic_inc(&tinfo->queue_count);
2010-07-30 05:46:22 +00:00
traced_lock(&tinfo->lock, flags);
2010-08-05 15:54:48 +00:00
list_add_tail(&mref_a->io_head, &tinfo->mref_list);
2010-07-30 05:46:22 +00:00
traced_unlock(&tinfo->lock, flags);
2010-06-17 16:57:10 +00:00
2022-12-02 21:49:15 +00:00
brick_wake_smp(&tinfo->event);
2010-06-17 16:57:10 +00:00
}
2011-03-07 10:27:38 +00:00
static int sio_thread(void *data)
2010-06-17 16:57:10 +00:00
{
2010-06-18 11:44:14 +00:00
struct sio_threadinfo *tinfo = data;
2010-06-17 16:57:10 +00:00
MARS_INF("sio thread has started.\n");
2010-06-17 16:57:10 +00:00
//set_user_nice(current, -20);
while (!brick_thread_should_stop()) {
2010-08-03 16:03:32 +00:00
struct list_head *tmp = NULL;
2010-12-15 12:13:18 +00:00
struct mref_object *mref;
2011-03-07 10:27:38 +00:00
struct sio_mref_aspect *mref_a;
2010-07-30 05:46:22 +00:00
unsigned long flags;
2010-06-17 16:57:10 +00:00
2022-12-02 21:49:15 +00:00
brick_wait_smp(
2010-08-03 16:03:32 +00:00
tinfo->event,
!list_empty(&tinfo->mref_list) || brick_thread_should_stop(),
2010-08-03 16:03:32 +00:00
HZ);
2010-06-17 16:57:10 +00:00
2010-08-03 16:03:32 +00:00
tinfo->last_jiffies = jiffies;
2010-06-17 16:57:10 +00:00
2010-07-30 05:46:22 +00:00
traced_lock(&tinfo->lock, flags);
2010-08-04 17:32:04 +00:00
2010-08-05 15:54:48 +00:00
if (!list_empty(&tinfo->mref_list)) {
tmp = tinfo->mref_list.next;
2010-08-03 16:03:32 +00:00
list_del_init(tmp);
2012-02-07 11:24:49 +00:00
atomic_dec(&tinfo->queue_count);
2010-08-03 16:03:32 +00:00
}
2010-07-30 05:46:22 +00:00
traced_unlock(&tinfo->lock, flags);
2010-06-17 16:57:10 +00:00
2010-08-03 16:03:32 +00:00
if (!tmp)
continue;
2011-03-07 10:27:38 +00:00
mref_a = container_of(tmp, struct sio_mref_aspect, io_head);
2010-08-05 15:54:48 +00:00
mref = mref_a->object;
MARS_IO("got %p %p\n", mref_a, mref);
2012-02-07 11:24:49 +00:00
_sio_ref_io(tinfo, mref);
2010-06-17 16:57:10 +00:00
}
MARS_INF("sio thread has stopped.\n");
2010-06-17 16:57:10 +00:00
return 0;
}
2010-08-03 16:03:32 +00:00
2011-03-07 10:27:38 +00:00
static int sio_get_info(struct sio_output *output, struct mars_info *info)
2010-06-28 05:53:46 +00:00
{
struct file *file = output->mf->mf_filp;
if (unlikely(!file || !file->f_mapping || !file->f_mapping->host))
return -EINVAL;
info->tf_align = 1;
info->tf_min_size = 1;
2010-07-07 14:09:16 +00:00
info->current_size = i_size_read(file->f_mapping->host);
MARS_DBG("determined file size = %lld\n", info->current_size);
2010-07-07 14:09:16 +00:00
return 0;
2010-06-28 05:53:46 +00:00
}
2012-02-07 11:24:49 +00:00
//////////////// informational / statistics ///////////////
static noinline
char *sio_statistics(struct sio_brick *brick, int verbose)
{
struct sio_output *output = brick->outputs[0];
char *res = brick_string_alloc(1024);
int queue_sum = 0;
int fly_sum = 0;
int total_sum = 0;
int i;
if (!res)
return NULL;
for (i = 1; i <= WITH_THREAD; i++) {
struct sio_threadinfo *tinfo = &output->tinfo[i];
queue_sum += atomic_read(&tinfo->queue_count);
fly_sum += atomic_read(&tinfo->fly_count);
total_sum += atomic_read(&tinfo->total_count);
}
snprintf(res, 1024,
"queued read = %d write = %d "
"flying read = %d write = %d "
"total read = %d write = %d "
"\n",
queue_sum, atomic_read(&output->tinfo[0].queue_count),
fly_sum, atomic_read(&output->tinfo[0].fly_count),
total_sum, atomic_read(&output->tinfo[0].total_count)
);
return res;
}
static noinline
void sio_reset_statistics(struct sio_brick *brick)
{
struct sio_output *output = brick->outputs[0];
int i;
for (i = 0; i <= WITH_THREAD; i++) {
struct sio_threadinfo *tinfo = &output->tinfo[i];
atomic_set(&tinfo->total_count, 0);
}
}
2010-06-22 13:21:42 +00:00
//////////////// object / aspect constructors / destructors ///////////////
static int sio_mref_aspect_init_fn(struct generic_aspect *_ini)
2010-07-23 11:55:18 +00:00
{
2011-03-07 10:27:38 +00:00
struct sio_mref_aspect *ini = (void*)_ini;
2010-08-04 17:32:04 +00:00
INIT_LIST_HEAD(&ini->io_head);
2010-07-23 11:55:18 +00:00
return 0;
}
static void sio_mref_aspect_exit_fn(struct generic_aspect *_ini)
2010-08-08 09:03:42 +00:00
{
2011-03-07 10:27:38 +00:00
struct sio_mref_aspect *ini = (void*)_ini;
2010-08-08 09:03:42 +00:00
(void)ini;
#if 1
CHECK_HEAD_EMPTY(&ini->io_head);
#endif
}
2011-03-07 10:27:38 +00:00
MARS_MAKE_STATICS(sio);
2010-07-23 11:55:18 +00:00
2010-06-22 13:21:42 +00:00
////////////////////// brick constructors / destructors ////////////////////
2010-06-15 18:31:06 +00:00
2011-03-07 10:27:38 +00:00
static int sio_brick_construct(struct sio_brick *brick)
2010-06-15 18:31:06 +00:00
{
return 0;
}
2011-03-07 10:27:38 +00:00
static int sio_switch(struct sio_brick *brick)
2010-06-15 18:31:06 +00:00
{
static int sio_nr = 0;
2011-03-07 10:27:38 +00:00
struct sio_output *output = brick->outputs[0];
const char *path = output->brick->brick_path;
int status = 0;
2010-06-16 13:21:30 +00:00
2011-02-23 20:48:06 +00:00
if (brick->power.button) {
2020-07-28 16:26:01 +00:00
int flags = O_CREAT | O_NOFOLLOW | O_RDWR | O_LARGEFILE;
int index;
if (brick->power.led_on)
goto done;
if (brick->o_direct) {
flags |= O_DIRECT;
MARS_INF("using O_DIRECT on %s\n", path);
}
mars_power_led_off((void*)brick, false);
2020-09-30 08:34:14 +00:00
output->error = 0;
output->mf = mapfree_get(path, flags, &output->error);
if (unlikely(!output->mf || IS_ERR(output->mf))) {
status = output->error;
if (!status)
status = -ENOENT;
2020-07-28 16:26:01 +00:00
output->mf = NULL;
MARS_ERR("open '%s' flags=%d status=%d\n",
path,
flags,
status);
goto done;
2010-08-20 10:58:24 +00:00
}
output->index = 0;
for (index = 0; index <= WITH_THREAD; index++) {
struct sio_threadinfo *tinfo = &output->tinfo[index];
tinfo->last_jiffies = jiffies;
tinfo->thread = brick_thread_create(sio_thread, tinfo, "mars_sio%d", sio_nr++);
if (unlikely(!tinfo->thread)) {
MARS_ERR("cannot create thread\n");
status = -ENOENT;
goto done;
}
}
2011-02-23 20:48:06 +00:00
mars_power_led_on((void*)brick, true);
}
done:
if (unlikely(status < 0) || !brick->power.button) {
int index;
2015-07-15 06:17:31 +00:00
int count;
2011-02-23 20:48:06 +00:00
mars_power_led_on((void*)brick, false);
2015-07-15 06:17:31 +00:00
for (;;) {
count = atomic_read(&output->work_count);
if (count <= 0)
break;
MARS_DBG("working on %d requests\n", count);
brick_msleep(1000);
}
for (index = 0; index <= WITH_THREAD; index++) {
struct sio_threadinfo *tinfo = &output->tinfo[index];
if (!tinfo->thread)
continue;
MARS_DBG("stopping thread %d\n", index);
brick_thread_stop(tinfo->thread);
tinfo->thread = NULL;
}
if (output->mf) {
MARS_DBG("closing file\n");
mapfree_put(output->mf);
output->mf = NULL;
}
2011-02-23 20:48:06 +00:00
mars_power_led_off((void*)brick, true);
2010-08-20 10:58:24 +00:00
}
return status;
2010-08-20 10:58:24 +00:00
}
2011-03-07 10:27:38 +00:00
static int sio_output_construct(struct sio_output *output)
2010-08-20 10:58:24 +00:00
{
int index;
2010-06-17 16:57:10 +00:00
2010-06-18 11:44:14 +00:00
spin_lock_init(&output->g_lock);
for (index = 0; index <= WITH_THREAD; index++) {
struct sio_threadinfo *tinfo = &output->tinfo[index];
tinfo->output = output;
spin_lock_init(&tinfo->lock);
init_waitqueue_head(&tinfo->event);
2010-08-05 15:54:48 +00:00
INIT_LIST_HEAD(&tinfo->mref_list);
2010-06-17 16:57:10 +00:00
}
2010-06-16 13:21:30 +00:00
return 0;
}
2011-03-07 10:27:38 +00:00
static int sio_output_destruct(struct sio_output *output)
2010-06-16 13:21:30 +00:00
{
2010-06-15 18:31:06 +00:00
return 0;
}
///////////////////////// static structs ////////////////////////
2011-03-07 10:27:38 +00:00
static struct sio_brick_ops sio_brick_ops = {
.brick_switch = sio_switch,
2012-02-07 11:24:49 +00:00
.brick_statistics = sio_statistics,
.reset_statistics = sio_reset_statistics,
2010-06-15 18:31:06 +00:00
};
2011-03-07 10:27:38 +00:00
static struct sio_output_ops sio_output_ops = {
.mref_get = sio_ref_get,
.mref_put = sio_ref_put,
.mref_io = sio_ref_io,
2011-03-07 10:27:38 +00:00
.mars_get_info = sio_get_info,
2010-06-15 18:31:06 +00:00
};
2011-03-23 17:58:02 +00:00
const struct sio_input_type sio_input_type = {
.type_name = "sio_input",
.input_size = sizeof(struct sio_input),
};
static const struct sio_input_type *sio_input_types[] = {
&sio_input_type,
};
2011-03-07 10:27:38 +00:00
const struct sio_output_type sio_output_type = {
.type_name = "sio_output",
.output_size = sizeof(struct sio_output),
.master_ops = &sio_output_ops,
.output_construct = &sio_output_construct,
.output_destruct = &sio_output_destruct,
2010-06-15 18:31:06 +00:00
};
2011-03-07 10:27:38 +00:00
static const struct sio_output_type *sio_output_types[] = {
&sio_output_type,
2010-06-15 18:31:06 +00:00
};
2011-03-07 10:27:38 +00:00
const struct sio_brick_type sio_brick_type = {
.type_name = "sio_brick",
.brick_size = sizeof(struct sio_brick),
2010-06-15 18:31:06 +00:00
.max_inputs = 0,
.max_outputs = 1,
2011-03-07 10:27:38 +00:00
.master_ops = &sio_brick_ops,
.aspect_types = sio_aspect_types,
2011-03-23 17:58:02 +00:00
.default_input_types = sio_input_types,
2011-03-07 10:27:38 +00:00
.default_output_types = sio_output_types,
.brick_construct = &sio_brick_construct,
2010-06-15 18:31:06 +00:00
};
2011-03-07 10:27:38 +00:00
EXPORT_SYMBOL_GPL(sio_brick_type);
2010-06-15 18:31:06 +00:00
////////////////// module init stuff /////////////////////////
int __init init_mars_sio(void)
2010-06-15 18:31:06 +00:00
{
2011-03-07 10:27:38 +00:00
MARS_INF("init_sio()\n");
_sio_brick_type = (void*)&sio_brick_type;
2011-03-07 10:27:38 +00:00
return sio_register_brick_type();
2010-06-15 18:31:06 +00:00
}
2014-04-23 11:16:26 +00:00
void exit_mars_sio(void)
2010-06-15 18:31:06 +00:00
{
2011-03-07 10:27:38 +00:00
MARS_INF("exit_sio()\n");
sio_unregister_brick_type();
2010-06-15 18:31:06 +00:00
}