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.
|
|
|
|
*/
|
|
|
|
|
2011-11-14 17:52:05 +00:00
|
|
|
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/string.h>
|
2022-07-21 08:16:42 +00:00
|
|
|
#include <linux/pagemap.h>
|
2011-11-14 17:52:05 +00:00
|
|
|
|
2022-08-23 14:03:52 +00:00
|
|
|
#include "brick_wait.h"
|
2011-11-14 17:52:05 +00:00
|
|
|
#include "brick_say.h"
|
2013-07-18 10:45:34 +00:00
|
|
|
#include "lamport.h"
|
2011-11-14 17:52:05 +00:00
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// messaging
|
|
|
|
|
2012-01-10 12:55:50 +00:00
|
|
|
#include <linux/fs.h>
|
2013-01-17 16:24:53 +00:00
|
|
|
#include <linux/blkdev.h>
|
|
|
|
#include <linux/file.h>
|
2012-01-09 16:12:06 +00:00
|
|
|
#include <linux/sched.h>
|
2011-11-14 17:52:05 +00:00
|
|
|
#include <linux/preempt.h>
|
|
|
|
#include <linux/hardirq.h>
|
2012-10-11 06:25:41 +00:00
|
|
|
#include <linux/smp.h>
|
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/kthread.h>
|
2012-11-13 11:50:48 +00:00
|
|
|
#include <linux/syscalls.h>
|
2011-11-14 17:52:05 +00:00
|
|
|
|
2012-01-10 12:55:50 +00:00
|
|
|
#include <asm/uaccess.h>
|
|
|
|
|
2015-07-01 05:19:21 +00:00
|
|
|
#include "compat.h"
|
|
|
|
|
2021-01-25 13:37:30 +00:00
|
|
|
#ifdef MARS_HAS_PREPATCH_V2
|
|
|
|
/* needed for mars_rename() */
|
|
|
|
#include "sy_old/strategy.h"
|
|
|
|
#endif
|
|
|
|
|
2012-01-30 16:29:59 +00:00
|
|
|
#ifndef GFP_BRICK
|
|
|
|
#define GFP_BRICK GFP_NOIO
|
|
|
|
#endif
|
|
|
|
|
2012-01-09 16:12:06 +00:00
|
|
|
#define SAY_ORDER 0
|
|
|
|
#define SAY_BUFMAX (PAGE_SIZE << SAY_ORDER)
|
2012-11-20 08:09:33 +00:00
|
|
|
#define SAY_BUF_LIMIT (SAY_BUFMAX - 1500)
|
2012-01-09 16:12:06 +00:00
|
|
|
#define MAX_FILELEN 16
|
2017-01-18 08:00:36 +00:00
|
|
|
#define MAX_IDS 16384
|
2012-01-09 16:12:06 +00:00
|
|
|
|
2013-04-08 09:44:09 +00:00
|
|
|
const char *say_class[MAX_SAY_CLASS] = {
|
2012-10-11 06:25:41 +00:00
|
|
|
[SAY_DEBUG] = "debug",
|
|
|
|
[SAY_INFO] = "info",
|
|
|
|
[SAY_WARN] = "warn",
|
|
|
|
[SAY_ERROR] = "error",
|
|
|
|
[SAY_FATAL] = "fatal",
|
|
|
|
[SAY_TOTAL] = "total",
|
|
|
|
};
|
2013-04-08 09:44:09 +00:00
|
|
|
EXPORT_SYMBOL_GPL(say_class);
|
2011-11-14 17:52:05 +00:00
|
|
|
|
2013-04-08 08:45:20 +00:00
|
|
|
int brick_say_logging = 1;
|
|
|
|
EXPORT_SYMBOL_GPL(brick_say_logging);
|
2013-01-20 22:09:09 +00:00
|
|
|
int brick_say_debug =
|
|
|
|
#ifdef CONFIG_MARS_DEBUG_DEFAULT
|
|
|
|
1;
|
|
|
|
#else
|
|
|
|
0;
|
|
|
|
#endif
|
|
|
|
EXPORT_SYMBOL_GPL(brick_say_debug);
|
2012-11-13 11:50:48 +00:00
|
|
|
int brick_say_syslog_min = 1;
|
|
|
|
EXPORT_SYMBOL_GPL(brick_say_syslog_min);
|
|
|
|
int brick_say_syslog_max = -1;
|
|
|
|
EXPORT_SYMBOL_GPL(brick_say_syslog_max);
|
2014-01-28 09:51:17 +00:00
|
|
|
int brick_say_syslog_flood_class = 3;
|
|
|
|
EXPORT_SYMBOL_GPL(brick_say_syslog_flood_class);
|
|
|
|
int brick_say_syslog_flood_limit = 20;
|
|
|
|
EXPORT_SYMBOL_GPL(brick_say_syslog_flood_limit);
|
|
|
|
int brick_say_syslog_flood_recovery = 300;
|
|
|
|
EXPORT_SYMBOL_GPL(brick_say_syslog_flood_recovery);
|
2012-11-20 08:09:33 +00:00
|
|
|
int delay_say_on_overflow =
|
|
|
|
#ifdef CONFIG_MARS_DEBUG
|
|
|
|
1;
|
|
|
|
#else
|
|
|
|
0;
|
|
|
|
#endif
|
|
|
|
EXPORT_SYMBOL_GPL(delay_say_on_overflow);
|
2012-11-13 11:50:48 +00:00
|
|
|
|
2013-01-09 13:55:13 +00:00
|
|
|
static atomic_t say_alloc_channels = ATOMIC_INIT(0);
|
|
|
|
static atomic_t say_alloc_names = ATOMIC_INIT(0);
|
|
|
|
static atomic_t say_alloc_pages = ATOMIC_INIT(0);
|
2012-11-13 11:50:48 +00:00
|
|
|
|
2014-01-28 09:51:17 +00:00
|
|
|
static unsigned long flood_start_jiffies = 0;
|
|
|
|
static int flood_count = 0;
|
|
|
|
|
2012-10-11 06:25:41 +00:00
|
|
|
struct say_channel {
|
|
|
|
char *ch_name;
|
|
|
|
struct say_channel *ch_next;
|
|
|
|
spinlock_t ch_lock[MAX_SAY_CLASS];
|
|
|
|
char *ch_buf[MAX_SAY_CLASS][2];
|
2012-11-13 11:50:48 +00:00
|
|
|
short ch_index[MAX_SAY_CLASS];
|
|
|
|
struct file *ch_filp[MAX_SAY_CLASS][2];
|
2012-10-11 06:25:41 +00:00
|
|
|
int ch_overflow[MAX_SAY_CLASS];
|
|
|
|
bool ch_written[MAX_SAY_CLASS];
|
2012-11-13 11:50:48 +00:00
|
|
|
bool ch_rollover;
|
2013-04-08 09:44:09 +00:00
|
|
|
bool ch_must_exist;
|
|
|
|
bool ch_is_dir;
|
2013-04-24 08:45:08 +00:00
|
|
|
bool ch_delete;
|
2012-11-13 11:50:48 +00:00
|
|
|
int ch_status_written;
|
2012-10-11 06:25:41 +00:00
|
|
|
int ch_id_max;
|
|
|
|
void *ch_ids[MAX_IDS];
|
2012-11-20 08:09:33 +00:00
|
|
|
wait_queue_head_t ch_progress;
|
2012-10-11 06:25:41 +00:00
|
|
|
};
|
2012-02-02 10:16:06 +00:00
|
|
|
|
2012-10-11 06:25:41 +00:00
|
|
|
struct say_channel *default_channel = NULL;
|
|
|
|
EXPORT_SYMBOL_GPL(default_channel);
|
2012-01-10 12:55:50 +00:00
|
|
|
|
2012-10-11 06:25:41 +00:00
|
|
|
static struct say_channel *channel_list = NULL;
|
|
|
|
|
2017-12-11 09:04:07 +00:00
|
|
|
static DECLARE_RWSEM(say_mutex);
|
2012-10-11 06:25:41 +00:00
|
|
|
|
|
|
|
static struct task_struct *say_thread = NULL;
|
|
|
|
|
|
|
|
static DECLARE_WAIT_QUEUE_HEAD(say_event);
|
|
|
|
|
|
|
|
bool say_dirty = false;
|
|
|
|
|
2017-12-11 08:28:55 +00:00
|
|
|
static
|
|
|
|
bool cannot_schedule(void)
|
|
|
|
{
|
|
|
|
return (preempt_count() & (SOFTIRQ_MASK | HARDIRQ_MASK | NMI_MASK)) != 0 || in_atomic() || irqs_disabled();
|
|
|
|
}
|
|
|
|
|
2012-11-20 08:09:33 +00:00
|
|
|
static
|
|
|
|
void wait_channel(struct say_channel *ch, int class)
|
|
|
|
{
|
|
|
|
if (delay_say_on_overflow && ch->ch_index[class] > SAY_BUF_LIMIT) {
|
2017-12-11 08:28:55 +00:00
|
|
|
bool use_atomic = cannot_schedule();
|
2022-08-23 14:03:52 +00:00
|
|
|
|
2012-11-20 08:09:33 +00:00
|
|
|
if (!use_atomic) {
|
|
|
|
say_dirty = true;
|
2022-08-23 14:03:52 +00:00
|
|
|
brick_wake_smp(&say_event);
|
|
|
|
brick_wait_smp(ch->ch_progress,
|
|
|
|
ch->ch_index[class] < SAY_BUF_LIMIT,
|
|
|
|
HZ / 10);
|
2012-11-20 08:09:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-11 06:25:41 +00:00
|
|
|
static
|
|
|
|
struct say_channel *find_channel(const void *id)
|
2012-02-02 10:16:06 +00:00
|
|
|
{
|
2012-10-11 06:25:41 +00:00
|
|
|
struct say_channel *res = default_channel;
|
|
|
|
struct say_channel *ch;
|
|
|
|
|
2020-03-07 18:49:18 +00:00
|
|
|
if (!default_channel || cannot_schedule())
|
2017-12-11 09:04:07 +00:00
|
|
|
return res;
|
|
|
|
|
|
|
|
down_read(&say_mutex);
|
2012-10-11 06:25:41 +00:00
|
|
|
for (ch = channel_list; ch; ch = ch->ch_next) {
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < ch->ch_id_max; i++) {
|
|
|
|
if (ch->ch_ids[i] == id) {
|
|
|
|
res = ch;
|
|
|
|
goto found;
|
|
|
|
}
|
|
|
|
}
|
2012-02-02 10:16:06 +00:00
|
|
|
}
|
2012-10-11 06:25:41 +00:00
|
|
|
found:
|
2017-12-11 09:04:07 +00:00
|
|
|
up_read(&say_mutex);
|
2012-10-11 06:25:41 +00:00
|
|
|
return res;
|
2012-02-02 10:16:06 +00:00
|
|
|
}
|
|
|
|
|
2012-11-13 11:50:48 +00:00
|
|
|
static
|
|
|
|
void _remove_binding(struct task_struct *whom)
|
|
|
|
{
|
|
|
|
struct say_channel *ch;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (ch = channel_list; ch; ch = ch->ch_next) {
|
|
|
|
for (i = 0; i < ch->ch_id_max; i++) {
|
|
|
|
if (ch->ch_ids[i] == whom) {
|
|
|
|
ch->ch_ids[i] = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-11 06:25:41 +00:00
|
|
|
void bind_to_channel(struct say_channel *ch, struct task_struct *whom)
|
2012-02-02 10:16:06 +00:00
|
|
|
{
|
2012-10-11 06:25:41 +00:00
|
|
|
int i;
|
2017-05-16 08:17:19 +00:00
|
|
|
|
2020-03-07 18:49:18 +00:00
|
|
|
if (!default_channel || !ch || cannot_schedule())
|
2017-12-11 09:04:07 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
down_write(&say_mutex);
|
2012-11-13 11:50:48 +00:00
|
|
|
_remove_binding(whom);
|
2012-10-11 06:25:41 +00:00
|
|
|
for (i = 0; i < ch->ch_id_max; i++) {
|
|
|
|
if (!ch->ch_ids[i]) {
|
|
|
|
ch->ch_ids[i] = whom;
|
|
|
|
goto done;
|
|
|
|
}
|
2012-02-02 10:16:06 +00:00
|
|
|
}
|
2012-10-11 06:25:41 +00:00
|
|
|
if (likely(ch->ch_id_max < MAX_IDS - 1)) {
|
2012-11-13 11:50:48 +00:00
|
|
|
ch->ch_ids[ch->ch_id_max++] = whom;
|
2012-10-11 06:25:41 +00:00
|
|
|
} else {
|
2012-11-13 11:50:48 +00:00
|
|
|
goto err;
|
2012-10-11 06:25:41 +00:00
|
|
|
}
|
2012-11-13 11:50:48 +00:00
|
|
|
|
2012-10-11 06:25:41 +00:00
|
|
|
done:
|
2017-12-11 09:04:07 +00:00
|
|
|
up_write(&say_mutex);
|
2012-11-13 11:50:48 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
err:
|
2017-12-11 09:04:07 +00:00
|
|
|
up_write(&say_mutex);
|
2012-11-13 11:50:48 +00:00
|
|
|
|
|
|
|
say_to(default_channel, SAY_ERROR, "ID overflow for thread '%s'\n", whom->comm);
|
2012-02-02 10:16:06 +00:00
|
|
|
}
|
2012-10-11 06:25:41 +00:00
|
|
|
EXPORT_SYMBOL_GPL(bind_to_channel);
|
2012-02-02 10:16:06 +00:00
|
|
|
|
2012-11-13 11:50:48 +00:00
|
|
|
struct say_channel *get_binding(struct task_struct *whom)
|
2011-11-14 17:52:05 +00:00
|
|
|
{
|
2012-10-11 06:25:41 +00:00
|
|
|
struct say_channel *ch;
|
|
|
|
int i;
|
2012-01-09 16:12:06 +00:00
|
|
|
|
2020-03-07 18:49:18 +00:00
|
|
|
if (!default_channel || cannot_schedule())
|
2017-12-11 09:04:07 +00:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
down_read(&say_mutex);
|
2012-10-11 06:25:41 +00:00
|
|
|
for (ch = channel_list; ch; ch = ch->ch_next) {
|
|
|
|
for (i = 0; i < ch->ch_id_max; i++) {
|
|
|
|
if (ch->ch_ids[i] == whom) {
|
2012-11-13 11:50:48 +00:00
|
|
|
goto found;
|
2012-10-11 06:25:41 +00:00
|
|
|
}
|
|
|
|
}
|
2011-11-14 17:52:05 +00:00
|
|
|
}
|
2012-11-13 11:50:48 +00:00
|
|
|
ch = NULL;
|
|
|
|
found:
|
2017-12-11 09:04:07 +00:00
|
|
|
up_read(&say_mutex);
|
2012-11-13 11:50:48 +00:00
|
|
|
return ch;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(get_binding);
|
|
|
|
|
|
|
|
void remove_binding_from(struct say_channel *ch, struct task_struct *whom)
|
|
|
|
{
|
|
|
|
bool found = false;
|
|
|
|
int i;
|
|
|
|
|
2020-03-07 18:49:18 +00:00
|
|
|
if (!default_channel || !ch || cannot_schedule())
|
2017-12-11 09:04:07 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
down_write(&say_mutex);
|
2012-11-13 11:50:48 +00:00
|
|
|
for (i = 0; i < ch->ch_id_max; i++) {
|
|
|
|
if (ch->ch_ids[i] == whom) {
|
|
|
|
ch->ch_ids[i] = NULL;
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!found) {
|
|
|
|
_remove_binding(whom);
|
|
|
|
}
|
2017-12-11 09:04:07 +00:00
|
|
|
up_write(&say_mutex);
|
2012-11-13 11:50:48 +00:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(remove_binding_from);
|
|
|
|
|
|
|
|
void remove_binding(struct task_struct *whom)
|
|
|
|
{
|
2020-03-07 18:49:18 +00:00
|
|
|
if (!default_channel || cannot_schedule())
|
2017-12-11 09:04:07 +00:00
|
|
|
return;
|
2017-05-16 08:17:19 +00:00
|
|
|
|
2017-12-11 09:04:07 +00:00
|
|
|
down_write(&say_mutex);
|
2012-11-13 11:50:48 +00:00
|
|
|
_remove_binding(whom);
|
2017-12-11 09:04:07 +00:00
|
|
|
up_write(&say_mutex);
|
2012-01-09 16:12:06 +00:00
|
|
|
}
|
2012-10-11 06:25:41 +00:00
|
|
|
EXPORT_SYMBOL_GPL(remove_binding);
|
2012-01-09 16:12:06 +00:00
|
|
|
|
2012-11-13 11:50:48 +00:00
|
|
|
void rollover_channel(struct say_channel *ch)
|
|
|
|
{
|
2020-03-07 18:49:18 +00:00
|
|
|
if (!default_channel || cannot_schedule())
|
|
|
|
return;
|
|
|
|
|
2012-11-13 11:50:48 +00:00
|
|
|
if (!ch) {
|
|
|
|
ch = find_channel(current);
|
|
|
|
}
|
|
|
|
if (likely(ch))
|
|
|
|
ch->ch_rollover = true;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(rollover_channel);
|
|
|
|
|
|
|
|
void rollover_all(void)
|
|
|
|
{
|
|
|
|
struct say_channel *ch;
|
|
|
|
|
2020-03-07 18:49:18 +00:00
|
|
|
if (!default_channel || cannot_schedule())
|
2017-12-11 09:04:07 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
down_read(&say_mutex);
|
2012-11-13 11:50:48 +00:00
|
|
|
for (ch = channel_list; ch; ch = ch->ch_next) {
|
|
|
|
ch->ch_rollover = true;
|
|
|
|
}
|
2017-12-11 09:04:07 +00:00
|
|
|
up_read(&say_mutex);
|
2012-11-13 11:50:48 +00:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(rollover_all);
|
|
|
|
|
2012-10-11 06:25:41 +00:00
|
|
|
void del_channel(struct say_channel *ch)
|
2013-04-24 08:45:08 +00:00
|
|
|
{
|
|
|
|
if (unlikely(!ch))
|
|
|
|
return;
|
|
|
|
if (unlikely(ch == default_channel)) {
|
|
|
|
say_to(default_channel, SAY_ERROR, "thread '%s' tried to delete the default channel\n", current->comm);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ch->ch_delete = true;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(del_channel);
|
|
|
|
|
|
|
|
static
|
|
|
|
void _del_channel(struct say_channel *ch)
|
2012-01-09 16:12:06 +00:00
|
|
|
{
|
2012-10-11 06:25:41 +00:00
|
|
|
struct say_channel *tmp;
|
|
|
|
struct say_channel **_tmp;
|
2012-11-13 11:50:48 +00:00
|
|
|
int i, j;
|
2012-10-11 06:25:41 +00:00
|
|
|
|
2013-04-08 07:14:23 +00:00
|
|
|
if (!ch)
|
|
|
|
return;
|
2017-12-11 09:04:07 +00:00
|
|
|
if (cannot_schedule())
|
|
|
|
return;
|
2013-04-08 07:14:23 +00:00
|
|
|
|
2017-12-11 09:04:07 +00:00
|
|
|
down_write(&say_mutex);
|
2012-10-11 06:25:41 +00:00
|
|
|
for (_tmp = &channel_list; (tmp = *_tmp) != NULL; _tmp = &tmp->ch_next) {
|
|
|
|
if (tmp == ch) {
|
|
|
|
*_tmp = tmp->ch_next;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-12-11 09:04:07 +00:00
|
|
|
up_write(&say_mutex);
|
2012-01-09 16:12:06 +00:00
|
|
|
|
2012-11-13 11:50:48 +00:00
|
|
|
for (i = 0; i < MAX_SAY_CLASS; i++) {
|
|
|
|
for (j = 0; j < 2; j++) {
|
|
|
|
if (ch->ch_filp[i][j]) {
|
|
|
|
filp_close(ch->ch_filp[i][j], NULL);
|
2013-04-24 08:45:08 +00:00
|
|
|
ch->ch_filp[i][j] = NULL;
|
2012-10-11 06:25:41 +00:00
|
|
|
}
|
2012-01-10 12:55:50 +00:00
|
|
|
}
|
2012-11-13 11:50:48 +00:00
|
|
|
for (j = 0; j < 2; j++) {
|
|
|
|
char *buf = ch->ch_buf[i][j];
|
2013-01-09 13:55:13 +00:00
|
|
|
if (buf) {
|
2012-11-13 11:50:48 +00:00
|
|
|
__free_pages(virt_to_page((unsigned long)buf), SAY_ORDER);
|
2013-01-09 13:55:13 +00:00
|
|
|
atomic_dec(&say_alloc_pages);
|
|
|
|
}
|
2012-11-13 11:50:48 +00:00
|
|
|
}
|
2012-01-11 15:16:39 +00:00
|
|
|
}
|
2013-01-09 13:55:13 +00:00
|
|
|
if (ch->ch_name) {
|
|
|
|
kfree(ch->ch_name);
|
|
|
|
atomic_dec(&say_alloc_names);
|
|
|
|
}
|
2012-11-13 11:50:48 +00:00
|
|
|
kfree(ch);
|
2013-01-09 13:55:13 +00:00
|
|
|
atomic_dec(&say_alloc_channels);
|
2012-10-11 06:25:41 +00:00
|
|
|
}
|
2012-01-11 15:16:39 +00:00
|
|
|
|
2012-10-11 06:25:41 +00:00
|
|
|
static
|
2013-04-08 09:44:09 +00:00
|
|
|
struct say_channel *_make_channel(const char *name, bool must_exist)
|
2012-10-11 06:25:41 +00:00
|
|
|
{
|
|
|
|
struct say_channel *res = NULL;
|
2012-11-13 11:50:48 +00:00
|
|
|
struct kstat kstat = {};
|
2012-10-11 06:25:41 +00:00
|
|
|
int i, j;
|
2012-11-13 11:50:48 +00:00
|
|
|
mm_segment_t oldfs;
|
2013-04-08 09:44:09 +00:00
|
|
|
bool is_dir = false;
|
2012-11-13 11:50:48 +00:00
|
|
|
int status;
|
|
|
|
|
2013-04-08 09:44:09 +00:00
|
|
|
oldfs = get_fs();
|
2021-01-29 21:35:46 +00:00
|
|
|
set_fs(KERNEL_DS);
|
2012-11-13 11:50:48 +00:00
|
|
|
status = vfs_stat((char*)name, &kstat);
|
2013-04-08 09:44:09 +00:00
|
|
|
set_fs(oldfs);
|
2012-11-13 11:50:48 +00:00
|
|
|
|
2013-04-08 09:44:09 +00:00
|
|
|
if (unlikely(status < 0)) {
|
|
|
|
if (must_exist) {
|
|
|
|
say(SAY_ERROR, "cannot create channel '%s', status = %d\n", name, status);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
is_dir = S_ISDIR(kstat.mode);
|
2012-11-13 11:50:48 +00:00
|
|
|
}
|
2013-04-08 09:44:09 +00:00
|
|
|
|
2012-12-04 17:01:17 +00:00
|
|
|
restart:
|
2017-12-11 09:08:48 +00:00
|
|
|
res = kzalloc(sizeof(struct say_channel), GFP_BRICK);
|
2012-10-11 06:25:41 +00:00
|
|
|
if (unlikely(!res)) {
|
2017-05-17 10:10:35 +00:00
|
|
|
cond_resched();
|
2012-12-04 17:01:17 +00:00
|
|
|
goto restart;
|
2012-10-11 06:25:41 +00:00
|
|
|
}
|
2013-01-09 13:55:13 +00:00
|
|
|
atomic_inc(&say_alloc_channels);
|
2013-04-08 09:44:09 +00:00
|
|
|
res->ch_must_exist = must_exist;
|
|
|
|
res->ch_is_dir = is_dir;
|
2012-11-20 08:09:33 +00:00
|
|
|
init_waitqueue_head(&res->ch_progress);
|
2012-12-04 17:01:17 +00:00
|
|
|
restart2:
|
2017-12-11 09:08:48 +00:00
|
|
|
res->ch_name = kstrdup(name, GFP_BRICK);
|
2012-10-11 06:25:41 +00:00
|
|
|
if (unlikely(!res->ch_name)) {
|
2017-05-17 10:10:35 +00:00
|
|
|
cond_resched();
|
2012-12-04 17:01:17 +00:00
|
|
|
goto restart2;
|
2012-10-11 06:25:41 +00:00
|
|
|
}
|
2013-01-09 13:55:13 +00:00
|
|
|
atomic_inc(&say_alloc_names);
|
2012-10-11 06:25:41 +00:00
|
|
|
for (i = 0; i < MAX_SAY_CLASS; i++) {
|
|
|
|
spin_lock_init(&res->ch_lock[i]);
|
|
|
|
for (j = 0; j < 2; j++) {
|
2012-12-04 17:01:17 +00:00
|
|
|
char *buf;
|
|
|
|
restart3:
|
2017-12-11 09:08:48 +00:00
|
|
|
buf = (void*)__get_free_pages(GFP_BRICK, SAY_ORDER);
|
2012-10-11 06:25:41 +00:00
|
|
|
if (unlikely(!buf)) {
|
2017-05-17 10:10:35 +00:00
|
|
|
cond_resched();
|
2012-12-04 17:01:17 +00:00
|
|
|
goto restart3;
|
2012-01-11 15:16:39 +00:00
|
|
|
}
|
2013-01-09 13:55:13 +00:00
|
|
|
atomic_inc(&say_alloc_pages);
|
2012-10-11 06:25:41 +00:00
|
|
|
res->ch_buf[i][j] = buf;
|
2012-01-11 15:16:39 +00:00
|
|
|
}
|
|
|
|
}
|
2012-10-11 06:25:41 +00:00
|
|
|
done:
|
|
|
|
return res;
|
2011-11-14 17:52:05 +00:00
|
|
|
}
|
2012-01-09 16:12:06 +00:00
|
|
|
|
2013-04-08 09:44:09 +00:00
|
|
|
struct say_channel *make_channel(const char *name, bool must_exist)
|
2012-01-09 16:12:06 +00:00
|
|
|
{
|
2012-10-11 06:25:41 +00:00
|
|
|
struct say_channel *res = NULL;
|
|
|
|
struct say_channel *ch;
|
|
|
|
|
2020-03-07 18:49:18 +00:00
|
|
|
if (must_exist && !default_channel)
|
|
|
|
return NULL;
|
|
|
|
|
2017-12-11 09:04:07 +00:00
|
|
|
if (cannot_schedule()) {
|
|
|
|
printk(KERN_ERR "trying to make channel in atomic\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
down_read(&say_mutex);
|
2012-10-11 06:25:41 +00:00
|
|
|
for (ch = channel_list; ch; ch = ch->ch_next) {
|
|
|
|
if (!strcmp(ch->ch_name, name)) {
|
|
|
|
res = ch;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-12-11 09:04:07 +00:00
|
|
|
up_read(&say_mutex);
|
2012-10-11 06:25:41 +00:00
|
|
|
|
|
|
|
if (unlikely(!res)) {
|
2013-04-08 09:44:09 +00:00
|
|
|
res = _make_channel(name, must_exist);
|
2012-10-11 06:25:41 +00:00
|
|
|
if (unlikely(!res))
|
|
|
|
goto done;
|
|
|
|
|
2017-12-11 09:04:07 +00:00
|
|
|
down_write(&say_mutex);
|
2012-10-11 06:25:41 +00:00
|
|
|
|
|
|
|
for (ch = channel_list; ch; ch = ch->ch_next) {
|
2012-11-13 11:50:48 +00:00
|
|
|
if (ch != res && unlikely(!strcmp(ch->ch_name, name))) {
|
2018-05-09 05:51:21 +00:00
|
|
|
up_write(&say_mutex);
|
2013-04-24 08:45:08 +00:00
|
|
|
_del_channel(res);
|
2012-10-11 06:25:41 +00:00
|
|
|
res = ch;
|
2018-05-09 05:51:21 +00:00
|
|
|
goto done;
|
2012-10-11 06:25:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
res->ch_next = channel_list;
|
|
|
|
channel_list = res;
|
|
|
|
|
2017-12-11 09:04:07 +00:00
|
|
|
up_write(&say_mutex);
|
2012-10-11 06:25:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
done:
|
|
|
|
return res;
|
2012-01-09 16:12:06 +00:00
|
|
|
}
|
2012-10-11 06:25:41 +00:00
|
|
|
EXPORT_SYMBOL_GPL(make_channel);
|
2011-11-14 17:52:05 +00:00
|
|
|
|
2012-10-11 06:25:41 +00:00
|
|
|
// tell gcc to check for varargs errors
|
2012-01-12 13:37:06 +00:00
|
|
|
static
|
2012-10-11 06:25:41 +00:00
|
|
|
void _say(struct say_channel *ch, int class, va_list args, bool use_args, const char *fmt, ...) __attribute__ ((format (printf, 5, 6)));
|
|
|
|
|
2012-02-02 10:16:06 +00:00
|
|
|
static
|
2012-10-11 06:25:41 +00:00
|
|
|
void _say(struct say_channel *ch, int class, va_list args, bool use_args, const char *fmt, ...)
|
2011-11-14 17:52:05 +00:00
|
|
|
{
|
2012-10-11 06:25:41 +00:00
|
|
|
char *start;
|
2012-11-20 08:09:33 +00:00
|
|
|
int offset;
|
2012-01-12 10:07:37 +00:00
|
|
|
int rest;
|
2012-10-11 06:25:41 +00:00
|
|
|
int written;
|
2011-11-14 17:52:05 +00:00
|
|
|
|
2020-03-07 18:49:18 +00:00
|
|
|
if (!default_channel)
|
|
|
|
return;
|
|
|
|
|
2013-04-24 08:45:08 +00:00
|
|
|
if (unlikely(!ch))
|
|
|
|
return;
|
|
|
|
if (unlikely(ch->ch_delete && ch != default_channel)) {
|
|
|
|
say_to(default_channel, SAY_ERROR, "thread '%s' tried to write on deleted channel\n", current->comm);
|
2012-10-11 06:25:41 +00:00
|
|
|
return;
|
2013-04-24 08:45:08 +00:00
|
|
|
}
|
2012-01-09 16:12:06 +00:00
|
|
|
|
2012-11-20 08:09:33 +00:00
|
|
|
offset = ch->ch_index[class];
|
|
|
|
start = ch->ch_buf[class][0] + offset;
|
|
|
|
rest = SAY_BUFMAX - 1 - offset;
|
2012-10-11 06:25:41 +00:00
|
|
|
if (unlikely(rest <= 0)) {
|
|
|
|
ch->ch_overflow[class]++;
|
|
|
|
return;
|
|
|
|
}
|
2012-01-12 10:07:37 +00:00
|
|
|
|
2012-01-12 13:37:06 +00:00
|
|
|
if (use_args) {
|
2012-10-11 06:25:41 +00:00
|
|
|
va_list args2;
|
|
|
|
va_start(args2, fmt);
|
2014-02-18 06:56:17 +00:00
|
|
|
written = vscnprintf(start, rest, fmt, args2);
|
2012-10-11 06:25:41 +00:00
|
|
|
va_end(args2);
|
2012-01-12 13:37:06 +00:00
|
|
|
} else {
|
2014-02-18 06:56:17 +00:00
|
|
|
written = vscnprintf(start, rest, fmt, args);
|
2012-01-12 13:37:06 +00:00
|
|
|
}
|
2012-01-09 16:12:06 +00:00
|
|
|
|
2012-01-12 10:07:37 +00:00
|
|
|
if (likely(rest > written)) {
|
|
|
|
start[written] = '\0';
|
2012-11-13 11:50:48 +00:00
|
|
|
ch->ch_index[class] += written;
|
2012-10-11 06:25:41 +00:00
|
|
|
say_dirty = true;
|
2012-01-12 13:37:06 +00:00
|
|
|
} else {
|
|
|
|
// indicate overflow
|
2012-01-30 16:29:59 +00:00
|
|
|
start[0] = '\0';
|
2012-10-11 06:25:41 +00:00
|
|
|
ch->ch_overflow[class]++;
|
2012-01-12 13:37:06 +00:00
|
|
|
}
|
2012-10-11 06:25:41 +00:00
|
|
|
}
|
2012-02-02 10:16:06 +00:00
|
|
|
|
2012-10-11 06:25:41 +00:00
|
|
|
void say_to(struct say_channel *ch, int class, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
unsigned long flags;
|
2012-02-02 10:16:06 +00:00
|
|
|
|
2020-03-07 18:49:18 +00:00
|
|
|
if (!default_channel)
|
|
|
|
return;
|
|
|
|
|
2013-01-20 22:09:09 +00:00
|
|
|
if (!class && !brick_say_debug)
|
|
|
|
return;
|
|
|
|
|
2012-10-11 06:25:41 +00:00
|
|
|
if (!ch) {
|
|
|
|
ch = find_channel(current);
|
|
|
|
}
|
2012-02-02 10:16:06 +00:00
|
|
|
|
2017-12-11 09:17:48 +00:00
|
|
|
if (ch && ch != default_channel) {
|
2013-04-08 09:44:09 +00:00
|
|
|
if (!ch->ch_is_dir)
|
|
|
|
class = SAY_TOTAL;
|
2012-10-11 06:25:41 +00:00
|
|
|
if (likely(class >= 0 && class < MAX_SAY_CLASS)) {
|
2012-11-20 08:09:33 +00:00
|
|
|
wait_channel(ch, class);
|
2012-10-11 06:25:41 +00:00
|
|
|
spin_lock_irqsave(&ch->ch_lock[class], flags);
|
2012-02-02 10:16:06 +00:00
|
|
|
|
2012-10-11 06:25:41 +00:00
|
|
|
va_start(args, fmt);
|
|
|
|
_say(ch, class, args, false, fmt);
|
|
|
|
va_end(args);
|
2012-02-02 10:16:06 +00:00
|
|
|
|
2012-10-11 06:25:41 +00:00
|
|
|
spin_unlock_irqrestore(&ch->ch_lock[class], flags);
|
2012-02-02 10:16:06 +00:00
|
|
|
}
|
2012-11-13 11:50:48 +00:00
|
|
|
}
|
2012-01-12 13:37:06 +00:00
|
|
|
|
2012-11-13 11:50:48 +00:00
|
|
|
ch = default_channel;
|
|
|
|
if (likely(ch)) {
|
2012-10-11 06:25:41 +00:00
|
|
|
class = SAY_TOTAL;
|
2012-11-20 08:09:33 +00:00
|
|
|
wait_channel(ch, class);
|
2012-10-11 06:25:41 +00:00
|
|
|
spin_lock_irqsave(&ch->ch_lock[class], flags);
|
|
|
|
|
|
|
|
va_start(args, fmt);
|
|
|
|
_say(ch, class, args, false, fmt);
|
|
|
|
va_end(args);
|
2012-01-12 13:37:06 +00:00
|
|
|
|
2012-10-11 06:25:41 +00:00
|
|
|
spin_unlock_irqrestore(&ch->ch_lock[class], flags);
|
2012-01-09 16:12:06 +00:00
|
|
|
|
2022-08-23 14:03:52 +00:00
|
|
|
brick_wake_smp(&say_event);
|
2012-10-11 06:25:41 +00:00
|
|
|
}
|
2011-11-14 17:52:05 +00:00
|
|
|
}
|
2012-10-11 06:25:41 +00:00
|
|
|
EXPORT_SYMBOL_GPL(say_to);
|
2012-01-09 16:12:06 +00:00
|
|
|
|
2012-10-11 06:25:41 +00:00
|
|
|
void brick_say_to(struct say_channel *ch, int class, bool dump, const char *prefix, const char *file, int line, const char *func, const char *fmt, ...)
|
2012-01-09 16:12:06 +00:00
|
|
|
{
|
2012-11-13 11:50:48 +00:00
|
|
|
const char *channel_name = "-";
|
2019-02-19 09:18:29 +00:00
|
|
|
struct lamport_time s_now;
|
|
|
|
struct lamport_time l_now;
|
2012-01-09 16:12:06 +00:00
|
|
|
int filelen;
|
2013-04-08 09:44:09 +00:00
|
|
|
int orig_class;
|
2012-01-09 16:12:06 +00:00
|
|
|
va_list args;
|
2012-10-11 06:25:41 +00:00
|
|
|
unsigned long flags;
|
2012-01-09 16:12:06 +00:00
|
|
|
|
2020-03-07 18:49:18 +00:00
|
|
|
if (!default_channel)
|
|
|
|
return;
|
|
|
|
|
2013-01-20 22:09:09 +00:00
|
|
|
if (!class && !brick_say_debug)
|
|
|
|
return;
|
|
|
|
|
2017-04-15 06:21:21 +00:00
|
|
|
get_lamport(&s_now, &l_now);
|
2013-07-18 10:45:34 +00:00
|
|
|
|
2012-10-11 06:25:41 +00:00
|
|
|
if (!ch) {
|
|
|
|
ch = find_channel(current);
|
|
|
|
}
|
2012-01-09 16:12:06 +00:00
|
|
|
|
2013-04-08 09:44:09 +00:00
|
|
|
orig_class = class;
|
|
|
|
|
2012-01-12 10:07:37 +00:00
|
|
|
// limit the filename
|
2012-01-09 16:12:06 +00:00
|
|
|
filelen = strlen(file);
|
|
|
|
if (filelen > MAX_FILELEN)
|
|
|
|
file += filelen - MAX_FILELEN;
|
2012-10-11 06:25:41 +00:00
|
|
|
|
2012-11-13 11:50:48 +00:00
|
|
|
if (likely(ch)) {
|
|
|
|
channel_name = ch->ch_name;
|
2013-04-08 09:44:09 +00:00
|
|
|
if (!ch->ch_is_dir)
|
|
|
|
class = SAY_TOTAL;
|
2012-10-11 06:25:41 +00:00
|
|
|
if (likely(class >= 0 && class < MAX_SAY_CLASS)) {
|
2012-11-20 08:09:33 +00:00
|
|
|
wait_channel(ch, class);
|
2012-10-11 06:25:41 +00:00
|
|
|
spin_lock_irqsave(&ch->ch_lock[class], flags);
|
|
|
|
|
2012-11-13 11:50:48 +00:00
|
|
|
_say(ch, class, NULL, true,
|
2021-01-27 11:42:10 +00:00
|
|
|
"%lld.%09ld %lld.%09ld %s %s[%d] %s:%d %s(): ",
|
|
|
|
(s64)s_now.tv_sec, s_now.tv_nsec,
|
|
|
|
(s64)l_now.tv_sec, l_now.tv_nsec,
|
2012-11-13 11:50:48 +00:00
|
|
|
prefix,
|
|
|
|
current->comm, (int)smp_processor_id(),
|
|
|
|
file, line,
|
|
|
|
func);
|
|
|
|
|
2012-10-11 06:25:41 +00:00
|
|
|
va_start(args, fmt);
|
|
|
|
_say(ch, class, args, false, fmt);
|
|
|
|
va_end(args);
|
|
|
|
|
|
|
|
spin_unlock_irqrestore(&ch->ch_lock[class], flags);
|
|
|
|
}
|
2012-11-13 11:50:48 +00:00
|
|
|
}
|
2012-10-11 06:25:41 +00:00
|
|
|
|
2012-11-13 11:50:48 +00:00
|
|
|
ch = default_channel;
|
|
|
|
if (likely(ch)) {
|
2012-11-20 08:09:33 +00:00
|
|
|
wait_channel(ch, SAY_TOTAL);
|
2012-10-11 06:25:41 +00:00
|
|
|
spin_lock_irqsave(&ch->ch_lock[SAY_TOTAL], flags);
|
|
|
|
|
2012-11-13 11:50:48 +00:00
|
|
|
_say(ch, SAY_TOTAL, NULL, true,
|
2021-01-27 11:42:10 +00:00
|
|
|
"%lld.%09ld %lld.%09ld %s_%-5s %s %s[%d] %s:%d %s(): ",
|
|
|
|
(s64)s_now.tv_sec, s_now.tv_nsec,
|
|
|
|
(s64)l_now.tv_sec, l_now.tv_nsec,
|
2013-04-08 09:44:09 +00:00
|
|
|
prefix, say_class[orig_class],
|
2012-11-13 11:50:48 +00:00
|
|
|
channel_name,
|
|
|
|
current->comm, (int)smp_processor_id(),
|
|
|
|
file, line,
|
|
|
|
func);
|
|
|
|
|
2012-10-11 06:25:41 +00:00
|
|
|
va_start(args, fmt);
|
|
|
|
_say(ch, SAY_TOTAL, args, false, fmt);
|
|
|
|
va_end(args);
|
2012-01-09 16:12:06 +00:00
|
|
|
|
2012-10-11 06:25:41 +00:00
|
|
|
spin_unlock_irqrestore(&ch->ch_lock[SAY_TOTAL], flags);
|
2012-01-09 16:12:06 +00:00
|
|
|
|
2012-10-11 06:25:41 +00:00
|
|
|
}
|
2012-02-08 11:44:42 +00:00
|
|
|
#ifdef CONFIG_MARS_DEBUG
|
2012-01-11 15:16:39 +00:00
|
|
|
if (dump)
|
|
|
|
brick_dump_stack();
|
|
|
|
#endif
|
2022-08-23 14:03:52 +00:00
|
|
|
brick_wake_smp(&say_event);
|
2012-01-09 16:12:06 +00:00
|
|
|
}
|
2012-10-11 06:25:41 +00:00
|
|
|
EXPORT_SYMBOL_GPL(brick_say_to);
|
2012-01-09 16:12:06 +00:00
|
|
|
|
2012-10-11 06:25:41 +00:00
|
|
|
static
|
|
|
|
void try_open_file(struct file **file, char *filename, bool creat)
|
|
|
|
{
|
2013-01-17 16:24:53 +00:00
|
|
|
struct address_space *mapping;
|
2012-10-11 06:25:41 +00:00
|
|
|
int flags = O_APPEND | O_WRONLY | O_LARGEFILE;
|
2012-01-10 12:55:50 +00:00
|
|
|
int prot = 0600;
|
2012-01-11 15:16:39 +00:00
|
|
|
|
2012-10-11 06:25:41 +00:00
|
|
|
if (creat)
|
2013-04-08 09:44:09 +00:00
|
|
|
flags |= O_CREAT;
|
2012-01-11 15:16:39 +00:00
|
|
|
|
2012-10-11 06:25:41 +00:00
|
|
|
*file = filp_open(filename, flags, prot);
|
|
|
|
if (unlikely(IS_ERR(*file))) {
|
|
|
|
*file = NULL;
|
2013-01-17 16:24:53 +00:00
|
|
|
} else if ((mapping = (*file)->f_mapping)) {
|
|
|
|
mapping_set_gfp_mask(mapping, mapping_gfp_mask(mapping) & ~(__GFP_IO | __GFP_FS));
|
2012-01-11 15:16:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-11 06:25:41 +00:00
|
|
|
static
|
|
|
|
void out_to_file(struct file *file, char *buf, int len)
|
2012-01-11 15:16:39 +00:00
|
|
|
{
|
2012-10-11 06:25:41 +00:00
|
|
|
loff_t log_pos = 0;
|
2012-01-11 15:16:39 +00:00
|
|
|
|
2012-10-11 06:25:41 +00:00
|
|
|
if (file) {
|
2019-02-21 12:54:36 +00:00
|
|
|
#ifdef MARS_HAS_KERNEL_READ
|
|
|
|
(void)kernel_write(file,
|
|
|
|
buf,
|
|
|
|
len,
|
|
|
|
&log_pos);
|
|
|
|
#else
|
|
|
|
mm_segment_t oldfs = get_fs();
|
2021-01-29 21:35:46 +00:00
|
|
|
|
|
|
|
set_fs(KERNEL_DS);
|
2012-10-11 06:25:41 +00:00
|
|
|
(void)vfs_write(file, buf, len, &log_pos);
|
2012-01-11 15:16:39 +00:00
|
|
|
set_fs(oldfs);
|
2019-02-21 12:54:36 +00:00
|
|
|
#endif
|
2012-11-13 11:50:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-28 09:51:17 +00:00
|
|
|
static inline
|
|
|
|
void reset_flood(void)
|
|
|
|
{
|
|
|
|
if (flood_start_jiffies &&
|
|
|
|
(long)jiffies >= (long)(flood_start_jiffies + brick_say_syslog_flood_recovery * HZ)) {
|
|
|
|
flood_start_jiffies = 0;
|
|
|
|
flood_count = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-13 11:50:48 +00:00
|
|
|
static
|
|
|
|
void out_to_syslog(int class, char *buf, int len)
|
|
|
|
{
|
2014-01-28 09:51:17 +00:00
|
|
|
reset_flood();
|
2012-11-13 11:50:48 +00:00
|
|
|
if (class >= brick_say_syslog_min && class <= brick_say_syslog_max) {
|
2014-01-28 09:51:17 +00:00
|
|
|
buf[len] = '\0';
|
2012-10-11 06:25:41 +00:00
|
|
|
printk("%s", buf);
|
2014-01-28 09:51:17 +00:00
|
|
|
} else if (class >= brick_say_syslog_flood_class && brick_say_syslog_flood_class >= 0 && class != SAY_TOTAL) {
|
|
|
|
flood_start_jiffies = jiffies;
|
|
|
|
if (++flood_count <= brick_say_syslog_flood_limit) {
|
|
|
|
buf[len] = '\0';
|
|
|
|
printk("%s", buf);
|
|
|
|
}
|
2012-01-11 15:16:39 +00:00
|
|
|
}
|
2012-10-11 06:25:41 +00:00
|
|
|
}
|
|
|
|
|
2012-11-13 11:50:48 +00:00
|
|
|
static inline
|
|
|
|
char *_make_filename(struct say_channel *ch, int class, int transact, int add_tmp)
|
|
|
|
{
|
2012-12-04 17:01:17 +00:00
|
|
|
char *filename;
|
|
|
|
|
|
|
|
restart:
|
|
|
|
filename = kmalloc(1024, GFP_KERNEL);
|
|
|
|
if (unlikely(!filename)) {
|
2017-05-17 10:10:35 +00:00
|
|
|
cond_resched();
|
2012-12-04 17:01:17 +00:00
|
|
|
goto restart;
|
2012-11-13 11:50:48 +00:00
|
|
|
}
|
2013-01-09 13:55:13 +00:00
|
|
|
atomic_inc(&say_alloc_names);
|
2013-04-08 09:44:09 +00:00
|
|
|
if (ch->ch_is_dir) {
|
|
|
|
snprintf(filename, 1023, "%s/%d.%s.%s%s", ch->ch_name, class, say_class[class], transact ? "status" : "log", add_tmp ? ".tmp" : "");
|
|
|
|
} else {
|
|
|
|
snprintf(filename, 1023, "%s.%s%s", ch->ch_name, transact ? "status" : "log", add_tmp ? ".tmp" : "");
|
|
|
|
}
|
2012-11-13 11:50:48 +00:00
|
|
|
return filename;
|
|
|
|
}
|
|
|
|
|
2012-10-11 06:25:41 +00:00
|
|
|
static
|
2012-11-13 11:50:48 +00:00
|
|
|
void _rollover_channel(struct say_channel *ch)
|
|
|
|
{
|
2013-04-08 09:44:09 +00:00
|
|
|
int start = 0;
|
2012-11-13 11:50:48 +00:00
|
|
|
int class;
|
|
|
|
|
|
|
|
ch->ch_rollover = false;
|
|
|
|
ch->ch_status_written = 0;
|
2013-04-08 09:44:09 +00:00
|
|
|
|
|
|
|
if (!ch->ch_is_dir)
|
|
|
|
start = SAY_TOTAL;
|
|
|
|
|
|
|
|
for (class = start; class < MAX_SAY_CLASS; class++) {
|
2012-11-13 11:50:48 +00:00
|
|
|
char *old = _make_filename(ch, class, 1, 1);
|
|
|
|
char *new = _make_filename(ch, class, 1, 0);
|
|
|
|
|
|
|
|
if (likely(old && new)) {
|
|
|
|
int i;
|
|
|
|
mm_segment_t oldfs;
|
|
|
|
|
|
|
|
for (i = 0; i < 2; i++) {
|
|
|
|
if (ch->ch_filp[class][i]) {
|
|
|
|
filp_close(ch->ch_filp[class][i], NULL);
|
|
|
|
ch->ch_filp[class][i] = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
oldfs = get_fs();
|
2021-01-29 21:35:46 +00:00
|
|
|
set_fs(KERNEL_DS);
|
2021-01-27 10:49:50 +00:00
|
|
|
#ifdef MARS_HAS_PREPATCH_V2
|
|
|
|
mars_rename(old, new);
|
|
|
|
#elif defined(MARS_HAS_PREPATCH)
|
2012-11-13 11:50:48 +00:00
|
|
|
sys_rename(old, new);
|
2015-05-27 08:01:20 +00:00
|
|
|
#else
|
|
|
|
_compat_rename(old, new);
|
|
|
|
#endif
|
2012-11-13 11:50:48 +00:00
|
|
|
set_fs(oldfs);
|
|
|
|
}
|
|
|
|
|
2013-01-09 13:55:13 +00:00
|
|
|
if (likely(old)) {
|
2012-11-13 11:50:48 +00:00
|
|
|
kfree(old);
|
2013-01-09 13:55:13 +00:00
|
|
|
atomic_dec(&say_alloc_names);
|
|
|
|
}
|
|
|
|
if (likely(new)) {
|
2012-11-13 11:50:48 +00:00
|
|
|
kfree(new);
|
2013-01-09 13:55:13 +00:00
|
|
|
atomic_dec(&say_alloc_names);
|
|
|
|
}
|
2012-11-13 11:50:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
void treat_channel(struct say_channel *ch, int class)
|
2012-10-11 06:25:41 +00:00
|
|
|
{
|
|
|
|
int len;
|
|
|
|
int overflow;
|
2012-11-13 11:50:48 +00:00
|
|
|
int transact;
|
2013-04-08 08:45:20 +00:00
|
|
|
int start;
|
2012-10-11 06:25:41 +00:00
|
|
|
char *buf;
|
|
|
|
char *tmp;
|
|
|
|
unsigned long flags;
|
|
|
|
|
|
|
|
spin_lock_irqsave(&ch->ch_lock[class], flags);
|
|
|
|
|
2012-11-13 11:50:48 +00:00
|
|
|
buf = ch->ch_buf[class][0];
|
|
|
|
tmp = ch->ch_buf[class][1];
|
|
|
|
ch->ch_buf[class][1] = buf;
|
|
|
|
ch->ch_buf[class][0] = tmp;
|
|
|
|
len = ch->ch_index[class];
|
|
|
|
ch->ch_index[class] = 0;
|
2012-10-11 06:25:41 +00:00
|
|
|
overflow = ch->ch_overflow[class];
|
|
|
|
ch->ch_overflow[class] = 0;
|
|
|
|
|
|
|
|
spin_unlock_irqrestore(&ch->ch_lock[class], flags);
|
|
|
|
|
2022-08-23 14:03:52 +00:00
|
|
|
brick_wake_smp(&ch->ch_progress);
|
2012-11-20 08:09:33 +00:00
|
|
|
|
2012-11-13 11:50:48 +00:00
|
|
|
ch->ch_status_written += len;
|
|
|
|
out_to_syslog(class, buf, len);
|
2013-04-08 08:45:20 +00:00
|
|
|
start = 0;
|
|
|
|
if (!brick_say_logging)
|
|
|
|
start++;
|
|
|
|
for (transact = start; transact < 2; transact++) {
|
2012-11-20 08:09:33 +00:00
|
|
|
if (unlikely(!ch->ch_filp[class][transact])) {
|
|
|
|
char *filename = _make_filename(ch, class, transact, transact);
|
|
|
|
if (likely(filename)) {
|
|
|
|
try_open_file(&ch->ch_filp[class][transact], filename, transact);
|
|
|
|
kfree(filename);
|
2013-01-09 13:55:13 +00:00
|
|
|
atomic_dec(&say_alloc_names);
|
2012-11-20 08:09:33 +00:00
|
|
|
}
|
|
|
|
}
|
2012-11-13 11:50:48 +00:00
|
|
|
out_to_file(ch->ch_filp[class][transact], buf, len);
|
|
|
|
}
|
2012-10-11 06:25:41 +00:00
|
|
|
|
|
|
|
if (unlikely(overflow > 0)) {
|
2019-02-19 09:18:29 +00:00
|
|
|
struct lamport_time s_now;
|
|
|
|
struct lamport_time l_now;
|
2017-04-15 06:21:21 +00:00
|
|
|
|
|
|
|
get_lamport(&s_now, &l_now);
|
2014-02-18 06:56:17 +00:00
|
|
|
len = scnprintf(buf,
|
2013-07-18 10:45:34 +00:00
|
|
|
SAY_BUFMAX,
|
2021-01-27 11:42:10 +00:00
|
|
|
"%lld.%09ld %lld.%09ld %s %d OVERFLOW %d times\n",
|
|
|
|
(s64)s_now.tv_sec, s_now.tv_nsec,
|
|
|
|
(s64)l_now.tv_sec, l_now.tv_nsec,
|
2013-07-18 10:45:34 +00:00
|
|
|
ch->ch_name,
|
|
|
|
class,
|
|
|
|
overflow);
|
2012-11-13 11:50:48 +00:00
|
|
|
ch->ch_status_written += len;
|
|
|
|
out_to_syslog(class, buf, len);
|
|
|
|
for (transact = 0; transact < 2; transact++) {
|
|
|
|
out_to_file(ch->ch_filp[class][transact], buf, len);
|
|
|
|
}
|
2012-10-11 06:25:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
int _say_thread(void *data)
|
|
|
|
{
|
|
|
|
while (!kthread_should_stop()) {
|
|
|
|
struct say_channel *ch;
|
|
|
|
int i;
|
|
|
|
|
2022-08-23 14:03:52 +00:00
|
|
|
brick_wait_smp(say_event, say_dirty, HZ);
|
2012-10-11 06:25:41 +00:00
|
|
|
say_dirty = false;
|
2012-11-13 11:50:48 +00:00
|
|
|
|
|
|
|
restart_rollover:
|
2017-12-11 09:04:07 +00:00
|
|
|
down_read(&say_mutex);
|
2012-11-13 11:50:48 +00:00
|
|
|
for (ch = channel_list; ch; ch = ch->ch_next) {
|
|
|
|
if (ch->ch_rollover && ch->ch_status_written > 0) {
|
2017-12-11 09:04:07 +00:00
|
|
|
up_read(&say_mutex);
|
2012-11-13 11:50:48 +00:00
|
|
|
_rollover_channel(ch);
|
|
|
|
goto restart_rollover;
|
|
|
|
}
|
|
|
|
}
|
2017-12-11 09:04:07 +00:00
|
|
|
up_read(&say_mutex);
|
2012-10-11 06:25:41 +00:00
|
|
|
|
|
|
|
restart:
|
2017-12-11 09:04:07 +00:00
|
|
|
down_read(&say_mutex);
|
2012-11-13 11:50:48 +00:00
|
|
|
for (ch = channel_list; ch; ch = ch->ch_next) {
|
2013-04-08 09:44:09 +00:00
|
|
|
int start = 0;
|
|
|
|
if (!ch->ch_is_dir)
|
|
|
|
start = SAY_TOTAL;
|
|
|
|
for (i = start; i < MAX_SAY_CLASS; i++) {
|
2012-11-13 11:50:48 +00:00
|
|
|
if (ch->ch_index[i] > 0) {
|
2017-12-11 09:04:07 +00:00
|
|
|
up_read(&say_mutex);
|
2012-11-13 11:50:48 +00:00
|
|
|
treat_channel(ch, i);
|
|
|
|
goto restart;
|
2012-10-11 06:25:41 +00:00
|
|
|
}
|
|
|
|
}
|
2013-04-24 08:45:08 +00:00
|
|
|
if (ch->ch_delete) {
|
2017-12-11 09:04:07 +00:00
|
|
|
up_read(&say_mutex);
|
2013-04-24 08:45:08 +00:00
|
|
|
_del_channel(ch);
|
|
|
|
goto restart;
|
|
|
|
}
|
2012-10-11 06:25:41 +00:00
|
|
|
}
|
2017-12-11 09:04:07 +00:00
|
|
|
up_read(&say_mutex);
|
2012-01-10 12:55:50 +00:00
|
|
|
}
|
2012-10-11 06:25:41 +00:00
|
|
|
|
|
|
|
return 0;
|
2012-01-11 15:16:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void init_say(void)
|
|
|
|
{
|
2020-03-07 18:49:18 +00:00
|
|
|
/* Only initialize once */
|
|
|
|
if (default_channel)
|
|
|
|
return;
|
|
|
|
|
|
|
|
default_channel = make_channel(CONFIG_MARS_LOGDIR, false);
|
|
|
|
if (!default_channel)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!say_thread)
|
|
|
|
say_thread = kthread_create(_say_thread, NULL, "brick_say");
|
2012-10-11 06:25:41 +00:00
|
|
|
if (IS_ERR(say_thread)) {
|
|
|
|
say_thread = NULL;
|
|
|
|
} else {
|
|
|
|
get_task_struct(say_thread);
|
|
|
|
wake_up_process(say_thread);
|
2012-02-02 10:16:06 +00:00
|
|
|
}
|
2012-10-11 06:25:41 +00:00
|
|
|
|
2012-01-10 12:55:50 +00:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(init_say);
|
|
|
|
|
|
|
|
void exit_say(void)
|
2012-01-09 16:12:06 +00:00
|
|
|
{
|
2013-01-09 13:55:13 +00:00
|
|
|
int memleak_channels;
|
|
|
|
int memleak_names;
|
|
|
|
int memleak_pages;
|
|
|
|
|
2012-10-11 06:25:41 +00:00
|
|
|
if (say_thread) {
|
|
|
|
kthread_stop(say_thread);
|
|
|
|
put_task_struct(say_thread);
|
|
|
|
say_thread = NULL;
|
2012-01-09 16:12:06 +00:00
|
|
|
}
|
2012-10-11 06:25:41 +00:00
|
|
|
|
|
|
|
default_channel = NULL;
|
|
|
|
while (channel_list) {
|
2013-04-24 08:45:08 +00:00
|
|
|
_del_channel(channel_list);
|
2012-02-02 10:16:06 +00:00
|
|
|
}
|
2013-01-09 13:55:13 +00:00
|
|
|
|
|
|
|
memleak_channels = atomic_read(&say_alloc_channels);
|
|
|
|
memleak_names = atomic_read(&say_alloc_names);
|
|
|
|
memleak_pages = atomic_read(&say_alloc_pages);
|
|
|
|
if (unlikely(memleak_channels || memleak_names || memleak_pages)) {
|
|
|
|
printk("MEMLEAK: channels=%d names=%d pages=%d\n", memleak_channels, memleak_names, memleak_pages);
|
|
|
|
}
|
2012-01-09 16:12:06 +00:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(exit_say);
|
|
|
|
|
2012-02-08 11:44:42 +00:00
|
|
|
#ifdef CONFIG_MARS_DEBUG
|
2011-11-14 17:52:05 +00:00
|
|
|
|
2012-10-11 06:25:41 +00:00
|
|
|
static int dump_max = 5;
|
|
|
|
|
2011-11-14 17:52:05 +00:00
|
|
|
void brick_dump_stack(void)
|
|
|
|
{
|
|
|
|
if (dump_max > 0) {
|
|
|
|
dump_max--; // racy, but does no harm
|
|
|
|
dump_stack();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(brick_dump_stack);
|
|
|
|
|
|
|
|
#endif
|