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> | |
6 | * Copyright (C) 2008-2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
7 | * | |
8 | * Re-using content from kernel/relay.c. | |
9 | * | |
10 | * This file is released under the GPL v2. | |
11 | */ | |
12 | ||
13 | #include <linux/module.h> | |
14 | #include <linux/fs.h> | |
15 | ||
90225db5 | 16 | #include "../../wrapper/splice.h" |
f3bc08c5 MD |
17 | #include "../../wrapper/ringbuffer/backend.h" |
18 | #include "../../wrapper/ringbuffer/frontend.h" | |
19 | #include "../../wrapper/ringbuffer/vfs.h" | |
20 | ||
21 | #if 0 | |
22 | #define printk_dbg(fmt, args...) printk(fmt, args) | |
23 | #else | |
24 | #define printk_dbg(fmt, args...) | |
25 | #endif | |
26 | ||
27 | loff_t lib_ring_buffer_no_llseek(struct file *file, loff_t offset, int origin) | |
28 | { | |
29 | return -ESPIPE; | |
30 | } | |
31 | ||
32 | /* | |
33 | * Release pages from the buffer so splice pipe_to_file can move them. | |
34 | * Called after the pipe has been populated with buffer pages. | |
35 | */ | |
36 | static void lib_ring_buffer_pipe_buf_release(struct pipe_inode_info *pipe, | |
37 | struct pipe_buffer *pbuf) | |
38 | { | |
39 | __free_page(pbuf->page); | |
40 | } | |
41 | ||
42 | static const struct pipe_buf_operations ring_buffer_pipe_buf_ops = { | |
43 | .can_merge = 0, | |
44 | .map = generic_pipe_buf_map, | |
45 | .unmap = generic_pipe_buf_unmap, | |
46 | .confirm = generic_pipe_buf_confirm, | |
47 | .release = lib_ring_buffer_pipe_buf_release, | |
48 | .steal = generic_pipe_buf_steal, | |
49 | .get = generic_pipe_buf_get, | |
50 | }; | |
51 | ||
52 | /* | |
53 | * Page release operation after splice pipe_to_file ends. | |
54 | */ | |
55 | static void lib_ring_buffer_page_release(struct splice_pipe_desc *spd, | |
56 | unsigned int i) | |
57 | { | |
58 | __free_page(spd->pages[i]); | |
59 | } | |
60 | ||
61 | /* | |
62 | * subbuf_splice_actor - splice up to one subbuf's worth of data | |
63 | */ | |
64 | static int subbuf_splice_actor(struct file *in, | |
65 | loff_t *ppos, | |
66 | struct pipe_inode_info *pipe, | |
67 | size_t len, | |
68 | unsigned int flags) | |
69 | { | |
70 | struct lib_ring_buffer *buf = in->private_data; | |
71 | struct channel *chan = buf->backend.chan; | |
72 | const struct lib_ring_buffer_config *config = chan->backend.config; | |
73 | unsigned int poff, subbuf_pages, nr_pages; | |
74 | struct page *pages[PIPE_DEF_BUFFERS]; | |
75 | struct partial_page partial[PIPE_DEF_BUFFERS]; | |
76 | struct splice_pipe_desc spd = { | |
77 | .pages = pages, | |
78 | .nr_pages = 0, | |
79 | .partial = partial, | |
80 | .flags = flags, | |
81 | .ops = &ring_buffer_pipe_buf_ops, | |
82 | .spd_release = lib_ring_buffer_page_release, | |
83 | }; | |
84 | unsigned long consumed_old, consumed_idx, roffset; | |
85 | unsigned long bytes_avail; | |
86 | ||
87 | /* | |
88 | * Check that a GET_SUBBUF ioctl has been done before. | |
89 | */ | |
90 | WARN_ON(atomic_long_read(&buf->active_readers) != 1); | |
91 | consumed_old = lib_ring_buffer_get_consumed(config, buf); | |
92 | consumed_old += *ppos; | |
93 | consumed_idx = subbuf_index(consumed_old, chan); | |
94 | ||
95 | /* | |
96 | * Adjust read len, if longer than what is available. | |
97 | * Max read size is 1 subbuffer due to get_subbuf/put_subbuf for | |
98 | * protection. | |
99 | */ | |
100 | bytes_avail = chan->backend.subbuf_size; | |
101 | WARN_ON(bytes_avail > chan->backend.buf_size); | |
102 | len = min_t(size_t, len, bytes_avail); | |
103 | subbuf_pages = bytes_avail >> PAGE_SHIFT; | |
104 | nr_pages = min_t(unsigned int, subbuf_pages, PIPE_DEF_BUFFERS); | |
105 | roffset = consumed_old & PAGE_MASK; | |
106 | poff = consumed_old & ~PAGE_MASK; | |
107 | printk_dbg(KERN_DEBUG "SPLICE actor len %zu pos %zd write_pos %ld\n", | |
108 | len, (ssize_t)*ppos, lib_ring_buffer_get_offset(config, buf)); | |
109 | ||
110 | for (; spd.nr_pages < nr_pages; spd.nr_pages++) { | |
111 | unsigned int this_len; | |
112 | struct page **page, *new_page; | |
113 | void **virt; | |
114 | ||
115 | if (!len) | |
116 | break; | |
117 | printk_dbg(KERN_DEBUG "SPLICE actor loop len %zu roffset %ld\n", | |
118 | len, roffset); | |
119 | ||
120 | /* | |
121 | * We have to replace the page we are moving into the splice | |
122 | * pipe. | |
123 | */ | |
124 | new_page = alloc_pages_node(cpu_to_node(max(buf->backend.cpu, | |
125 | 0)), | |
126 | GFP_KERNEL | __GFP_ZERO, 0); | |
127 | if (!new_page) | |
128 | break; | |
129 | ||
130 | this_len = PAGE_SIZE - poff; | |
131 | page = lib_ring_buffer_read_get_page(&buf->backend, roffset, &virt); | |
132 | spd.pages[spd.nr_pages] = *page; | |
133 | *page = new_page; | |
134 | *virt = page_address(new_page); | |
135 | spd.partial[spd.nr_pages].offset = poff; | |
136 | spd.partial[spd.nr_pages].len = this_len; | |
137 | ||
138 | poff = 0; | |
139 | roffset += PAGE_SIZE; | |
140 | len -= this_len; | |
141 | } | |
142 | ||
143 | if (!spd.nr_pages) | |
144 | return 0; | |
145 | ||
90225db5 | 146 | return wrapper_splice_to_pipe(pipe, &spd); |
f3bc08c5 MD |
147 | } |
148 | ||
149 | ssize_t lib_ring_buffer_splice_read(struct file *in, loff_t *ppos, | |
150 | struct pipe_inode_info *pipe, size_t len, | |
151 | unsigned int flags) | |
152 | { | |
153 | struct lib_ring_buffer *buf = in->private_data; | |
154 | struct channel *chan = buf->backend.chan; | |
155 | const struct lib_ring_buffer_config *config = chan->backend.config; | |
156 | ssize_t spliced; | |
157 | int ret; | |
158 | ||
159 | if (config->output != RING_BUFFER_SPLICE) | |
160 | return -EINVAL; | |
161 | ||
162 | /* | |
163 | * We require ppos and length to be page-aligned for performance reasons | |
164 | * (no page copy). Size is known using the ioctl | |
165 | * RING_BUFFER_GET_PADDED_SUBBUF_SIZE, which is page-size padded. | |
166 | * We fail when the ppos or len passed is not page-sized, because splice | |
167 | * is not allowed to copy more than the length passed as parameter (so | |
168 | * the ABI does not let us silently copy more than requested to include | |
169 | * padding). | |
170 | */ | |
171 | if (*ppos != PAGE_ALIGN(*ppos) || len != PAGE_ALIGN(len)) | |
172 | return -EINVAL; | |
173 | ||
174 | ret = 0; | |
175 | spliced = 0; | |
176 | ||
177 | printk_dbg(KERN_DEBUG "SPLICE read len %zu pos %zd\n", len, | |
178 | (ssize_t)*ppos); | |
179 | while (len && !spliced) { | |
180 | ret = subbuf_splice_actor(in, ppos, pipe, len, flags); | |
181 | printk_dbg(KERN_DEBUG "SPLICE read loop ret %d\n", ret); | |
182 | if (ret < 0) | |
183 | break; | |
184 | else if (!ret) { | |
185 | if (flags & SPLICE_F_NONBLOCK) | |
186 | ret = -EAGAIN; | |
187 | break; | |
188 | } | |
189 | ||
190 | *ppos += ret; | |
191 | if (ret > len) | |
192 | len = 0; | |
193 | else | |
194 | len -= ret; | |
195 | spliced += ret; | |
196 | } | |
197 | ||
198 | if (spliced) | |
199 | return spliced; | |
200 | ||
201 | return ret; | |
202 | } | |
203 | EXPORT_SYMBOL_GPL(lib_ring_buffer_splice_read); |