2 * writer/debugfs_writer.c
3 * @author Vyacheslav Cherkashin <v.cherkashin@samsung.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 * Copyright (C) Samsung Electronics, 2013
25 * @section DESCRIPTION
27 * Writer debugfs implementation.
31 #include <linux/module.h>
32 #include <linux/debugfs.h>
33 #include <linux/vmalloc.h>
34 #include <linux/slab.h>
35 #include <linux/uaccess.h>
36 #include <master/swap_debugfs.h>
37 #include <master/swap_initializer.h>
38 #include "swap_writer_module.h"
39 #include "event_filter.h"
42 /* ============================================================================
44 * ============================================================================
46 static char *common_buf;
47 enum { subbuf_size = 8*1024 };
48 enum { common_buf_size = subbuf_size * NR_CPUS };
50 static int init_buffer(void)
52 common_buf = vmalloc(common_buf_size);
54 return common_buf ? 0 : -ENOMEM;
57 static void exit_buffer(void)
63 static void *get_current_buf(void)
65 return common_buf + subbuf_size * get_cpu();
68 static void put_current_buf(void)
77 /* ============================================================================
79 * ============================================================================
81 static ssize_t write_raw(struct file *file, const char __user *user_buf,
82 size_t count, loff_t *ppos)
87 if (count > subbuf_size)
90 buf = get_current_buf();
91 if (copy_from_user(buf, user_buf, count)) {
96 ret = raw_msg(buf, count);
103 static const struct file_operations fops_raw = {
104 .owner = THIS_MODULE,
105 .open = swap_init_simple_open,
106 .release = swap_init_simple_release,
108 .llseek = default_llseek
115 /* ============================================================================
116 * === FOPS_AVAILABLE_FILTERS ===
117 * ============================================================================
125 static void func_for_read(struct ev_filter *f, void *data)
127 struct read_buf *rbuf = (struct read_buf *)data;
128 int len = strlen(f->name);
130 if (rbuf->end - rbuf->ptr < len + 2)
133 if (rbuf->ptr != rbuf->begin) {
138 memcpy(rbuf->ptr, f->name, len);
142 static ssize_t read_af(struct file *file, char __user *user_buf,
143 size_t count, loff_t *ppos)
146 struct read_buf rbuf = {
149 .end = buf + sizeof(buf)
152 event_filter_on_each(func_for_read, (void *)&rbuf);
157 return simple_read_from_buffer(user_buf, count, ppos,
158 rbuf.begin, rbuf.ptr - rbuf.begin);
161 static const struct file_operations fops_available_filters = {
162 .owner = THIS_MODULE,
163 .open = swap_init_simple_open,
164 .release = swap_init_simple_release,
166 .llseek = default_llseek
173 /* ============================================================================
174 * === FOPS_FILTER ===
175 * ============================================================================
177 static ssize_t read_filter(struct file *file, char __user *user_buf,
178 size_t count, loff_t *ppos)
180 const char *name = event_filter_get();
181 int len = strlen(name);
185 buf = kmalloc(len + 2, GFP_KERNEL);
186 memcpy(buf, name, len);
191 ret = simple_read_from_buffer(user_buf, count, ppos, buf, len + 2);
197 static ssize_t write_filter(struct file *file, const char __user *user_buf,
198 size_t count, loff_t *ppos)
201 char buf[len], name[len];
205 buf_size = min(count, (size_t)(len - 1));
206 if (copy_from_user(buf, user_buf, buf_size))
210 ret = sscanf(buf, "%31s", name);
214 ret = event_filter_set(name);
221 static const struct file_operations fops_filter = {
222 .owner = THIS_MODULE,
223 .open = swap_init_simple_open,
224 .release = swap_init_simple_release,
226 .write = write_filter,
227 .llseek = default_llseek
234 /* ============================================================================
236 * ============================================================================
238 static struct dentry *writer_dir;
241 * @brief Removes writer debugfs.
245 void exit_debugfs_writer(void)
248 debugfs_remove_recursive(writer_dir);
256 * @brief Initializes writer debugfs.
258 * @return 0 on success, error code on error.
260 int init_debugfs_writer(void)
263 struct dentry *swap_dir, *dentry;
269 swap_dir = swap_debugfs_getdir();
270 if (swap_dir == NULL)
273 writer_dir = debugfs_create_dir("writer", swap_dir);
274 if (writer_dir == NULL)
277 dentry = debugfs_create_file("raw", 0600, writer_dir, NULL, &fops_raw);
281 dentry = debugfs_create_file("available_filters", 0600, writer_dir,
282 NULL, &fops_available_filters);
286 dentry = debugfs_create_file("filter", 0600,
287 writer_dir, NULL, &fops_filter);
294 exit_debugfs_writer();