Commit | Line | Data |
---|---|---|
f3bc08c5 MD |
1 | /* |
2 | * ring_buffer_splice.c | |
3 | * | |
4 | * Copyright (C) 2002-2005 - Tom Zanussi <zanussi@us.ibm.com>, IBM Corp | |
5 | * Copyright (C) 1999-2005 - Karim Yaghmour <karim@opersys.com> | |
886d51a3 | 6 | * Copyright (C) 2008-2012 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
f3bc08c5 | 7 | * |
886d51a3 MD |
8 | * This library is free software; you can redistribute it and/or |
9 | * modify it under the terms of the GNU Lesser General Public | |
10 | * License as published by the Free Software Foundation; only | |
11 | * version 2.1 of the License. | |
f3bc08c5 | 12 | * |
886d51a3 MD |
13 | * This library is distributed in the hope that it will be useful, |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 | * Lesser General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU Lesser General Public | |
19 | * License along with this library; if not, write to the Free Software | |
20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
21 | * | |
22 | * Re-using code from kernel/relay.c, which is why it is licensed under | |
23 | * the GPLv2. | |
f3bc08c5 MD |
24 | */ |
25 | ||
26 | #include <linux/module.h> | |
27 | #include <linux/fs.h> | |
989f58e8 | 28 | #include <linux/version.h> |
f3bc08c5 | 29 | |
90225db5 | 30 | #include "../../wrapper/splice.h" |
f3bc08c5 MD |
31 | #include "../../wrapper/ringbuffer/backend.h" |
32 | #include "../../wrapper/ringbuffer/frontend.h" | |
33 | #include "../../wrapper/ringbuffer/vfs.h" | |
34 | ||
35 | #if 0 | |
36 | #define printk_dbg(fmt, args...) printk(fmt, args) | |
37 | #else | |
38 | #define printk_dbg(fmt, args...) | |
39 | #endif | |
40 | ||
d83004aa JD |
41 | loff_t vfs_lib_ring_buffer_no_llseek(struct file *file, loff_t offset, |
42 | int origin) | |
f3bc08c5 MD |
43 | { |
44 | return -ESPIPE; | |
45 | } | |
d83004aa | 46 | EXPORT_SYMBOL_GPL(vfs_lib_ring_buffer_no_llseek); |
f3bc08c5 MD |
47 | |
48 | /* | |
49 | * Release pages from the buffer so splice pipe_to_file can move them. | |
50 | * Called after the pipe has been populated with buffer pages. | |
51 | */ | |
52 | static void lib_ring_buffer_pipe_buf_release(struct pipe_inode_info *pipe, | |
53 | struct pipe_buffer *pbuf) | |
54 | { | |
55 | __free_page(pbuf->page); | |
56 | } | |
57 | ||
58 | static const struct pipe_buf_operations ring_buffer_pipe_buf_ops = { | |
59 | .can_merge = 0, | |
989f58e8 | 60 | #if (LINUX_VERSION_CODE < KERNEL_VERSION(3,15,0)) |
f3bc08c5 MD |
61 | .map = generic_pipe_buf_map, |
62 | .unmap = generic_pipe_buf_unmap, | |
989f58e8 | 63 | #endif |
f3bc08c5 MD |
64 | .confirm = generic_pipe_buf_confirm, |
65 | .release = lib_ring_buffer_pipe_buf_release, | |
66 | .steal = generic_pipe_buf_steal, | |
67 | .get = generic_pipe_buf_get, | |
68 | }; | |
69 | ||
70 | /* | |
71 | * Page release operation after splice pipe_to_file ends. | |
72 | */ | |
73 | static void lib_ring_buffer_page_release(struct splice_pipe_desc *spd, | |
74 | unsigned int i) | |
75 | { | |
76 | __free_page(spd->pages[i]); | |
77 | } | |
78 | ||
79 | /* | |
80 | * subbuf_splice_actor - splice up to one subbuf's worth of data | |
81 | */ | |
82 | static int subbuf_splice_actor(struct file *in, | |
83 | loff_t *ppos, | |
84 | struct pipe_inode_info *pipe, | |
85 | size_t len, | |
d83004aa JD |
86 | unsigned int flags, |
87 | struct lib_ring_buffer *buf) | |
f3bc08c5 | 88 | { |
f3bc08c5 | 89 | struct channel *chan = buf->backend.chan; |
5a8fd222 | 90 | const struct lib_ring_buffer_config *config = &chan->backend.config; |
f3bc08c5 MD |
91 | unsigned int poff, subbuf_pages, nr_pages; |
92 | struct page *pages[PIPE_DEF_BUFFERS]; | |
93 | struct partial_page partial[PIPE_DEF_BUFFERS]; | |
94 | struct splice_pipe_desc spd = { | |
95 | .pages = pages, | |
96 | .nr_pages = 0, | |
97 | .partial = partial, | |
98 | .flags = flags, | |
99 | .ops = &ring_buffer_pipe_buf_ops, | |
100 | .spd_release = lib_ring_buffer_page_release, | |
101 | }; | |
88dfd899 | 102 | unsigned long consumed_old, roffset; |
f3bc08c5 MD |
103 | unsigned long bytes_avail; |
104 | ||
105 | /* | |
106 | * Check that a GET_SUBBUF ioctl has been done before. | |
107 | */ | |
108 | WARN_ON(atomic_long_read(&buf->active_readers) != 1); | |
109 | consumed_old = lib_ring_buffer_get_consumed(config, buf); | |
110 | consumed_old += *ppos; | |
f3bc08c5 MD |
111 | |
112 | /* | |
113 | * Adjust read len, if longer than what is available. | |
114 | * Max read size is 1 subbuffer due to get_subbuf/put_subbuf for | |
115 | * protection. | |
116 | */ | |
117 | bytes_avail = chan->backend.subbuf_size; | |
118 | WARN_ON(bytes_avail > chan->backend.buf_size); | |
119 | len = min_t(size_t, len, bytes_avail); | |
120 | subbuf_pages = bytes_avail >> PAGE_SHIFT; | |
121 | nr_pages = min_t(unsigned int, subbuf_pages, PIPE_DEF_BUFFERS); | |
122 | roffset = consumed_old & PAGE_MASK; | |
123 | poff = consumed_old & ~PAGE_MASK; | |
124 | printk_dbg(KERN_DEBUG "SPLICE actor len %zu pos %zd write_pos %ld\n", | |
125 | len, (ssize_t)*ppos, lib_ring_buffer_get_offset(config, buf)); | |
126 | ||
127 | for (; spd.nr_pages < nr_pages; spd.nr_pages++) { | |
128 | unsigned int this_len; | |
129 | struct page **page, *new_page; | |
130 | void **virt; | |
131 | ||
132 | if (!len) | |
133 | break; | |
134 | printk_dbg(KERN_DEBUG "SPLICE actor loop len %zu roffset %ld\n", | |
135 | len, roffset); | |
136 | ||
137 | /* | |
138 | * We have to replace the page we are moving into the splice | |
139 | * pipe. | |
140 | */ | |
141 | new_page = alloc_pages_node(cpu_to_node(max(buf->backend.cpu, | |
142 | 0)), | |
143 | GFP_KERNEL | __GFP_ZERO, 0); | |
144 | if (!new_page) | |
145 | break; | |
146 | ||
147 | this_len = PAGE_SIZE - poff; | |
148 | page = lib_ring_buffer_read_get_page(&buf->backend, roffset, &virt); | |
149 | spd.pages[spd.nr_pages] = *page; | |
150 | *page = new_page; | |
151 | *virt = page_address(new_page); | |
152 | spd.partial[spd.nr_pages].offset = poff; | |
153 | spd.partial[spd.nr_pages].len = this_len; | |
154 | ||
155 | poff = 0; | |
156 | roffset += PAGE_SIZE; | |
157 | len -= this_len; | |
158 | } | |
159 | ||
160 | if (!spd.nr_pages) | |
161 | return 0; | |
162 | ||
90225db5 | 163 | return wrapper_splice_to_pipe(pipe, &spd); |
f3bc08c5 MD |
164 | } |
165 | ||
166 | ssize_t lib_ring_buffer_splice_read(struct file *in, loff_t *ppos, | |
167 | struct pipe_inode_info *pipe, size_t len, | |
d83004aa JD |
168 | unsigned int flags, |
169 | struct lib_ring_buffer *buf) | |
f3bc08c5 | 170 | { |
f3bc08c5 | 171 | struct channel *chan = buf->backend.chan; |
5a8fd222 | 172 | const struct lib_ring_buffer_config *config = &chan->backend.config; |
f3bc08c5 MD |
173 | ssize_t spliced; |
174 | int ret; | |
175 | ||
176 | if (config->output != RING_BUFFER_SPLICE) | |
177 | return -EINVAL; | |
178 | ||
179 | /* | |
180 | * We require ppos and length to be page-aligned for performance reasons | |
181 | * (no page copy). Size is known using the ioctl | |
182 | * RING_BUFFER_GET_PADDED_SUBBUF_SIZE, which is page-size padded. | |
183 | * We fail when the ppos or len passed is not page-sized, because splice | |
184 | * is not allowed to copy more than the length passed as parameter (so | |
185 | * the ABI does not let us silently copy more than requested to include | |
186 | * padding). | |
187 | */ | |
188 | if (*ppos != PAGE_ALIGN(*ppos) || len != PAGE_ALIGN(len)) | |
189 | return -EINVAL; | |
190 | ||
191 | ret = 0; | |
192 | spliced = 0; | |
193 | ||
194 | printk_dbg(KERN_DEBUG "SPLICE read len %zu pos %zd\n", len, | |
195 | (ssize_t)*ppos); | |
196 | while (len && !spliced) { | |
d83004aa | 197 | ret = subbuf_splice_actor(in, ppos, pipe, len, flags, buf); |
f3bc08c5 MD |
198 | printk_dbg(KERN_DEBUG "SPLICE read loop ret %d\n", ret); |
199 | if (ret < 0) | |
200 | break; | |
201 | else if (!ret) { | |
202 | if (flags & SPLICE_F_NONBLOCK) | |
203 | ret = -EAGAIN; | |
204 | break; | |
205 | } | |
206 | ||
207 | *ppos += ret; | |
208 | if (ret > len) | |
209 | len = 0; | |
210 | else | |
211 | len -= ret; | |
212 | spliced += ret; | |
213 | } | |
214 | ||
215 | if (spliced) | |
216 | return spliced; | |
217 | ||
218 | return ret; | |
219 | } | |
220 | EXPORT_SYMBOL_GPL(lib_ring_buffer_splice_read); | |
d83004aa JD |
221 | |
222 | ssize_t vfs_lib_ring_buffer_splice_read(struct file *in, loff_t *ppos, | |
223 | struct pipe_inode_info *pipe, size_t len, | |
224 | unsigned int flags) | |
225 | { | |
226 | struct lib_ring_buffer *buf = in->private_data; | |
227 | ||
228 | return lib_ring_buffer_splice_read(in, ppos, pipe, len, flags, buf); | |
229 | } | |
230 | EXPORT_SYMBOL_GPL(vfs_lib_ring_buffer_splice_read); |