proc: Add proc_mkdir_data()
[linux-2.6.git] / lib / scatterlist.c
blobb83c144d731f3707402be43e83d2041ab4df82b7
1 /*
2 * Copyright (C) 2007 Jens Axboe <jens.axboe@oracle.com>
4 * Scatterlist handling helpers.
6 * This source code is licensed under the GNU General Public License,
7 * Version 2. See the file COPYING for more details.
8 */
9 #include <linux/export.h>
10 #include <linux/slab.h>
11 #include <linux/scatterlist.h>
12 #include <linux/highmem.h>
13 #include <linux/kmemleak.h>
15 /**
16 * sg_next - return the next scatterlist entry in a list
17 * @sg: The current sg entry
19 * Description:
20 * Usually the next entry will be @sg@ + 1, but if this sg element is part
21 * of a chained scatterlist, it could jump to the start of a new
22 * scatterlist array.
24 **/
25 struct scatterlist *sg_next(struct scatterlist *sg)
27 #ifdef CONFIG_DEBUG_SG
28 BUG_ON(sg->sg_magic != SG_MAGIC);
29 #endif
30 if (sg_is_last(sg))
31 return NULL;
33 sg++;
34 if (unlikely(sg_is_chain(sg)))
35 sg = sg_chain_ptr(sg);
37 return sg;
39 EXPORT_SYMBOL(sg_next);
41 /**
42 * sg_nents - return total count of entries in scatterlist
43 * @sg: The scatterlist
45 * Description:
46 * Allows to know how many entries are in sg, taking into acount
47 * chaining as well
49 **/
50 int sg_nents(struct scatterlist *sg)
52 int nents;
53 for (nents = 0; sg; sg = sg_next(sg))
54 nents++;
55 return nents;
57 EXPORT_SYMBOL(sg_nents);
60 /**
61 * sg_last - return the last scatterlist entry in a list
62 * @sgl: First entry in the scatterlist
63 * @nents: Number of entries in the scatterlist
65 * Description:
66 * Should only be used casually, it (currently) scans the entire list
67 * to get the last entry.
69 * Note that the @sgl@ pointer passed in need not be the first one,
70 * the important bit is that @nents@ denotes the number of entries that
71 * exist from @sgl@.
73 **/
74 struct scatterlist *sg_last(struct scatterlist *sgl, unsigned int nents)
76 #ifndef ARCH_HAS_SG_CHAIN
77 struct scatterlist *ret = &sgl[nents - 1];
78 #else
79 struct scatterlist *sg, *ret = NULL;
80 unsigned int i;
82 for_each_sg(sgl, sg, nents, i)
83 ret = sg;
85 #endif
86 #ifdef CONFIG_DEBUG_SG
87 BUG_ON(sgl[0].sg_magic != SG_MAGIC);
88 BUG_ON(!sg_is_last(ret));
89 #endif
90 return ret;
92 EXPORT_SYMBOL(sg_last);
94 /**
95 * sg_init_table - Initialize SG table
96 * @sgl: The SG table
97 * @nents: Number of entries in table
99 * Notes:
100 * If this is part of a chained sg table, sg_mark_end() should be
101 * used only on the last table part.
104 void sg_init_table(struct scatterlist *sgl, unsigned int nents)
106 memset(sgl, 0, sizeof(*sgl) * nents);
107 #ifdef CONFIG_DEBUG_SG
109 unsigned int i;
110 for (i = 0; i < nents; i++)
111 sgl[i].sg_magic = SG_MAGIC;
113 #endif
114 sg_mark_end(&sgl[nents - 1]);
116 EXPORT_SYMBOL(sg_init_table);
119 * sg_init_one - Initialize a single entry sg list
120 * @sg: SG entry
121 * @buf: Virtual address for IO
122 * @buflen: IO length
125 void sg_init_one(struct scatterlist *sg, const void *buf, unsigned int buflen)
127 sg_init_table(sg, 1);
128 sg_set_buf(sg, buf, buflen);
130 EXPORT_SYMBOL(sg_init_one);
133 * The default behaviour of sg_alloc_table() is to use these kmalloc/kfree
134 * helpers.
136 static struct scatterlist *sg_kmalloc(unsigned int nents, gfp_t gfp_mask)
138 if (nents == SG_MAX_SINGLE_ALLOC) {
140 * Kmemleak doesn't track page allocations as they are not
141 * commonly used (in a raw form) for kernel data structures.
142 * As we chain together a list of pages and then a normal
143 * kmalloc (tracked by kmemleak), in order to for that last
144 * allocation not to become decoupled (and thus a
145 * false-positive) we need to inform kmemleak of all the
146 * intermediate allocations.
148 void *ptr = (void *) __get_free_page(gfp_mask);
149 kmemleak_alloc(ptr, PAGE_SIZE, 1, gfp_mask);
150 return ptr;
151 } else
152 return kmalloc(nents * sizeof(struct scatterlist), gfp_mask);
155 static void sg_kfree(struct scatterlist *sg, unsigned int nents)
157 if (nents == SG_MAX_SINGLE_ALLOC) {
158 kmemleak_free(sg);
159 free_page((unsigned long) sg);
160 } else
161 kfree(sg);
165 * __sg_free_table - Free a previously mapped sg table
166 * @table: The sg table header to use
167 * @max_ents: The maximum number of entries per single scatterlist
168 * @free_fn: Free function
170 * Description:
171 * Free an sg table previously allocated and setup with
172 * __sg_alloc_table(). The @max_ents value must be identical to
173 * that previously used with __sg_alloc_table().
176 void __sg_free_table(struct sg_table *table, unsigned int max_ents,
177 sg_free_fn *free_fn)
179 struct scatterlist *sgl, *next;
181 if (unlikely(!table->sgl))
182 return;
184 sgl = table->sgl;
185 while (table->orig_nents) {
186 unsigned int alloc_size = table->orig_nents;
187 unsigned int sg_size;
190 * If we have more than max_ents segments left,
191 * then assign 'next' to the sg table after the current one.
192 * sg_size is then one less than alloc size, since the last
193 * element is the chain pointer.
195 if (alloc_size > max_ents) {
196 next = sg_chain_ptr(&sgl[max_ents - 1]);
197 alloc_size = max_ents;
198 sg_size = alloc_size - 1;
199 } else {
200 sg_size = alloc_size;
201 next = NULL;
204 table->orig_nents -= sg_size;
205 free_fn(sgl, alloc_size);
206 sgl = next;
209 table->sgl = NULL;
211 EXPORT_SYMBOL(__sg_free_table);
214 * sg_free_table - Free a previously allocated sg table
215 * @table: The mapped sg table header
218 void sg_free_table(struct sg_table *table)
220 __sg_free_table(table, SG_MAX_SINGLE_ALLOC, sg_kfree);
222 EXPORT_SYMBOL(sg_free_table);
225 * __sg_alloc_table - Allocate and initialize an sg table with given allocator
226 * @table: The sg table header to use
227 * @nents: Number of entries in sg list
228 * @max_ents: The maximum number of entries the allocator returns per call
229 * @gfp_mask: GFP allocation mask
230 * @alloc_fn: Allocator to use
232 * Description:
233 * This function returns a @table @nents long. The allocator is
234 * defined to return scatterlist chunks of maximum size @max_ents.
235 * Thus if @nents is bigger than @max_ents, the scatterlists will be
236 * chained in units of @max_ents.
238 * Notes:
239 * If this function returns non-0 (eg failure), the caller must call
240 * __sg_free_table() to cleanup any leftover allocations.
243 int __sg_alloc_table(struct sg_table *table, unsigned int nents,
244 unsigned int max_ents, gfp_t gfp_mask,
245 sg_alloc_fn *alloc_fn)
247 struct scatterlist *sg, *prv;
248 unsigned int left;
250 #ifndef ARCH_HAS_SG_CHAIN
251 if (WARN_ON_ONCE(nents > max_ents))
252 return -EINVAL;
253 #endif
255 memset(table, 0, sizeof(*table));
257 left = nents;
258 prv = NULL;
259 do {
260 unsigned int sg_size, alloc_size = left;
262 if (alloc_size > max_ents) {
263 alloc_size = max_ents;
264 sg_size = alloc_size - 1;
265 } else
266 sg_size = alloc_size;
268 left -= sg_size;
270 sg = alloc_fn(alloc_size, gfp_mask);
271 if (unlikely(!sg)) {
273 * Adjust entry count to reflect that the last
274 * entry of the previous table won't be used for
275 * linkage. Without this, sg_kfree() may get
276 * confused.
278 if (prv)
279 table->nents = ++table->orig_nents;
281 return -ENOMEM;
284 sg_init_table(sg, alloc_size);
285 table->nents = table->orig_nents += sg_size;
288 * If this is the first mapping, assign the sg table header.
289 * If this is not the first mapping, chain previous part.
291 if (prv)
292 sg_chain(prv, max_ents, sg);
293 else
294 table->sgl = sg;
297 * If no more entries after this one, mark the end
299 if (!left)
300 sg_mark_end(&sg[sg_size - 1]);
302 prv = sg;
303 } while (left);
305 return 0;
307 EXPORT_SYMBOL(__sg_alloc_table);
310 * sg_alloc_table - Allocate and initialize an sg table
311 * @table: The sg table header to use
312 * @nents: Number of entries in sg list
313 * @gfp_mask: GFP allocation mask
315 * Description:
316 * Allocate and initialize an sg table. If @nents@ is larger than
317 * SG_MAX_SINGLE_ALLOC a chained sg table will be setup.
320 int sg_alloc_table(struct sg_table *table, unsigned int nents, gfp_t gfp_mask)
322 int ret;
324 ret = __sg_alloc_table(table, nents, SG_MAX_SINGLE_ALLOC,
325 gfp_mask, sg_kmalloc);
326 if (unlikely(ret))
327 __sg_free_table(table, SG_MAX_SINGLE_ALLOC, sg_kfree);
329 return ret;
331 EXPORT_SYMBOL(sg_alloc_table);
334 * sg_alloc_table_from_pages - Allocate and initialize an sg table from
335 * an array of pages
336 * @sgt: The sg table header to use
337 * @pages: Pointer to an array of page pointers
338 * @n_pages: Number of pages in the pages array
339 * @offset: Offset from start of the first page to the start of a buffer
340 * @size: Number of valid bytes in the buffer (after offset)
341 * @gfp_mask: GFP allocation mask
343 * Description:
344 * Allocate and initialize an sg table from a list of pages. Contiguous
345 * ranges of the pages are squashed into a single scatterlist node. A user
346 * may provide an offset at a start and a size of valid data in a buffer
347 * specified by the page array. The returned sg table is released by
348 * sg_free_table.
350 * Returns:
351 * 0 on success, negative error on failure
353 int sg_alloc_table_from_pages(struct sg_table *sgt,
354 struct page **pages, unsigned int n_pages,
355 unsigned long offset, unsigned long size,
356 gfp_t gfp_mask)
358 unsigned int chunks;
359 unsigned int i;
360 unsigned int cur_page;
361 int ret;
362 struct scatterlist *s;
364 /* compute number of contiguous chunks */
365 chunks = 1;
366 for (i = 1; i < n_pages; ++i)
367 if (page_to_pfn(pages[i]) != page_to_pfn(pages[i - 1]) + 1)
368 ++chunks;
370 ret = sg_alloc_table(sgt, chunks, gfp_mask);
371 if (unlikely(ret))
372 return ret;
374 /* merging chunks and putting them into the scatterlist */
375 cur_page = 0;
376 for_each_sg(sgt->sgl, s, sgt->orig_nents, i) {
377 unsigned long chunk_size;
378 unsigned int j;
380 /* look for the end of the current chunk */
381 for (j = cur_page + 1; j < n_pages; ++j)
382 if (page_to_pfn(pages[j]) !=
383 page_to_pfn(pages[j - 1]) + 1)
384 break;
386 chunk_size = ((j - cur_page) << PAGE_SHIFT) - offset;
387 sg_set_page(s, pages[cur_page], min(size, chunk_size), offset);
388 size -= chunk_size;
389 offset = 0;
390 cur_page = j;
393 return 0;
395 EXPORT_SYMBOL(sg_alloc_table_from_pages);
397 void __sg_page_iter_start(struct sg_page_iter *piter,
398 struct scatterlist *sglist, unsigned int nents,
399 unsigned long pgoffset)
401 piter->__pg_advance = 0;
402 piter->__nents = nents;
404 piter->page = NULL;
405 piter->sg = sglist;
406 piter->sg_pgoffset = pgoffset;
408 EXPORT_SYMBOL(__sg_page_iter_start);
410 static int sg_page_count(struct scatterlist *sg)
412 return PAGE_ALIGN(sg->offset + sg->length) >> PAGE_SHIFT;
415 bool __sg_page_iter_next(struct sg_page_iter *piter)
417 if (!piter->__nents || !piter->sg)
418 return false;
420 piter->sg_pgoffset += piter->__pg_advance;
421 piter->__pg_advance = 1;
423 while (piter->sg_pgoffset >= sg_page_count(piter->sg)) {
424 piter->sg_pgoffset -= sg_page_count(piter->sg);
425 piter->sg = sg_next(piter->sg);
426 if (!--piter->__nents || !piter->sg)
427 return false;
429 piter->page = nth_page(sg_page(piter->sg), piter->sg_pgoffset);
431 return true;
433 EXPORT_SYMBOL(__sg_page_iter_next);
436 * sg_miter_start - start mapping iteration over a sg list
437 * @miter: sg mapping iter to be started
438 * @sgl: sg list to iterate over
439 * @nents: number of sg entries
441 * Description:
442 * Starts mapping iterator @miter.
444 * Context:
445 * Don't care.
447 void sg_miter_start(struct sg_mapping_iter *miter, struct scatterlist *sgl,
448 unsigned int nents, unsigned int flags)
450 memset(miter, 0, sizeof(struct sg_mapping_iter));
452 __sg_page_iter_start(&miter->piter, sgl, nents, 0);
453 WARN_ON(!(flags & (SG_MITER_TO_SG | SG_MITER_FROM_SG)));
454 miter->__flags = flags;
456 EXPORT_SYMBOL(sg_miter_start);
459 * sg_miter_next - proceed mapping iterator to the next mapping
460 * @miter: sg mapping iter to proceed
462 * Description:
463 * Proceeds @miter to the next mapping. @miter should have been started
464 * using sg_miter_start(). On successful return, @miter->page,
465 * @miter->addr and @miter->length point to the current mapping.
467 * Context:
468 * Preemption disabled if SG_MITER_ATOMIC. Preemption must stay disabled
469 * till @miter is stopped. May sleep if !SG_MITER_ATOMIC.
471 * Returns:
472 * true if @miter contains the next mapping. false if end of sg
473 * list is reached.
475 bool sg_miter_next(struct sg_mapping_iter *miter)
477 sg_miter_stop(miter);
480 * Get to the next page if necessary.
481 * __remaining, __offset is adjusted by sg_miter_stop
483 if (!miter->__remaining) {
484 struct scatterlist *sg;
485 unsigned long pgoffset;
487 if (!__sg_page_iter_next(&miter->piter))
488 return false;
490 sg = miter->piter.sg;
491 pgoffset = miter->piter.sg_pgoffset;
493 miter->__offset = pgoffset ? 0 : sg->offset;
494 miter->__remaining = sg->offset + sg->length -
495 (pgoffset << PAGE_SHIFT) - miter->__offset;
496 miter->__remaining = min_t(unsigned long, miter->__remaining,
497 PAGE_SIZE - miter->__offset);
499 miter->page = miter->piter.page;
500 miter->consumed = miter->length = miter->__remaining;
502 if (miter->__flags & SG_MITER_ATOMIC)
503 miter->addr = kmap_atomic(miter->page) + miter->__offset;
504 else
505 miter->addr = kmap(miter->page) + miter->__offset;
507 return true;
509 EXPORT_SYMBOL(sg_miter_next);
512 * sg_miter_stop - stop mapping iteration
513 * @miter: sg mapping iter to be stopped
515 * Description:
516 * Stops mapping iterator @miter. @miter should have been started
517 * started using sg_miter_start(). A stopped iteration can be
518 * resumed by calling sg_miter_next() on it. This is useful when
519 * resources (kmap) need to be released during iteration.
521 * Context:
522 * Preemption disabled if the SG_MITER_ATOMIC is set. Don't care
523 * otherwise.
525 void sg_miter_stop(struct sg_mapping_iter *miter)
527 WARN_ON(miter->consumed > miter->length);
529 /* drop resources from the last iteration */
530 if (miter->addr) {
531 miter->__offset += miter->consumed;
532 miter->__remaining -= miter->consumed;
534 if (miter->__flags & SG_MITER_TO_SG)
535 flush_kernel_dcache_page(miter->page);
537 if (miter->__flags & SG_MITER_ATOMIC) {
538 WARN_ON_ONCE(preemptible());
539 kunmap_atomic(miter->addr);
540 } else
541 kunmap(miter->page);
543 miter->page = NULL;
544 miter->addr = NULL;
545 miter->length = 0;
546 miter->consumed = 0;
549 EXPORT_SYMBOL(sg_miter_stop);
552 * sg_copy_buffer - Copy data between a linear buffer and an SG list
553 * @sgl: The SG list
554 * @nents: Number of SG entries
555 * @buf: Where to copy from
556 * @buflen: The number of bytes to copy
557 * @to_buffer: transfer direction (non zero == from an sg list to a
558 * buffer, 0 == from a buffer to an sg list
560 * Returns the number of copied bytes.
563 static size_t sg_copy_buffer(struct scatterlist *sgl, unsigned int nents,
564 void *buf, size_t buflen, int to_buffer)
566 unsigned int offset = 0;
567 struct sg_mapping_iter miter;
568 unsigned long flags;
569 unsigned int sg_flags = SG_MITER_ATOMIC;
571 if (to_buffer)
572 sg_flags |= SG_MITER_FROM_SG;
573 else
574 sg_flags |= SG_MITER_TO_SG;
576 sg_miter_start(&miter, sgl, nents, sg_flags);
578 local_irq_save(flags);
580 while (sg_miter_next(&miter) && offset < buflen) {
581 unsigned int len;
583 len = min(miter.length, buflen - offset);
585 if (to_buffer)
586 memcpy(buf + offset, miter.addr, len);
587 else
588 memcpy(miter.addr, buf + offset, len);
590 offset += len;
593 sg_miter_stop(&miter);
595 local_irq_restore(flags);
596 return offset;
600 * sg_copy_from_buffer - Copy from a linear buffer to an SG list
601 * @sgl: The SG list
602 * @nents: Number of SG entries
603 * @buf: Where to copy from
604 * @buflen: The number of bytes to copy
606 * Returns the number of copied bytes.
609 size_t sg_copy_from_buffer(struct scatterlist *sgl, unsigned int nents,
610 void *buf, size_t buflen)
612 return sg_copy_buffer(sgl, nents, buf, buflen, 0);
614 EXPORT_SYMBOL(sg_copy_from_buffer);
617 * sg_copy_to_buffer - Copy from an SG list to a linear buffer
618 * @sgl: The SG list
619 * @nents: Number of SG entries
620 * @buf: Where to copy to
621 * @buflen: The number of bytes to copy
623 * Returns the number of copied bytes.
626 size_t sg_copy_to_buffer(struct scatterlist *sgl, unsigned int nents,
627 void *buf, size_t buflen)
629 return sg_copy_buffer(sgl, nents, buf, buflen, 1);
631 EXPORT_SYMBOL(sg_copy_to_buffer);