[IMPROVE] x86: apply jumper for US probes installing
[kernel/swap-modules.git] / writer / debugfs_writer.c
1 /**
2  * writer/debugfs_writer.c
3  * @author Vyacheslav Cherkashin <v.cherkashin@samsung.com>
4  *
5  * @section LICENSE
6  *
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.
11  *
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.
16  *
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.
20  *
21  * @section COPYRIGHT
22  *
23  * Copyright (C) Samsung Electronics, 2013
24  *
25  * @section DESCRIPTION
26  *
27  * Writer debugfs implementation.
28  */
29
30
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"
39
40
41 /* ============================================================================
42  * ===                               BUFFER                                 ===
43  * ============================================================================
44  */
45 static char *common_buf = NULL;
46 enum { subbuf_size = 8*1024 };
47 enum { common_buf_size = subbuf_size * NR_CPUS };
48
49 static int init_buffer(void)
50 {
51         common_buf = vmalloc(common_buf_size);
52
53         return common_buf ? 0 : -ENOMEM;
54 }
55
56 static void exit_buffer(void)
57 {
58         vfree(common_buf);
59         common_buf = NULL;
60 }
61
62 static void *get_current_buf(void)
63 {
64         return common_buf + subbuf_size * get_cpu();
65 }
66
67 static void put_current_buf(void)
68 {
69         put_cpu();
70 }
71
72
73
74
75
76 /* ============================================================================
77  * ===                             FOPS_RAW                                 ===
78  * ============================================================================
79  */
80 static ssize_t write_raw(struct file *file, const char __user *user_buf,
81                          size_t count, loff_t *ppos)
82 {
83         int ret;
84         void *buf;
85
86         if (count > subbuf_size)
87                 return -EINVAL;
88
89         buf = get_current_buf();
90         if (copy_from_user(buf, user_buf, count)) {
91                 ret = -EFAULT;
92                 goto put_buf;
93         }
94
95         ret = raw_msg(buf, count);
96
97 put_buf:
98         put_current_buf();
99         return ret;
100 }
101
102 static const struct file_operations fops_raw = {
103         .owner = THIS_MODULE,
104         .write =        write_raw,
105         .llseek =       default_llseek
106 };
107
108
109
110
111
112 /* ============================================================================
113  * ===                        FOPS_AVAILABLE_FILTERS                        ===
114  * ============================================================================
115  */
116 struct read_buf {
117         char *begin;
118         char *ptr;
119         char *end;
120 };
121
122 static void func_for_read(struct ev_filter *f, void *data)
123 {
124         struct read_buf *rbuf = (struct read_buf *)data;
125         int len = strlen(f->name);
126
127         if (rbuf->end - rbuf->ptr < len + 2)
128                 return;
129
130         if (rbuf->ptr != rbuf->begin) {
131                 *rbuf->ptr = ' ';
132                 ++rbuf->ptr;
133         }
134
135         memcpy(rbuf->ptr, f->name, len);
136         rbuf->ptr += len;
137 }
138
139 static ssize_t read_af(struct file *file, char __user *user_buf,
140                        size_t count, loff_t *ppos)
141 {
142         char buf[512];
143         struct read_buf rbuf = {
144                 .begin = buf,
145                 .ptr = buf,
146                 .end = buf + sizeof(buf)
147         };
148
149         event_filter_on_each(func_for_read, (void *)&rbuf);
150
151         *rbuf.ptr = '\n';
152         ++rbuf.ptr;
153
154         return simple_read_from_buffer(user_buf, count, ppos,
155                                        rbuf.begin, rbuf.ptr - rbuf.begin);
156 }
157
158 static const struct file_operations fops_available_filters = {
159         .owner = THIS_MODULE,
160         .read =         read_af,
161         .llseek =       default_llseek
162 };
163
164
165
166
167
168 /* ============================================================================
169  * ===                              FOPS_FILTER                             ===
170  * ============================================================================
171  */
172 static ssize_t read_filter(struct file *file, char __user *user_buf,
173                            size_t count, loff_t *ppos)
174 {
175         const char *name = event_filter_get();
176         int len = strlen(name);
177         char *buf;
178         ssize_t ret;
179
180         buf = kmalloc(len + 2, GFP_KERNEL);
181         memcpy(buf, name, len);
182
183         buf[len] = '\0';
184         buf[len + 1] = '\n';
185
186         ret = simple_read_from_buffer(user_buf, count, ppos, buf, len + 2);
187         kfree(buf);
188
189         return ret;
190 }
191
192 static ssize_t write_filter(struct file *file, const char __user *user_buf,
193                         size_t count, loff_t *ppos)
194 {
195         enum { len = 32 };
196         char buf[len], name[len];
197         size_t buf_size;
198         ssize_t ret;
199
200         buf_size = min(count, (size_t)(len - 1));
201         if (copy_from_user(buf, user_buf, buf_size))
202                 return -EFAULT;
203
204         buf[len - 1] = '\0';
205         ret = sscanf(buf, "%31s", name);
206         if (ret != 1)
207                 return -EINVAL;
208
209         ret = event_filter_set(name);
210         if (ret)
211                 return -EINVAL;
212
213         return count;
214 }
215
216 static const struct file_operations fops_filter = {
217         .owner = THIS_MODULE,
218         .read =         read_filter,
219         .write =        write_filter,
220         .llseek =       default_llseek
221 };
222
223
224
225
226
227 /* ============================================================================
228  * ===                              INIT/EXIT                               ===
229  * ============================================================================
230  */
231 static struct dentry *writer_dir = NULL;
232
233 /**
234  * @brief Removes writer debugfs.
235  *
236  * @return Void.
237  */
238 void exit_debugfs_writer(void)
239 {
240         if (writer_dir)
241                 debugfs_remove_recursive(writer_dir);
242
243         writer_dir = NULL;
244
245         exit_buffer();
246 }
247
248 /**
249  * @brief Initializes writer debugfs.
250  *
251  * @return 0 on success, error code on error.
252  */
253 int init_debugfs_writer(void)
254 {
255         int ret;
256         struct dentry *swap_dir, *dentry;
257
258         ret = init_buffer();
259         if (ret)
260                 return ret;
261
262         swap_dir = get_swap_debugfs_dir();
263         if (swap_dir == NULL)
264                 return -ENOENT;
265
266         writer_dir = debugfs_create_dir("writer", swap_dir);
267         if (writer_dir == NULL)
268                 return -ENOMEM;
269
270         dentry = debugfs_create_file("raw", 0600, writer_dir, NULL, &fops_raw);
271         if (dentry == NULL)
272                 goto fail;
273
274         dentry = debugfs_create_file("available_filters", 0600, writer_dir, NULL, &fops_available_filters);
275         if (dentry == NULL)
276                 goto fail;
277
278         dentry = debugfs_create_file("filter", 0600, writer_dir, NULL, &fops_filter);
279         if (dentry == NULL)
280                 goto fail;
281
282         return 0;
283
284 fail:
285         exit_debugfs_writer();
286         return -ENOMEM;
287 }