[FIX] correct handling of do_munmap()
[kernel/swap-modules.git] / driver / driver_to_buffer.c
1 /*
2  *  SWAP driver
3  *  modules/driver/driver_to_buffer.c
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  *
19  * Copyright (C) Samsung Electronics, 2013
20  *
21  * 2013  Alexander Aksenov <a.aksenov@samsung.com>: SWAP device driver implement
22  *
23  */
24
25 #include <linux/string.h>
26 #include <linux/slab.h>
27 #include <linux/splice.h>
28 #include <asm/uaccess.h>
29 #include <linux/spinlock.h>
30
31 #include <buffer/swap_buffer_module.h>
32 #include <buffer/swap_buffer_errors.h>
33 #include <buffer/buffer_description.h>
34 #include <writer/swap_writer_module.h>
35
36 #include "driver_defs.h"
37 #include "swap_driver_errors.h"
38 #include "device_driver_to_driver_to_buffer.h"
39
40 /* Current busy buffer */
41 static struct swap_subbuffer *busy_buffer = NULL;
42
43 /* Buffers count ready to be read */
44 static int buffers_to_read = 0;
45
46 /* Pages count in one subbuffer */
47 static int pages_per_buffer = 0;
48
49 /* Used to sync changes of the buffers_to_read var */
50 static spinlock_t buf_to_read;
51
52
53 static inline void init_buffers_to_read(void)
54 {
55         spin_lock_init(&buf_to_read);
56         buffers_to_read = 0;
57 }
58
59 static inline void inc_buffers_to_read(void)
60 {
61         unsigned long flags;
62
63         spin_lock_irqsave(&buf_to_read, flags);
64         buffers_to_read++;
65         spin_unlock_irqrestore(&buf_to_read, flags);
66 }
67
68 static inline void dec_buffers_to_read(void)
69 {
70         unsigned long flags;
71
72         spin_lock_irqsave(&buf_to_read, flags);
73         buffers_to_read--;
74         spin_unlock_irqrestore(&buf_to_read, flags);
75 }
76
77 static inline void set_buffers_to_read(int count)
78 {
79         unsigned long flags;
80
81         spin_lock_irqsave(&buf_to_read, flags);
82         buffers_to_read = count;
83         spin_unlock_irqrestore(&buf_to_read, flags);
84 }
85
86 static inline int something_to_read(void)
87 {
88         unsigned long flags;
89         int result;
90
91         spin_lock_irqsave(&buf_to_read, flags);
92         result = buffers_to_read;
93         spin_unlock_irqrestore(&buf_to_read, flags);
94
95         return result;
96 }
97
98 /* TODO Get subbuffer for reading */
99 static size_t driver_to_buffer_get(void)
100 {
101         int result;
102
103         /* If there is no readable buffers, return error */
104         result = swap_buffer_get(&busy_buffer);
105         if (result == -E_SB_NO_READABLE_BUFFERS) {
106                 busy_buffer = NULL;
107                 return -E_SD_NO_DATA_TO_READ;
108         } else if (result < 0) {
109                 print_err("swap_buffer_get unhandle error %d\n", result);
110                 return -E_SD_BUFFER_ERROR;
111         }
112
113         return busy_buffer->full_buffer_part;
114 }
115
116 /* TODO Release subbuffer */
117 static int driver_to_buffer_release(void)
118 {
119         int result;
120
121         if (!busy_buffer)
122                 return -E_SD_NO_BUSY_SUBBUFFER;
123
124         result = swap_buffer_release(&busy_buffer);
125         if (result == -E_SB_NO_SUBBUFFER_IN_BUSY) {
126                 return -E_SD_WRONG_SUBBUFFER_PTR;
127         } else if (result < 0) {
128                 print_err("swap_buffer_release unhandle error %d\n", result);
129                 return -E_SD_BUFFER_ERROR;
130         }
131
132         busy_buffer = NULL;
133
134         return E_SD_SUCCESS;
135 }
136
137 /* Buffers callback function */
138 int driver_to_buffer_callback(void)
139 {
140         int result;
141
142         /* Increment buffers_to_read counter */
143         inc_buffers_to_read();
144         swap_device_wake_up_process();
145
146         return E_SD_SUCCESS;
147 }
148
149 /* Read buffers */
150 ssize_t driver_to_buffer_read(char __user *buf, size_t count)
151 {
152         size_t bytes_to_copy;
153         size_t bytes_to_read = 0;
154         int page_counter = 0;
155
156         /* Reading from swap_device means reading only current busy_buffer. So, if
157          * there is no busy_buffer, we don't get next to read, we just read nothing.
158          * In this case, or if there is nothing to read from busy_buffer - return
159          * -E_SD_NO_DATA_TO_READ. It should be correctly handled in device_driver */
160         if (!busy_buffer || !busy_buffer->full_buffer_part)
161                 return -E_SD_NO_DATA_TO_READ;
162
163         /* Bytes count that we're going to copy to user buffer is equal to user
164          * buffer size or to subbuffer readable size whichever is less */
165         bytes_to_copy = (count > busy_buffer->full_buffer_part) ?
166                     busy_buffer->full_buffer_part : count;
167
168         /* Copy data from each page to buffer */
169         while(bytes_to_copy > 0) {
170                 /* Get size that should be copied from current page */
171                 size_t read_from_this_page = (bytes_to_copy > PAGE_SIZE) ? PAGE_SIZE
172                                                                  : bytes_to_copy;
173
174                 /* Copy and add size to copied bytes count */
175
176                 // TODO Check with more than one page
177                 bytes_to_read += read_from_this_page -
178                          copy_to_user(buf, page_address(busy_buffer->data_buffer) +
179                                                         (sizeof(struct page*) *
180                                                          page_counter),
181                                                         read_from_this_page);
182                 bytes_to_copy -= read_from_this_page;
183                 page_counter++;
184         }
185
186         return bytes_to_read;
187 }
188
189 /* Flush swap_buffer */
190 int driver_to_buffer_flush(void)
191 {
192         int result;
193
194         result = swap_buffer_flush();
195
196         if (result >= 0)
197                 set_buffers_to_read(result);
198         else if (result < 0)
199                 return -E_SD_BUFFER_ERROR;
200
201         swap_device_wake_up_process();
202
203         return E_SD_SUCCESS;
204 }
205
206 /* Fills spd structure */
207 int driver_to_buffer_fill_spd(struct splice_pipe_desc *spd)
208 {
209         size_t data_to_splice = busy_buffer->full_buffer_part;
210         struct page **pages = spd->pages;
211         struct partial_page *partial = spd->partial;
212
213         while (data_to_splice) {
214                 size_t read_from_current_page = min(data_to_splice, PAGE_SIZE);
215
216                 pages[spd->nr_pages] = alloc_page(GFP_KERNEL);
217                 if (!pages[spd->nr_pages]) {
218                         print_err("Cannot alloc page for splice\n");
219                         return -ENOMEM;
220                 }
221
222                 /* FIXME: maybe there is more efficient way */
223                 memcpy(page_address(pages[spd->nr_pages]),
224                page_address(&busy_buffer->data_buffer[spd->nr_pages]),
225                read_from_current_page);
226
227                 /* Always beginning of the page */
228                 partial[spd->nr_pages].offset = 0;
229                 partial[spd->nr_pages].len = read_from_current_page;
230
231                 /* Private is not used */
232                 partial[spd->nr_pages].private = 0;
233
234                 spd->nr_pages++;
235                 data_to_splice -= read_from_current_page;
236
237                 /* TODO: add check for pipe->buffers exceeding */
238                 /* if (spd->nr_pages == pipe->buffers) { */
239                 /*      break; */
240                 /* } */
241         }
242         return 0;
243 }
244
245 /* Check for subbuffers ready to be read */
246 int driver_to_buffer_buffer_to_read(void)
247 {
248         return busy_buffer ? 1 : 0;
249 }
250
251 /* Set buffers size and count */
252 int driver_to_buffer_initialize(size_t size, unsigned int count)
253 {
254         int result;
255
256         if (size == 0 && count == 0) {
257                 return -E_SD_WRONG_ARGS;
258         }
259
260         result = swap_buffer_init(size, count, (void*)&driver_to_buffer_callback);
261         if (result == -E_SB_NO_MEM_QUEUE_BUSY
262                 || result == -E_SB_NO_MEM_BUFFER_STRUCT) {
263                 return -E_SD_NO_MEMORY;
264         }
265
266         // TODO Race condition: buffer can be used in other thread till we're in
267         // this func
268         /* Initialize driver_to_buffer variables */
269         pages_per_buffer = result;
270         busy_buffer = NULL;
271         init_buffers_to_read();
272
273         return E_SD_SUCCESS;
274 }
275
276 /* Uninitialize buffer */
277 int driver_to_buffer_uninitialize(void)
278 {
279         int result;
280
281         /* Release occupied buffer */
282         if (busy_buffer) {
283                 result = driver_to_buffer_release();
284                 // TODO Maybe release anyway
285                 if (result < 0) {
286                         return result;
287                 }
288                 busy_buffer = NULL;
289         }
290
291         result = swap_buffer_uninit();
292         if (result == -E_SB_UNRELEASED_BUFFERS) {
293                 print_err("Can't uninit buffer! There are busy subbuffers!\n");
294                 result = -E_SD_BUFFER_ERROR;
295         } else if (result < 0) {
296                 print_err("swap_buffer_uninit error %d\n", result);
297                 result = -E_SD_BUFFER_ERROR;
298         } else {
299                 result = E_SD_SUCCESS;
300         }
301
302         /* Reinit driver_to_buffer vars */
303         init_buffers_to_read();
304         pages_per_buffer = 0;
305
306         return result;
307 }
308
309 /* Get next buffer to read */
310 int driver_to_buffer_next_buffer_to_read(void)
311 {
312         int result;
313
314         /* If there is busy_buffer first release it */
315         if (busy_buffer) {
316                 result = driver_to_buffer_release();
317                 if (result)
318                         return result;
319         }
320
321         /* If there is no buffers to read, return E_SD_NO_DATA_TO_READ.
322          * SHOULD BE POSITIVE, cause there is no real error. */
323         if (!something_to_read()) {
324                 return E_SD_NO_DATA_TO_READ;
325         }
326
327         /* Get next buffer to read */
328         result = driver_to_buffer_get();
329         if (result < 0) {
330                 print_err("buffer_to_reads > 0, but there are no buffers to read\n");
331                 return result;
332         }
333
334         /* Decrement buffers_to_read counter */
335         dec_buffers_to_read();
336
337         return E_SD_SUCCESS;
338 }
339