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 <asm/uaccess.h>
36 #include <driver/swap_debugfs.h>
37 #include "swap_writer_module.h"
38 #include "event_filter.h"
41 /* ============================================================================
43 * ============================================================================
45 static char *common_buf = NULL;
46 enum { subbuf_size = 8*1024 };
47 enum { common_buf_size = subbuf_size * NR_CPUS };
49 static int init_buffer(void)
51 common_buf = vmalloc(common_buf_size);
53 return common_buf ? 0 : -ENOMEM;
56 static void exit_buffer(void)
62 static void *get_current_buf(void)
64 return common_buf + subbuf_size * get_cpu();
67 static void put_current_buf(void)
76 /* ============================================================================
78 * ============================================================================
80 static ssize_t write_raw(struct file *file, const char __user *user_buf,
81 size_t count, loff_t *ppos)
86 if (count > subbuf_size)
89 buf = get_current_buf();
90 if (copy_from_user(buf, user_buf, count)) {
95 ret = raw_msg(buf, count);
102 static const struct file_operations fops_raw = {
103 .owner = THIS_MODULE,
105 .llseek = default_llseek
112 /* ============================================================================
113 * === FOPS_AVAILABLE_FILTERS ===
114 * ============================================================================
122 static void func_for_read(struct ev_filter *f, void *data)
124 struct read_buf *rbuf = (struct read_buf *)data;
125 int len = strlen(f->name);
127 if (rbuf->end - rbuf->ptr < len + 2)
130 if (rbuf->ptr != rbuf->begin) {
135 memcpy(rbuf->ptr, f->name, len);
139 static ssize_t read_af(struct file *file, char __user *user_buf,
140 size_t count, loff_t *ppos)
143 struct read_buf rbuf = {
146 .end = buf + sizeof(buf)
149 event_filter_on_each(func_for_read, (void *)&rbuf);
154 return simple_read_from_buffer(user_buf, count, ppos,
155 rbuf.begin, rbuf.ptr - rbuf.begin);
158 static const struct file_operations fops_available_filters = {
159 .owner = THIS_MODULE,
161 .llseek = default_llseek
168 /* ============================================================================
169 * === FOPS_FILTER ===
170 * ============================================================================
172 static ssize_t read_filter(struct file *file, char __user *user_buf,
173 size_t count, loff_t *ppos)
175 const char *name = event_filter_get();
176 int len = strlen(name);
180 buf = kmalloc(len + 2, GFP_KERNEL);
181 memcpy(buf, name, len);
186 ret = simple_read_from_buffer(user_buf, count, ppos, buf, len + 2);
192 static ssize_t write_filter(struct file *file, const char __user *user_buf,
193 size_t count, loff_t *ppos)
196 char buf[len], name[len];
200 buf_size = min(count, (size_t)(len - 1));
201 if (copy_from_user(buf, user_buf, buf_size))
205 ret = sscanf(buf, "%31s", name);
209 ret = event_filter_set(name);
216 static const struct file_operations fops_filter = {
217 .owner = THIS_MODULE,
219 .write = write_filter,
220 .llseek = default_llseek
227 /* ============================================================================
229 * ============================================================================
231 static struct dentry *writer_dir = NULL;
234 * @brief Removes writer debugfs.
238 void exit_debugfs_writer(void)
241 debugfs_remove_recursive(writer_dir);
249 * @brief Initializes writer debugfs.
251 * @return 0 on success, error code on error.
253 int init_debugfs_writer(void)
256 struct dentry *swap_dir, *dentry;
262 swap_dir = get_swap_debugfs_dir();
263 if (swap_dir == NULL)
266 writer_dir = debugfs_create_dir("writer", swap_dir);
267 if (writer_dir == NULL)
270 dentry = debugfs_create_file("raw", 0600, writer_dir, NULL, &fops_raw);
274 dentry = debugfs_create_file("available_filters", 0600, writer_dir, NULL, &fops_available_filters);
278 dentry = debugfs_create_file("filter", 0600, writer_dir, NULL, &fops_filter);
285 exit_debugfs_writer();