]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - lib/kfifo.c
Btrfs: fix device replace of a missing RAID 5/6 device
[mirror_ubuntu-artful-kernel.git] / lib / kfifo.c
CommitLineData
1da177e4 1/*
2e956fb3 2 * A generic kernel FIFO implementation
1da177e4 3 *
2e956fb3 4 * Copyright (C) 2009/2010 Stefani Seibold <stefani@seibold.net>
1da177e4
LT
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
d78a3eda 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1da177e4
LT
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 *
20 */
21
22#include <linux/kernel.h>
9984de1a 23#include <linux/export.h>
1da177e4
LT
24#include <linux/slab.h>
25#include <linux/err.h>
f84d5a76 26#include <linux/log2.h>
a121f24a 27#include <linux/uaccess.h>
2e956fb3 28#include <linux/kfifo.h>
1da177e4 29
2e956fb3
SS
30/*
31 * internal helper to calculate the unused elements in a fifo
1da177e4 32 */
2e956fb3 33static inline unsigned int kfifo_unused(struct __kfifo *fifo)
1da177e4 34{
2e956fb3 35 return (fifo->mask + 1) - (fifo->in - fifo->out);
1da177e4 36}
1da177e4 37
2e956fb3
SS
38int __kfifo_alloc(struct __kfifo *fifo, unsigned int size,
39 size_t esize, gfp_t gfp_mask)
1da177e4 40{
1da177e4 41 /*
2e956fb3 42 * round down to the next power of 2, since our 'let the indices
b33112d1 43 * wrap' technique works only in this case.
1da177e4 44 */
dfe2a77f 45 size = roundup_pow_of_two(size);
2e956fb3
SS
46
47 fifo->in = 0;
48 fifo->out = 0;
49 fifo->esize = esize;
50
51 if (size < 2) {
52 fifo->data = NULL;
53 fifo->mask = 0;
54 return -EINVAL;
1da177e4
LT
55 }
56
2e956fb3
SS
57 fifo->data = kmalloc(size * esize, gfp_mask);
58
59 if (!fifo->data) {
60 fifo->mask = 0;
45465487
SS
61 return -ENOMEM;
62 }
2e956fb3 63 fifo->mask = size - 1;
1da177e4 64
45465487 65 return 0;
1da177e4 66}
2e956fb3 67EXPORT_SYMBOL(__kfifo_alloc);
1da177e4 68
2e956fb3 69void __kfifo_free(struct __kfifo *fifo)
1da177e4 70{
2e956fb3
SS
71 kfree(fifo->data);
72 fifo->in = 0;
73 fifo->out = 0;
74 fifo->esize = 0;
75 fifo->data = NULL;
76 fifo->mask = 0;
1da177e4 77}
2e956fb3 78EXPORT_SYMBOL(__kfifo_free);
1da177e4 79
2e956fb3
SS
80int __kfifo_init(struct __kfifo *fifo, void *buffer,
81 unsigned int size, size_t esize)
a121f24a 82{
2e956fb3
SS
83 size /= esize;
84
dfe2a77f 85 size = roundup_pow_of_two(size);
2e956fb3
SS
86
87 fifo->in = 0;
88 fifo->out = 0;
89 fifo->esize = esize;
90 fifo->data = buffer;
91
92 if (size < 2) {
93 fifo->mask = 0;
94 return -EINVAL;
a121f24a 95 }
2e956fb3
SS
96 fifo->mask = size - 1;
97
98 return 0;
a121f24a 99}
2e956fb3 100EXPORT_SYMBOL(__kfifo_init);
a121f24a 101
2e956fb3
SS
102static void kfifo_copy_in(struct __kfifo *fifo, const void *src,
103 unsigned int len, unsigned int off)
1da177e4 104{
2e956fb3
SS
105 unsigned int size = fifo->mask + 1;
106 unsigned int esize = fifo->esize;
1da177e4
LT
107 unsigned int l;
108
2e956fb3
SS
109 off &= fifo->mask;
110 if (esize != 1) {
111 off *= esize;
112 size *= esize;
113 len *= esize;
114 }
115 l = min(len, size - off);
116
117 memcpy(fifo->data + off, src, l);
118 memcpy(fifo->data, src + l, len - l);
a45bce49 119 /*
2e956fb3
SS
120 * make sure that the data in the fifo is up to date before
121 * incrementing the fifo->in index counter
a45bce49 122 */
2e956fb3
SS
123 smp_wmb();
124}
a45bce49 125
2e956fb3
SS
126unsigned int __kfifo_in(struct __kfifo *fifo,
127 const void *buf, unsigned int len)
128{
129 unsigned int l;
a121f24a 130
2e956fb3
SS
131 l = kfifo_unused(fifo);
132 if (len > l)
133 len = l;
1da177e4 134
2e956fb3
SS
135 kfifo_copy_in(fifo, buf, len, fifo->in);
136 fifo->in += len;
137 return len;
1da177e4 138}
2e956fb3 139EXPORT_SYMBOL(__kfifo_in);
1da177e4 140
2e956fb3
SS
141static void kfifo_copy_out(struct __kfifo *fifo, void *dst,
142 unsigned int len, unsigned int off)
1da177e4 143{
2e956fb3
SS
144 unsigned int size = fifo->mask + 1;
145 unsigned int esize = fifo->esize;
1da177e4
LT
146 unsigned int l;
147
2e956fb3
SS
148 off &= fifo->mask;
149 if (esize != 1) {
150 off *= esize;
151 size *= esize;
152 len *= esize;
153 }
154 l = min(len, size - off);
155
156 memcpy(dst, fifo->data + off, l);
157 memcpy(dst + l, fifo->data, len - l);
a45bce49 158 /*
2e956fb3
SS
159 * make sure that the data is copied before
160 * incrementing the fifo->out index counter
a45bce49 161 */
2e956fb3
SS
162 smp_wmb();
163}
a45bce49 164
2e956fb3
SS
165unsigned int __kfifo_out_peek(struct __kfifo *fifo,
166 void *buf, unsigned int len)
167{
168 unsigned int l;
a45bce49 169
2e956fb3
SS
170 l = fifo->in - fifo->out;
171 if (len > l)
172 len = l;
a121f24a 173
2e956fb3
SS
174 kfifo_copy_out(fifo, buf, len, fifo->out);
175 return len;
176}
177EXPORT_SYMBOL(__kfifo_out_peek);
1da177e4 178
2e956fb3
SS
179unsigned int __kfifo_out(struct __kfifo *fifo,
180 void *buf, unsigned int len)
181{
182 len = __kfifo_out_peek(fifo, buf, len);
183 fifo->out += len;
184 return len;
a121f24a 185}
2e956fb3 186EXPORT_SYMBOL(__kfifo_out);
a121f24a 187
2e956fb3
SS
188static unsigned long kfifo_copy_from_user(struct __kfifo *fifo,
189 const void __user *from, unsigned int len, unsigned int off,
190 unsigned int *copied)
a121f24a 191{
2e956fb3
SS
192 unsigned int size = fifo->mask + 1;
193 unsigned int esize = fifo->esize;
a121f24a 194 unsigned int l;
2e956fb3 195 unsigned long ret;
a121f24a 196
2e956fb3
SS
197 off &= fifo->mask;
198 if (esize != 1) {
199 off *= esize;
200 size *= esize;
201 len *= esize;
202 }
203 l = min(len, size - off);
204
205 ret = copy_from_user(fifo->data + off, from, l);
206 if (unlikely(ret))
207 ret = DIV_ROUND_UP(ret + len - l, esize);
208 else {
209 ret = copy_from_user(fifo->data, from + l, len - l);
210 if (unlikely(ret))
211 ret = DIV_ROUND_UP(ret, esize);
212 }
a45bce49 213 /*
2e956fb3
SS
214 * make sure that the data in the fifo is up to date before
215 * incrementing the fifo->in index counter
a45bce49 216 */
2e956fb3 217 smp_wmb();
a019e48c 218 *copied = len - ret * esize;
2e956fb3
SS
219 /* return the number of elements which are not copied */
220 return ret;
221}
a45bce49 222
2e956fb3
SS
223int __kfifo_from_user(struct __kfifo *fifo, const void __user *from,
224 unsigned long len, unsigned int *copied)
225{
226 unsigned int l;
227 unsigned long ret;
228 unsigned int esize = fifo->esize;
229 int err;
a45bce49 230
2e956fb3
SS
231 if (esize != 1)
232 len /= esize;
a121f24a 233
2e956fb3
SS
234 l = kfifo_unused(fifo);
235 if (len > l)
236 len = l;
a121f24a 237
2e956fb3
SS
238 ret = kfifo_copy_from_user(fifo, from, len, fifo->in, copied);
239 if (unlikely(ret)) {
240 len -= ret;
241 err = -EFAULT;
242 } else
243 err = 0;
244 fifo->in += len;
245 return err;
86d48803 246}
2e956fb3 247EXPORT_SYMBOL(__kfifo_from_user);
86d48803 248
2e956fb3
SS
249static unsigned long kfifo_copy_to_user(struct __kfifo *fifo, void __user *to,
250 unsigned int len, unsigned int off, unsigned int *copied)
86d48803
SS
251{
252 unsigned int l;
2e956fb3
SS
253 unsigned long ret;
254 unsigned int size = fifo->mask + 1;
255 unsigned int esize = fifo->esize;
256
257 off &= fifo->mask;
258 if (esize != 1) {
259 off *= esize;
260 size *= esize;
261 len *= esize;
262 }
263 l = min(len, size - off);
264
265 ret = copy_to_user(to, fifo->data + off, l);
266 if (unlikely(ret))
267 ret = DIV_ROUND_UP(ret + len - l, esize);
268 else {
269 ret = copy_to_user(to + l, fifo->data, len - l);
270 if (unlikely(ret))
271 ret = DIV_ROUND_UP(ret, esize);
272 }
86d48803 273 /*
2e956fb3
SS
274 * make sure that the data is copied before
275 * incrementing the fifo->out index counter
86d48803 276 */
2e956fb3 277 smp_wmb();
a019e48c 278 *copied = len - ret * esize;
2e956fb3
SS
279 /* return the number of elements which are not copied */
280 return ret;
281}
86d48803 282
2e956fb3
SS
283int __kfifo_to_user(struct __kfifo *fifo, void __user *to,
284 unsigned long len, unsigned int *copied)
285{
286 unsigned int l;
287 unsigned long ret;
288 unsigned int esize = fifo->esize;
289 int err;
86d48803 290
2e956fb3
SS
291 if (esize != 1)
292 len /= esize;
86d48803 293
2e956fb3
SS
294 l = fifo->in - fifo->out;
295 if (len > l)
296 len = l;
297 ret = kfifo_copy_to_user(fifo, to, len, fifo->out, copied);
64ce1037 298 if (unlikely(ret)) {
2e956fb3
SS
299 len -= ret;
300 err = -EFAULT;
301 } else
302 err = 0;
303 fifo->out += len;
304 return err;
305}
306EXPORT_SYMBOL(__kfifo_to_user);
a121f24a 307
2e956fb3
SS
308static int setup_sgl_buf(struct scatterlist *sgl, void *buf,
309 int nents, unsigned int len)
310{
311 int n;
312 unsigned int l;
313 unsigned int off;
314 struct page *page;
315
316 if (!nents)
317 return 0;
318
319 if (!len)
320 return 0;
321
322 n = 0;
323 page = virt_to_page(buf);
324 off = offset_in_page(buf);
325 l = 0;
326
327 while (len >= l + PAGE_SIZE - off) {
328 struct page *npage;
329
330 l += PAGE_SIZE;
331 buf += PAGE_SIZE;
332 npage = virt_to_page(buf);
333 if (page_to_phys(page) != page_to_phys(npage) - l) {
d78a3eda
SS
334 sg_set_page(sgl, page, l - off, off);
335 sgl = sg_next(sgl);
336 if (++n == nents || sgl == NULL)
2e956fb3
SS
337 return n;
338 page = npage;
339 len -= l - off;
340 l = off = 0;
341 }
64ce1037 342 }
d78a3eda 343 sg_set_page(sgl, page, len, off);
2e956fb3 344 return n + 1;
86d48803 345}
1da177e4 346
2e956fb3
SS
347static unsigned int setup_sgl(struct __kfifo *fifo, struct scatterlist *sgl,
348 int nents, unsigned int len, unsigned int off)
86d48803 349{
2e956fb3
SS
350 unsigned int size = fifo->mask + 1;
351 unsigned int esize = fifo->esize;
352 unsigned int l;
353 unsigned int n;
86d48803 354
2e956fb3
SS
355 off &= fifo->mask;
356 if (esize != 1) {
357 off *= esize;
358 size *= esize;
359 len *= esize;
360 }
361 l = min(len, size - off);
362
d78a3eda 363 n = setup_sgl_buf(sgl, fifo->data + off, nents, l);
2e956fb3
SS
364 n += setup_sgl_buf(sgl + n, fifo->data, nents - n, len - l);
365
2e956fb3 366 return n;
86d48803 367}
86d48803 368
2e956fb3
SS
369unsigned int __kfifo_dma_in_prepare(struct __kfifo *fifo,
370 struct scatterlist *sgl, int nents, unsigned int len)
86d48803 371{
2e956fb3 372 unsigned int l;
86d48803 373
2e956fb3
SS
374 l = kfifo_unused(fifo);
375 if (len > l)
376 len = l;
377
378 return setup_sgl(fifo, sgl, nents, len, fifo->in);
1da177e4 379}
2e956fb3 380EXPORT_SYMBOL(__kfifo_dma_in_prepare);
86d48803 381
2e956fb3
SS
382unsigned int __kfifo_dma_out_prepare(struct __kfifo *fifo,
383 struct scatterlist *sgl, int nents, unsigned int len)
86d48803 384{
2e956fb3
SS
385 unsigned int l;
386
387 l = fifo->in - fifo->out;
388 if (len > l)
389 len = l;
390
391 return setup_sgl(fifo, sgl, nents, len, fifo->out);
86d48803 392}
2e956fb3 393EXPORT_SYMBOL(__kfifo_dma_out_prepare);
86d48803 394
2e956fb3 395unsigned int __kfifo_max_r(unsigned int len, size_t recsize)
86d48803 396{
2e956fb3 397 unsigned int max = (1 << (recsize << 3)) - 1;
86d48803 398
2e956fb3
SS
399 if (len > max)
400 return max;
401 return len;
86d48803 402}
30059d93 403EXPORT_SYMBOL(__kfifo_max_r);
a121f24a 404
2e956fb3
SS
405#define __KFIFO_PEEK(data, out, mask) \
406 ((data)[(out) & (mask)])
407/*
408 * __kfifo_peek_n internal helper function for determinate the length of
409 * the next record in the fifo
a121f24a 410 */
2e956fb3 411static unsigned int __kfifo_peek_n(struct __kfifo *fifo, size_t recsize)
a121f24a 412{
2e956fb3
SS
413 unsigned int l;
414 unsigned int mask = fifo->mask;
415 unsigned char *data = fifo->data;
a121f24a 416
2e956fb3 417 l = __KFIFO_PEEK(data, fifo->out, mask);
a121f24a 418
2e956fb3
SS
419 if (--recsize)
420 l |= __KFIFO_PEEK(data, fifo->out + 1, mask) << 8;
421
422 return l;
86d48803 423}
2e956fb3
SS
424
425#define __KFIFO_POKE(data, in, mask, val) \
426 ( \
427 (data)[(in) & (mask)] = (unsigned char)(val) \
428 )
429
430/*
431 * __kfifo_poke_n internal helper function for storeing the length of
432 * the record into the fifo
a5b9e2c1 433 */
2e956fb3 434static void __kfifo_poke_n(struct __kfifo *fifo, unsigned int n, size_t recsize)
a5b9e2c1 435{
2e956fb3
SS
436 unsigned int mask = fifo->mask;
437 unsigned char *data = fifo->data;
a5b9e2c1 438
2e956fb3
SS
439 __KFIFO_POKE(data, fifo->in, mask, n);
440
441 if (recsize > 1)
442 __KFIFO_POKE(data, fifo->in + 1, mask, n >> 8);
a5b9e2c1 443}
a5b9e2c1 444
2e956fb3 445unsigned int __kfifo_len_r(struct __kfifo *fifo, size_t recsize)
86d48803 446{
2e956fb3 447 return __kfifo_peek_n(fifo, recsize);
86d48803 448}
2e956fb3 449EXPORT_SYMBOL(__kfifo_len_r);
a121f24a 450
2e956fb3
SS
451unsigned int __kfifo_in_r(struct __kfifo *fifo, const void *buf,
452 unsigned int len, size_t recsize)
86d48803 453{
2e956fb3
SS
454 if (len + recsize > kfifo_unused(fifo))
455 return 0;
64ce1037 456
2e956fb3 457 __kfifo_poke_n(fifo, len, recsize);
a121f24a 458
2e956fb3
SS
459 kfifo_copy_in(fifo, buf, len, fifo->in + recsize);
460 fifo->in += len + recsize;
461 return len;
86d48803 462}
2e956fb3
SS
463EXPORT_SYMBOL(__kfifo_in_r);
464
465static unsigned int kfifo_out_copy_r(struct __kfifo *fifo,
466 void *buf, unsigned int len, size_t recsize, unsigned int *n)
86d48803 467{
2e956fb3
SS
468 *n = __kfifo_peek_n(fifo, recsize);
469
470 if (len > *n)
471 len = *n;
472
473 kfifo_copy_out(fifo, buf, len, fifo->out + recsize);
474 return len;
475}
476
477unsigned int __kfifo_out_peek_r(struct __kfifo *fifo, void *buf,
478 unsigned int len, size_t recsize)
479{
480 unsigned int n;
481
482 if (fifo->in == fifo->out)
483 return 0;
484
485 return kfifo_out_copy_r(fifo, buf, len, recsize, &n);
86d48803 486}
2e956fb3 487EXPORT_SYMBOL(__kfifo_out_peek_r);
a121f24a 488
2e956fb3
SS
489unsigned int __kfifo_out_r(struct __kfifo *fifo, void *buf,
490 unsigned int len, size_t recsize)
86d48803 491{
2e956fb3
SS
492 unsigned int n;
493
494 if (fifo->in == fifo->out)
495 return 0;
496
497 len = kfifo_out_copy_r(fifo, buf, len, recsize, &n);
498 fifo->out += n + recsize;
499 return len;
86d48803 500}
2e956fb3 501EXPORT_SYMBOL(__kfifo_out_r);
a121f24a 502
b35de43b
AR
503void __kfifo_skip_r(struct __kfifo *fifo, size_t recsize)
504{
505 unsigned int n;
506
507 n = __kfifo_peek_n(fifo, recsize);
508 fifo->out += n + recsize;
509}
510EXPORT_SYMBOL(__kfifo_skip_r);
511
2e956fb3
SS
512int __kfifo_from_user_r(struct __kfifo *fifo, const void __user *from,
513 unsigned long len, unsigned int *copied, size_t recsize)
86d48803 514{
2e956fb3 515 unsigned long ret;
a121f24a 516
2e956fb3 517 len = __kfifo_max_r(len, recsize);
a121f24a 518
2e956fb3
SS
519 if (len + recsize > kfifo_unused(fifo)) {
520 *copied = 0;
521 return 0;
522 }
86d48803 523
2e956fb3 524 __kfifo_poke_n(fifo, len, recsize);
86d48803 525
2e956fb3
SS
526 ret = kfifo_copy_from_user(fifo, from, len, fifo->in + recsize, copied);
527 if (unlikely(ret)) {
528 *copied = 0;
529 return -EFAULT;
530 }
531 fifo->in += len + recsize;
532 return 0;
86d48803 533}
2e956fb3
SS
534EXPORT_SYMBOL(__kfifo_from_user_r);
535
536int __kfifo_to_user_r(struct __kfifo *fifo, void __user *to,
537 unsigned long len, unsigned int *copied, size_t recsize)
86d48803 538{
2e956fb3
SS
539 unsigned long ret;
540 unsigned int n;
541
542 if (fifo->in == fifo->out) {
543 *copied = 0;
544 return 0;
545 }
546
547 n = __kfifo_peek_n(fifo, recsize);
548 if (len > n)
549 len = n;
550
551 ret = kfifo_copy_to_user(fifo, to, len, fifo->out + recsize, copied);
552 if (unlikely(ret)) {
553 *copied = 0;
554 return -EFAULT;
555 }
556 fifo->out += n + recsize;
557 return 0;
a121f24a 558}
2e956fb3 559EXPORT_SYMBOL(__kfifo_to_user_r);
a121f24a 560
2e956fb3
SS
561unsigned int __kfifo_dma_in_prepare_r(struct __kfifo *fifo,
562 struct scatterlist *sgl, int nents, unsigned int len, size_t recsize)
86d48803 563{
89b3ac63 564 BUG_ON(!nents);
2e956fb3
SS
565
566 len = __kfifo_max_r(len, recsize);
567
568 if (len + recsize > kfifo_unused(fifo))
569 return 0;
570
571 return setup_sgl(fifo, sgl, nents, len, fifo->in + recsize);
86d48803 572}
2e956fb3 573EXPORT_SYMBOL(__kfifo_dma_in_prepare_r);
86d48803 574
2e956fb3
SS
575void __kfifo_dma_in_finish_r(struct __kfifo *fifo,
576 unsigned int len, size_t recsize)
86d48803 577{
2e956fb3
SS
578 len = __kfifo_max_r(len, recsize);
579 __kfifo_poke_n(fifo, len, recsize);
580 fifo->in += len + recsize;
86d48803 581}
2e956fb3 582EXPORT_SYMBOL(__kfifo_dma_in_finish_r);
86d48803 583
2e956fb3
SS
584unsigned int __kfifo_dma_out_prepare_r(struct __kfifo *fifo,
585 struct scatterlist *sgl, int nents, unsigned int len, size_t recsize)
86d48803 586{
89b3ac63 587 BUG_ON(!nents);
2e956fb3
SS
588
589 len = __kfifo_max_r(len, recsize);
590
591 if (len + recsize > fifo->in - fifo->out)
592 return 0;
593
594 return setup_sgl(fifo, sgl, nents, len, fifo->out + recsize);
86d48803 595}
2e956fb3
SS
596EXPORT_SYMBOL(__kfifo_dma_out_prepare_r);
597
598void __kfifo_dma_out_finish_r(struct __kfifo *fifo, size_t recsize)
599{
600 unsigned int len;
86d48803 601
2e956fb3
SS
602 len = __kfifo_peek_n(fifo, recsize);
603 fifo->out += len + recsize;
604}
605EXPORT_SYMBOL(__kfifo_dma_out_finish_r);