]> bbs.cooldavid.org Git - net-next-2.6.git/blame - kernel/kfifo.c
kfifo: add kfifo_out_peek
[net-next-2.6.git] / kernel / kfifo.c
CommitLineData
1da177e4 1/*
45465487 2 * A generic kernel FIFO implementation.
1da177e4 3 *
45465487 4 * Copyright (C) 2009 Stefani Seibold <stefani@seibold.net>
1da177e4
LT
5 * Copyright (C) 2004 Stelian Pop <stelian@popies.net>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 *
21 */
22
23#include <linux/kernel.h>
24#include <linux/module.h>
25#include <linux/slab.h>
26#include <linux/err.h>
27#include <linux/kfifo.h>
f84d5a76 28#include <linux/log2.h>
a121f24a 29#include <linux/uaccess.h>
1da177e4 30
8ecc2951 31static void _kfifo_init(struct kfifo *fifo, void *buffer,
c1e13f25 32 unsigned int size)
45465487
SS
33{
34 fifo->buffer = buffer;
35 fifo->size = size;
45465487
SS
36
37 kfifo_reset(fifo);
38}
39
1da177e4 40/**
45465487
SS
41 * kfifo_init - initialize a FIFO using a preallocated buffer
42 * @fifo: the fifo to assign the buffer
1da177e4
LT
43 * @buffer: the preallocated buffer to be used.
44 * @size: the size of the internal buffer, this have to be a power of 2.
1da177e4 45 *
1da177e4 46 */
8ecc2951 47void kfifo_init(struct kfifo *fifo, void *buffer, unsigned int size)
1da177e4 48{
1da177e4 49 /* size must be a power of 2 */
f84d5a76 50 BUG_ON(!is_power_of_2(size));
1da177e4 51
c1e13f25 52 _kfifo_init(fifo, buffer, size);
1da177e4
LT
53}
54EXPORT_SYMBOL(kfifo_init);
55
56/**
45465487
SS
57 * kfifo_alloc - allocates a new FIFO internal buffer
58 * @fifo: the fifo to assign then new buffer
59 * @size: the size of the buffer to be allocated, this have to be a power of 2.
1da177e4 60 * @gfp_mask: get_free_pages mask, passed to kmalloc()
1da177e4 61 *
45465487
SS
62 * This function dynamically allocates a new fifo internal buffer
63 *
1da177e4 64 * The size will be rounded-up to a power of 2.
45465487
SS
65 * The buffer will be release with kfifo_free().
66 * Return 0 if no error, otherwise the an error code
1da177e4 67 */
c1e13f25 68int kfifo_alloc(struct kfifo *fifo, unsigned int size, gfp_t gfp_mask)
1da177e4
LT
69{
70 unsigned char *buffer;
1da177e4
LT
71
72 /*
73 * round up to the next power of 2, since our 'let the indices
b33112d1 74 * wrap' technique works only in this case.
1da177e4 75 */
b33112d1 76 if (!is_power_of_2(size)) {
1da177e4
LT
77 BUG_ON(size > 0x80000000);
78 size = roundup_pow_of_two(size);
79 }
80
81 buffer = kmalloc(size, gfp_mask);
45465487 82 if (!buffer) {
c1e13f25 83 _kfifo_init(fifo, 0, 0);
45465487
SS
84 return -ENOMEM;
85 }
1da177e4 86
c1e13f25 87 _kfifo_init(fifo, buffer, size);
1da177e4 88
45465487 89 return 0;
1da177e4
LT
90}
91EXPORT_SYMBOL(kfifo_alloc);
92
93/**
45465487 94 * kfifo_free - frees the FIFO internal buffer
1da177e4
LT
95 * @fifo: the fifo to be freed.
96 */
97void kfifo_free(struct kfifo *fifo)
98{
99 kfree(fifo->buffer);
1da177e4
LT
100}
101EXPORT_SYMBOL(kfifo_free);
102
a121f24a
SS
103/**
104 * kfifo_skip - skip output data
105 * @fifo: the fifo to be used.
106 * @len: number of bytes to skip
107 */
108void kfifo_skip(struct kfifo *fifo, unsigned int len)
109{
110 if (len < kfifo_len(fifo)) {
111 __kfifo_add_out(fifo, len);
112 return;
113 }
114 kfifo_reset_out(fifo);
115}
116EXPORT_SYMBOL(kfifo_skip);
117
86d48803
SS
118static inline void __kfifo_in_data(struct kfifo *fifo,
119 const void *from, unsigned int len, unsigned int off)
1da177e4
LT
120{
121 unsigned int l;
122
a45bce49
PM
123 /*
124 * Ensure that we sample the fifo->out index -before- we
125 * start putting bytes into the kfifo.
126 */
127
128 smp_mb();
129
86d48803 130 off = __kfifo_off(fifo, fifo->in + off);
a121f24a 131
1da177e4 132 /* first put the data starting from fifo->in to buffer end */
a121f24a
SS
133 l = min(len, fifo->size - off);
134 memcpy(fifo->buffer + off, from, l);
1da177e4
LT
135
136 /* then put the rest (if any) at the beginning of the buffer */
7acd72eb 137 memcpy(fifo->buffer, from + l, len - l);
1da177e4 138}
1da177e4 139
86d48803
SS
140static inline void __kfifo_out_data(struct kfifo *fifo,
141 void *to, unsigned int len, unsigned int off)
1da177e4
LT
142{
143 unsigned int l;
144
a45bce49
PM
145 /*
146 * Ensure that we sample the fifo->in index -before- we
147 * start removing bytes from the kfifo.
148 */
149
150 smp_rmb();
151
86d48803 152 off = __kfifo_off(fifo, fifo->out + off);
a121f24a 153
1da177e4 154 /* first get the data from fifo->out until the end of the buffer */
a121f24a
SS
155 l = min(len, fifo->size - off);
156 memcpy(to, fifo->buffer + off, l);
1da177e4
LT
157
158 /* then get the rest (if any) from the beginning of the buffer */
7acd72eb 159 memcpy(to + l, fifo->buffer, len - l);
a121f24a 160}
a121f24a 161
64ce1037
AK
162static inline int __kfifo_from_user_data(struct kfifo *fifo,
163 const void __user *from, unsigned int len, unsigned int off,
164 unsigned *lenout)
a121f24a 165{
a121f24a
SS
166 unsigned int l;
167 int ret;
168
a45bce49 169 /*
a121f24a
SS
170 * Ensure that we sample the fifo->out index -before- we
171 * start putting bytes into the kfifo.
a45bce49
PM
172 */
173
174 smp_mb();
175
86d48803 176 off = __kfifo_off(fifo, fifo->in + off);
a121f24a
SS
177
178 /* first put the data starting from fifo->in to buffer end */
179 l = min(len, fifo->size - off);
180 ret = copy_from_user(fifo->buffer + off, from, l);
64ce1037
AK
181 if (unlikely(ret)) {
182 *lenout = ret;
183 return -EFAULT;
184 }
185 *lenout = l;
a121f24a
SS
186
187 /* then put the rest (if any) at the beginning of the buffer */
64ce1037
AK
188 ret = copy_from_user(fifo->buffer, from + l, len - l);
189 *lenout += ret ? ret : len - l;
190 return ret ? -EFAULT : 0;
86d48803
SS
191}
192
64ce1037
AK
193static inline int __kfifo_to_user_data(struct kfifo *fifo,
194 void __user *to, unsigned int len, unsigned int off, unsigned *lenout)
86d48803
SS
195{
196 unsigned int l;
197 int ret;
198
199 /*
200 * Ensure that we sample the fifo->in index -before- we
201 * start removing bytes from the kfifo.
202 */
203
204 smp_rmb();
205
206 off = __kfifo_off(fifo, fifo->out + off);
207
208 /* first get the data from fifo->out until the end of the buffer */
209 l = min(len, fifo->size - off);
210 ret = copy_to_user(to, fifo->buffer + off, l);
64ce1037
AK
211 *lenout = l;
212 if (unlikely(ret)) {
213 *lenout -= ret;
214 return -EFAULT;
215 }
a121f24a 216
86d48803 217 /* then get the rest (if any) from the beginning of the buffer */
64ce1037
AK
218 len -= l;
219 ret = copy_to_user(to + l, fifo->buffer, len);
220 if (unlikely(ret)) {
221 *lenout += len - ret;
222 return -EFAULT;
223 }
224 *lenout += len;
225 return 0;
86d48803 226}
1da177e4 227
86d48803
SS
228unsigned int __kfifo_in_n(struct kfifo *fifo,
229 const void *from, unsigned int len, unsigned int recsize)
230{
231 if (kfifo_avail(fifo) < len + recsize)
232 return len + 1;
233
234 __kfifo_in_data(fifo, from, len, recsize);
235 return 0;
236}
237EXPORT_SYMBOL(__kfifo_in_n);
238
239/**
240 * kfifo_in - puts some data into the FIFO
241 * @fifo: the fifo to be used.
242 * @from: the data to be added.
243 * @len: the length of the data to be added.
244 *
245 * This function copies at most @len bytes from the @from buffer into
246 * the FIFO depending on the free space, and returns the number of
247 * bytes copied.
248 *
249 * Note that with only one concurrent reader and one concurrent
250 * writer, you don't need extra locking to use these functions.
251 */
8ecc2951 252unsigned int kfifo_in(struct kfifo *fifo, const void *from,
86d48803
SS
253 unsigned int len)
254{
255 len = min(kfifo_avail(fifo), len);
256
257 __kfifo_in_data(fifo, from, len, 0);
258 __kfifo_add_in(fifo, len);
1da177e4
LT
259 return len;
260}
86d48803
SS
261EXPORT_SYMBOL(kfifo_in);
262
263unsigned int __kfifo_in_generic(struct kfifo *fifo,
264 const void *from, unsigned int len, unsigned int recsize)
265{
266 return __kfifo_in_rec(fifo, from, len, recsize);
267}
268EXPORT_SYMBOL(__kfifo_in_generic);
269
270unsigned int __kfifo_out_n(struct kfifo *fifo,
271 void *to, unsigned int len, unsigned int recsize)
272{
273 if (kfifo_len(fifo) < len + recsize)
274 return len;
275
276 __kfifo_out_data(fifo, to, len, recsize);
277 __kfifo_add_out(fifo, len + recsize);
278 return 0;
279}
280EXPORT_SYMBOL(__kfifo_out_n);
a121f24a
SS
281
282/**
86d48803 283 * kfifo_out - gets some data from the FIFO
a121f24a
SS
284 * @fifo: the fifo to be used.
285 * @to: where the data must be copied.
286 * @len: the size of the destination buffer.
287 *
288 * This function copies at most @len bytes from the FIFO into the
289 * @to buffer and returns the number of copied bytes.
290 *
291 * Note that with only one concurrent reader and one concurrent
292 * writer, you don't need extra locking to use these functions.
293 */
8ecc2951 294unsigned int kfifo_out(struct kfifo *fifo, void *to, unsigned int len)
a121f24a 295{
86d48803 296 len = min(kfifo_len(fifo), len);
a121f24a 297
86d48803
SS
298 __kfifo_out_data(fifo, to, len, 0);
299 __kfifo_add_out(fifo, len);
a121f24a 300
86d48803
SS
301 return len;
302}
303EXPORT_SYMBOL(kfifo_out);
a121f24a 304
a5b9e2c1
AK
305/**
306 * kfifo_out_peek - copy some data from the FIFO, but do not remove it
307 * @fifo: the fifo to be used.
308 * @to: where the data must be copied.
309 * @len: the size of the destination buffer.
310 * @offset: offset into the fifo
311 *
312 * This function copies at most @len bytes at @offset from the FIFO
313 * into the @to buffer and returns the number of copied bytes.
314 * The data is not removed from the FIFO.
315 */
316unsigned int kfifo_out_peek(struct kfifo *fifo, void *to, unsigned int len,
317 unsigned offset)
318{
319 len = min(kfifo_len(fifo), len + offset);
320
321 __kfifo_out_data(fifo, to, len, offset);
322 return len;
323}
324EXPORT_SYMBOL(kfifo_out_peek);
325
86d48803
SS
326unsigned int __kfifo_out_generic(struct kfifo *fifo,
327 void *to, unsigned int len, unsigned int recsize,
328 unsigned int *total)
329{
330 return __kfifo_out_rec(fifo, to, len, recsize, total);
331}
332EXPORT_SYMBOL(__kfifo_out_generic);
a121f24a 333
86d48803
SS
334unsigned int __kfifo_from_user_n(struct kfifo *fifo,
335 const void __user *from, unsigned int len, unsigned int recsize)
336{
64ce1037
AK
337 unsigned total;
338
86d48803
SS
339 if (kfifo_avail(fifo) < len + recsize)
340 return len + 1;
a121f24a 341
64ce1037
AK
342 __kfifo_from_user_data(fifo, from, len, recsize, &total);
343 return total;
86d48803
SS
344}
345EXPORT_SYMBOL(__kfifo_from_user_n);
a121f24a 346
86d48803
SS
347/**
348 * kfifo_from_user - puts some data from user space into the FIFO
349 * @fifo: the fifo to be used.
350 * @from: pointer to the data to be added.
351 * @len: the length of the data to be added.
352 *
353 * This function copies at most @len bytes from the @from into the
64ce1037 354 * FIFO depending and returns -EFAULT/0.
86d48803
SS
355 *
356 * Note that with only one concurrent reader and one concurrent
357 * writer, you don't need extra locking to use these functions.
358 */
64ce1037
AK
359int kfifo_from_user(struct kfifo *fifo,
360 const void __user *from, unsigned int len, unsigned *total)
86d48803 361{
64ce1037 362 int ret;
86d48803 363 len = min(kfifo_avail(fifo), len);
64ce1037
AK
364 ret = __kfifo_from_user_data(fifo, from, len, 0, total);
365 if (ret)
366 return ret;
86d48803 367 __kfifo_add_in(fifo, len);
64ce1037 368 return 0;
86d48803
SS
369}
370EXPORT_SYMBOL(kfifo_from_user);
a121f24a 371
86d48803
SS
372unsigned int __kfifo_from_user_generic(struct kfifo *fifo,
373 const void __user *from, unsigned int len, unsigned int recsize)
374{
375 return __kfifo_from_user_rec(fifo, from, len, recsize);
376}
377EXPORT_SYMBOL(__kfifo_from_user_generic);
a121f24a 378
86d48803
SS
379unsigned int __kfifo_to_user_n(struct kfifo *fifo,
380 void __user *to, unsigned int len, unsigned int reclen,
381 unsigned int recsize)
382{
64ce1037 383 unsigned int ret, total;
a121f24a 384
86d48803
SS
385 if (kfifo_len(fifo) < reclen + recsize)
386 return len;
a121f24a 387
64ce1037 388 ret = __kfifo_to_user_data(fifo, to, reclen, recsize, &total);
86d48803
SS
389
390 if (likely(ret == 0))
391 __kfifo_add_out(fifo, reclen + recsize);
392
64ce1037 393 return total;
86d48803
SS
394}
395EXPORT_SYMBOL(__kfifo_to_user_n);
396
397/**
398 * kfifo_to_user - gets data from the FIFO and write it to user space
399 * @fifo: the fifo to be used.
400 * @to: where the data must be copied.
401 * @len: the size of the destination buffer.
64ce1037 402 @ @lenout: pointer to output variable with copied data
86d48803
SS
403 *
404 * This function copies at most @len bytes from the FIFO into the
64ce1037 405 * @to buffer and 0 or -EFAULT.
86d48803
SS
406 *
407 * Note that with only one concurrent reader and one concurrent
408 * writer, you don't need extra locking to use these functions.
409 */
64ce1037
AK
410int kfifo_to_user(struct kfifo *fifo,
411 void __user *to, unsigned int len, unsigned *lenout)
86d48803 412{
64ce1037 413 int ret;
86d48803 414 len = min(kfifo_len(fifo), len);
64ce1037
AK
415 ret = __kfifo_to_user_data(fifo, to, len, 0, lenout);
416 __kfifo_add_out(fifo, *lenout);
417 return ret;
a121f24a
SS
418}
419EXPORT_SYMBOL(kfifo_to_user);
420
86d48803
SS
421unsigned int __kfifo_to_user_generic(struct kfifo *fifo,
422 void __user *to, unsigned int len, unsigned int recsize,
423 unsigned int *total)
424{
425 return __kfifo_to_user_rec(fifo, to, len, recsize, total);
426}
427EXPORT_SYMBOL(__kfifo_to_user_generic);
428
429unsigned int __kfifo_peek_generic(struct kfifo *fifo, unsigned int recsize)
430{
431 if (recsize == 0)
432 return kfifo_avail(fifo);
433
434 return __kfifo_peek_n(fifo, recsize);
435}
436EXPORT_SYMBOL(__kfifo_peek_generic);
437
438void __kfifo_skip_generic(struct kfifo *fifo, unsigned int recsize)
439{
440 __kfifo_skip_rec(fifo, recsize);
441}
442EXPORT_SYMBOL(__kfifo_skip_generic);
443