3 * modules/driver/driver_to_buffer.c
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.
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.
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.
19 * Copyright (C) Samsung Electronics, 2013
21 * 2013 Alexander Aksenov <a.aksenov@samsung.com>: SWAP device driver implement
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>
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>
36 #include "driver_defs.h"
37 #include "swap_driver_errors.h"
38 #include "device_driver_to_driver_to_buffer.h"
40 /* Current busy buffer */
41 static struct swap_subbuffer *busy_buffer = NULL;
43 /* Buffers count ready to be read */
44 static int buffers_to_read = 0;
46 /* Pages count in one subbuffer */
47 static int pages_per_buffer = 0;
49 /* Used to sync changes of the buffers_to_read var */
50 static spinlock_t buf_to_read;
53 static inline void init_buffers_to_read(void)
55 spin_lock_init(&buf_to_read);
59 static inline void inc_buffers_to_read(void)
63 spin_lock_irqsave(&buf_to_read, flags);
65 spin_unlock_irqrestore(&buf_to_read, flags);
68 static inline void dec_buffers_to_read(void)
72 spin_lock_irqsave(&buf_to_read, flags);
74 spin_unlock_irqrestore(&buf_to_read, flags);
77 static inline void set_buffers_to_read(int count)
81 spin_lock_irqsave(&buf_to_read, flags);
82 buffers_to_read = count;
83 spin_unlock_irqrestore(&buf_to_read, flags);
86 static inline int something_to_read(void)
91 spin_lock_irqsave(&buf_to_read, flags);
92 result = buffers_to_read;
93 spin_unlock_irqrestore(&buf_to_read, flags);
98 /* TODO Get subbuffer for reading */
99 static size_t driver_to_buffer_get(void)
103 /* If there is no readable buffers, return error */
104 result = swap_buffer_get(&busy_buffer);
105 if (result == -E_SB_NO_READABLE_BUFFERS) {
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;
113 return busy_buffer->full_buffer_part;
116 /* TODO Release subbuffer */
117 static int driver_to_buffer_release(void)
122 return -E_SD_NO_BUSY_SUBBUFFER;
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;
137 /* Buffers callback function */
138 int driver_to_buffer_callback(void)
142 /* Increment buffers_to_read counter */
143 inc_buffers_to_read();
144 swap_device_wake_up_process();
150 ssize_t driver_to_buffer_read(char __user *buf, size_t count)
152 size_t bytes_to_copy;
153 size_t bytes_to_read = 0;
154 int page_counter = 0;
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;
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;
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
174 /* Copy and add size to copied bytes count */
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*) *
181 read_from_this_page);
182 bytes_to_copy -= read_from_this_page;
186 return bytes_to_read;
189 /* Flush swap_buffer */
190 int driver_to_buffer_flush(void)
194 result = swap_buffer_flush();
197 set_buffers_to_read(result);
199 return -E_SD_BUFFER_ERROR;
201 swap_device_wake_up_process();
206 /* Fills spd structure */
207 int driver_to_buffer_fill_spd(struct splice_pipe_desc *spd)
209 size_t data_to_splice = busy_buffer->full_buffer_part;
210 struct page **pages = spd->pages;
211 struct partial_page *partial = spd->partial;
213 while (data_to_splice) {
214 size_t read_from_current_page = min(data_to_splice, PAGE_SIZE);
216 pages[spd->nr_pages] = alloc_page(GFP_KERNEL);
217 if (!pages[spd->nr_pages]) {
218 print_err("Cannot alloc page for splice\n");
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);
227 /* Always beginning of the page */
228 partial[spd->nr_pages].offset = 0;
229 partial[spd->nr_pages].len = read_from_current_page;
231 /* Private is not used */
232 partial[spd->nr_pages].private = 0;
235 data_to_splice -= read_from_current_page;
237 /* TODO: add check for pipe->buffers exceeding */
238 /* if (spd->nr_pages == pipe->buffers) { */
245 /* Check for subbuffers ready to be read */
246 int driver_to_buffer_buffer_to_read(void)
248 return busy_buffer ? 1 : 0;
251 /* Set buffers size and count */
252 int driver_to_buffer_initialize(size_t size, unsigned int count)
256 if (size == 0 && count == 0) {
257 return -E_SD_WRONG_ARGS;
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;
266 // TODO Race condition: buffer can be used in other thread till we're in
268 /* Initialize driver_to_buffer variables */
269 pages_per_buffer = result;
271 init_buffers_to_read();
276 /* Uninitialize buffer */
277 int driver_to_buffer_uninitialize(void)
281 /* Release occupied buffer */
283 result = driver_to_buffer_release();
284 // TODO Maybe release anyway
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;
299 result = E_SD_SUCCESS;
302 /* Reinit driver_to_buffer vars */
303 init_buffers_to_read();
304 pages_per_buffer = 0;
309 /* Get next buffer to read */
310 int driver_to_buffer_next_buffer_to_read(void)
314 /* If there is busy_buffer first release it */
316 result = driver_to_buffer_release();
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;
327 /* Get next buffer to read */
328 result = driver_to_buffer_get();
330 print_err("buffer_to_reads > 0, but there are no buffers to read\n");
334 /* Decrement buffers_to_read counter */
335 dec_buffers_to_read();