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-2012 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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.
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.
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
22 * Re-using code from kernel/relay.c, which is why it is licensed under
26 #include <linux/module.h>
29 #include "../../wrapper/splice.h"
30 #include "../../wrapper/ringbuffer/backend.h"
31 #include "../../wrapper/ringbuffer/frontend.h"
32 #include "../../wrapper/ringbuffer/vfs.h"
35 #define printk_dbg(fmt, args...) printk(fmt, args)
37 #define printk_dbg(fmt, args...)
40 loff_t
lib_ring_buffer_no_llseek(struct file
*file
, loff_t offset
, int origin
)
46 * Release pages from the buffer so splice pipe_to_file can move them.
47 * Called after the pipe has been populated with buffer pages.
49 static void lib_ring_buffer_pipe_buf_release(struct pipe_inode_info
*pipe
,
50 struct pipe_buffer
*pbuf
)
52 __free_page(pbuf
->page
);
55 static const struct pipe_buf_operations ring_buffer_pipe_buf_ops
= {
57 .map
= generic_pipe_buf_map
,
58 .unmap
= generic_pipe_buf_unmap
,
59 .confirm
= generic_pipe_buf_confirm
,
60 .release
= lib_ring_buffer_pipe_buf_release
,
61 .steal
= generic_pipe_buf_steal
,
62 .get
= generic_pipe_buf_get
,
66 * Page release operation after splice pipe_to_file ends.
68 static void lib_ring_buffer_page_release(struct splice_pipe_desc
*spd
,
71 __free_page(spd
->pages
[i
]);
75 * subbuf_splice_actor - splice up to one subbuf's worth of data
77 static int subbuf_splice_actor(struct file
*in
,
79 struct pipe_inode_info
*pipe
,
83 struct lib_ring_buffer
*buf
= in
->private_data
;
84 struct channel
*chan
= buf
->backend
.chan
;
85 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
86 unsigned int poff
, subbuf_pages
, nr_pages
;
87 struct page
*pages
[PIPE_DEF_BUFFERS
];
88 struct partial_page partial
[PIPE_DEF_BUFFERS
];
89 struct splice_pipe_desc spd
= {
94 .ops
= &ring_buffer_pipe_buf_ops
,
95 .spd_release
= lib_ring_buffer_page_release
,
97 unsigned long consumed_old
, roffset
;
98 unsigned long bytes_avail
;
101 * Check that a GET_SUBBUF ioctl has been done before.
103 WARN_ON(atomic_long_read(&buf
->active_readers
) != 1);
104 consumed_old
= lib_ring_buffer_get_consumed(config
, buf
);
105 consumed_old
+= *ppos
;
108 * Adjust read len, if longer than what is available.
109 * Max read size is 1 subbuffer due to get_subbuf/put_subbuf for
112 bytes_avail
= chan
->backend
.subbuf_size
;
113 WARN_ON(bytes_avail
> chan
->backend
.buf_size
);
114 len
= min_t(size_t, len
, bytes_avail
);
115 subbuf_pages
= bytes_avail
>> PAGE_SHIFT
;
116 nr_pages
= min_t(unsigned int, subbuf_pages
, PIPE_DEF_BUFFERS
);
117 roffset
= consumed_old
& PAGE_MASK
;
118 poff
= consumed_old
& ~PAGE_MASK
;
119 printk_dbg(KERN_DEBUG
"SPLICE actor len %zu pos %zd write_pos %ld\n",
120 len
, (ssize_t
)*ppos
, lib_ring_buffer_get_offset(config
, buf
));
122 for (; spd
.nr_pages
< nr_pages
; spd
.nr_pages
++) {
123 unsigned int this_len
;
124 struct page
**page
, *new_page
;
129 printk_dbg(KERN_DEBUG
"SPLICE actor loop len %zu roffset %ld\n",
133 * We have to replace the page we are moving into the splice
136 new_page
= alloc_pages_node(cpu_to_node(max(buf
->backend
.cpu
,
138 GFP_KERNEL
| __GFP_ZERO
, 0);
142 this_len
= PAGE_SIZE
- poff
;
143 page
= lib_ring_buffer_read_get_page(&buf
->backend
, roffset
, &virt
);
144 spd
.pages
[spd
.nr_pages
] = *page
;
146 *virt
= page_address(new_page
);
147 spd
.partial
[spd
.nr_pages
].offset
= poff
;
148 spd
.partial
[spd
.nr_pages
].len
= this_len
;
151 roffset
+= PAGE_SIZE
;
158 return wrapper_splice_to_pipe(pipe
, &spd
);
161 ssize_t
lib_ring_buffer_splice_read(struct file
*in
, loff_t
*ppos
,
162 struct pipe_inode_info
*pipe
, size_t len
,
165 struct lib_ring_buffer
*buf
= in
->private_data
;
166 struct channel
*chan
= buf
->backend
.chan
;
167 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
171 if (config
->output
!= RING_BUFFER_SPLICE
)
175 * We require ppos and length to be page-aligned for performance reasons
176 * (no page copy). Size is known using the ioctl
177 * RING_BUFFER_GET_PADDED_SUBBUF_SIZE, which is page-size padded.
178 * We fail when the ppos or len passed is not page-sized, because splice
179 * is not allowed to copy more than the length passed as parameter (so
180 * the ABI does not let us silently copy more than requested to include
183 if (*ppos
!= PAGE_ALIGN(*ppos
) || len
!= PAGE_ALIGN(len
))
189 printk_dbg(KERN_DEBUG
"SPLICE read len %zu pos %zd\n", len
,
191 while (len
&& !spliced
) {
192 ret
= subbuf_splice_actor(in
, ppos
, pipe
, len
, flags
);
193 printk_dbg(KERN_DEBUG
"SPLICE read loop ret %d\n", ret
);
197 if (flags
& SPLICE_F_NONBLOCK
)
215 EXPORT_SYMBOL_GPL(lib_ring_buffer_splice_read
);