scatterlist.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758
  1. /*
  2. * Copyright (C) 2007 Jens Axboe <jens.axboe@oracle.com>
  3. *
  4. * Scatterlist handling helpers.
  5. *
  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>
  14. /**
  15. * sg_next - return the next scatterlist entry in a list
  16. * @sg: The current sg entry
  17. *
  18. * Description:
  19. * Usually the next entry will be @sg@ + 1, but if this sg element is part
  20. * of a chained scatterlist, it could jump to the start of a new
  21. * scatterlist array.
  22. *
  23. **/
  24. struct scatterlist *sg_next(struct scatterlist *sg)
  25. {
  26. #ifdef CONFIG_DEBUG_SG
  27. BUG_ON(sg->sg_magic != SG_MAGIC);
  28. #endif
  29. if (sg_is_last(sg))
  30. return NULL;
  31. sg++;
  32. if (unlikely(sg_is_chain(sg)))
  33. sg = sg_chain_ptr(sg);
  34. return sg;
  35. }
  36. EXPORT_SYMBOL(sg_next);
  37. /**
  38. * sg_nents - return total count of entries in scatterlist
  39. * @sg: The scatterlist
  40. *
  41. * Description:
  42. * Allows to know how many entries are in sg, taking into acount
  43. * chaining as well
  44. *
  45. **/
  46. int sg_nents(struct scatterlist *sg)
  47. {
  48. int nents;
  49. for (nents = 0; sg; sg = sg_next(sg))
  50. nents++;
  51. return nents;
  52. }
  53. EXPORT_SYMBOL(sg_nents);
  54. /**
  55. * sg_nents_for_len - return total count of entries in scatterlist
  56. * needed to satisfy the supplied length
  57. * @sg: The scatterlist
  58. * @len: The total required length
  59. *
  60. * Description:
  61. * Determines the number of entries in sg that are required to meet
  62. * the supplied length, taking into acount chaining as well
  63. *
  64. * Returns:
  65. * the number of sg entries needed, negative error on failure
  66. *
  67. **/
  68. int sg_nents_for_len(struct scatterlist *sg, u64 len)
  69. {
  70. int nents;
  71. u64 total;
  72. if (!len)
  73. return 0;
  74. for (nents = 0, total = 0; sg; sg = sg_next(sg)) {
  75. nents++;
  76. total += sg->length;
  77. if (total >= len)
  78. return nents;
  79. }
  80. return -EINVAL;
  81. }
  82. EXPORT_SYMBOL(sg_nents_for_len);
  83. /**
  84. * sg_last - return the last scatterlist entry in a list
  85. * @sgl: First entry in the scatterlist
  86. * @nents: Number of entries in the scatterlist
  87. *
  88. * Description:
  89. * Should only be used casually, it (currently) scans the entire list
  90. * to get the last entry.
  91. *
  92. * Note that the @sgl@ pointer passed in need not be the first one,
  93. * the important bit is that @nents@ denotes the number of entries that
  94. * exist from @sgl@.
  95. *
  96. **/
  97. struct scatterlist *sg_last(struct scatterlist *sgl, unsigned int nents)
  98. {
  99. struct scatterlist *sg, *ret = NULL;
  100. unsigned int i;
  101. for_each_sg(sgl, sg, nents, i)
  102. ret = sg;
  103. #ifdef CONFIG_DEBUG_SG
  104. BUG_ON(sgl[0].sg_magic != SG_MAGIC);
  105. BUG_ON(!sg_is_last(ret));
  106. #endif
  107. return ret;
  108. }
  109. EXPORT_SYMBOL(sg_last);
  110. /**
  111. * sg_init_table - Initialize SG table
  112. * @sgl: The SG table
  113. * @nents: Number of entries in table
  114. *
  115. * Notes:
  116. * If this is part of a chained sg table, sg_mark_end() should be
  117. * used only on the last table part.
  118. *
  119. **/
  120. void sg_init_table(struct scatterlist *sgl, unsigned int nents)
  121. {
  122. memset(sgl, 0, sizeof(*sgl) * nents);
  123. #ifdef CONFIG_DEBUG_SG
  124. {
  125. unsigned int i;
  126. for (i = 0; i < nents; i++)
  127. sgl[i].sg_magic = SG_MAGIC;
  128. }
  129. #endif
  130. sg_mark_end(&sgl[nents - 1]);
  131. }
  132. EXPORT_SYMBOL(sg_init_table);
  133. /**
  134. * sg_init_one - Initialize a single entry sg list
  135. * @sg: SG entry
  136. * @buf: Virtual address for IO
  137. * @buflen: IO length
  138. *
  139. **/
  140. void sg_init_one(struct scatterlist *sg, const void *buf, unsigned int buflen)
  141. {
  142. sg_init_table(sg, 1);
  143. sg_set_buf(sg, buf, buflen);
  144. }
  145. EXPORT_SYMBOL(sg_init_one);
  146. /*
  147. * The default behaviour of sg_alloc_table() is to use these kmalloc/kfree
  148. * helpers.
  149. */
  150. static struct scatterlist *sg_kmalloc(unsigned int nents, gfp_t gfp_mask)
  151. {
  152. if (nents == SG_MAX_SINGLE_ALLOC) {
  153. /*
  154. * Kmemleak doesn't track page allocations as they are not
  155. * commonly used (in a raw form) for kernel data structures.
  156. * As we chain together a list of pages and then a normal
  157. * kmalloc (tracked by kmemleak), in order to for that last
  158. * allocation not to become decoupled (and thus a
  159. * false-positive) we need to inform kmemleak of all the
  160. * intermediate allocations.
  161. */
  162. void *ptr = (void *) __get_free_page(gfp_mask);
  163. kmemleak_alloc(ptr, PAGE_SIZE, 1, gfp_mask);
  164. return ptr;
  165. } else
  166. return kmalloc(nents * sizeof(struct scatterlist), gfp_mask);
  167. }
  168. static void sg_kfree(struct scatterlist *sg, unsigned int nents)
  169. {
  170. if (nents == SG_MAX_SINGLE_ALLOC) {
  171. kmemleak_free(sg);
  172. free_page((unsigned long) sg);
  173. } else
  174. kfree(sg);
  175. }
  176. /**
  177. * __sg_free_table - Free a previously mapped sg table
  178. * @table: The sg table header to use
  179. * @max_ents: The maximum number of entries per single scatterlist
  180. * @skip_first_chunk: don't free the (preallocated) first scatterlist chunk
  181. * @free_fn: Free function
  182. *
  183. * Description:
  184. * Free an sg table previously allocated and setup with
  185. * __sg_alloc_table(). The @max_ents value must be identical to
  186. * that previously used with __sg_alloc_table().
  187. *
  188. **/
  189. void __sg_free_table(struct sg_table *table, unsigned int max_ents,
  190. bool skip_first_chunk, sg_free_fn *free_fn)
  191. {
  192. struct scatterlist *sgl, *next;
  193. if (unlikely(!table->sgl))
  194. return;
  195. sgl = table->sgl;
  196. while (table->orig_nents) {
  197. unsigned int alloc_size = table->orig_nents;
  198. unsigned int sg_size;
  199. /*
  200. * If we have more than max_ents segments left,
  201. * then assign 'next' to the sg table after the current one.
  202. * sg_size is then one less than alloc size, since the last
  203. * element is the chain pointer.
  204. */
  205. if (alloc_size > max_ents) {
  206. next = sg_chain_ptr(&sgl[max_ents - 1]);
  207. alloc_size = max_ents;
  208. sg_size = alloc_size - 1;
  209. } else {
  210. sg_size = alloc_size;
  211. next = NULL;
  212. }
  213. table->orig_nents -= sg_size;
  214. if (skip_first_chunk)
  215. skip_first_chunk = false;
  216. else
  217. free_fn(sgl, alloc_size);
  218. sgl = next;
  219. }
  220. table->sgl = NULL;
  221. }
  222. EXPORT_SYMBOL(__sg_free_table);
  223. /**
  224. * sg_free_table - Free a previously allocated sg table
  225. * @table: The mapped sg table header
  226. *
  227. **/
  228. void sg_free_table(struct sg_table *table)
  229. {
  230. __sg_free_table(table, SG_MAX_SINGLE_ALLOC, false, sg_kfree);
  231. }
  232. EXPORT_SYMBOL(sg_free_table);
  233. /**
  234. * __sg_alloc_table - Allocate and initialize an sg table with given allocator
  235. * @table: The sg table header to use
  236. * @nents: Number of entries in sg list
  237. * @max_ents: The maximum number of entries the allocator returns per call
  238. * @gfp_mask: GFP allocation mask
  239. * @alloc_fn: Allocator to use
  240. *
  241. * Description:
  242. * This function returns a @table @nents long. The allocator is
  243. * defined to return scatterlist chunks of maximum size @max_ents.
  244. * Thus if @nents is bigger than @max_ents, the scatterlists will be
  245. * chained in units of @max_ents.
  246. *
  247. * Notes:
  248. * If this function returns non-0 (eg failure), the caller must call
  249. * __sg_free_table() to cleanup any leftover allocations.
  250. *
  251. **/
  252. int __sg_alloc_table(struct sg_table *table, unsigned int nents,
  253. unsigned int max_ents, struct scatterlist *first_chunk,
  254. gfp_t gfp_mask, sg_alloc_fn *alloc_fn)
  255. {
  256. struct scatterlist *sg, *prv;
  257. unsigned int left;
  258. memset(table, 0, sizeof(*table));
  259. if (nents == 0)
  260. return -EINVAL;
  261. #ifndef CONFIG_ARCH_HAS_SG_CHAIN
  262. if (WARN_ON_ONCE(nents > max_ents))
  263. return -EINVAL;
  264. #endif
  265. left = nents;
  266. prv = NULL;
  267. do {
  268. unsigned int sg_size, alloc_size = left;
  269. if (alloc_size > max_ents) {
  270. alloc_size = max_ents;
  271. sg_size = alloc_size - 1;
  272. } else
  273. sg_size = alloc_size;
  274. left -= sg_size;
  275. if (first_chunk) {
  276. sg = first_chunk;
  277. first_chunk = NULL;
  278. } else {
  279. sg = alloc_fn(alloc_size, gfp_mask);
  280. }
  281. if (unlikely(!sg)) {
  282. /*
  283. * Adjust entry count to reflect that the last
  284. * entry of the previous table won't be used for
  285. * linkage. Without this, sg_kfree() may get
  286. * confused.
  287. */
  288. if (prv)
  289. table->nents = ++table->orig_nents;
  290. return -ENOMEM;
  291. }
  292. sg_init_table(sg, alloc_size);
  293. table->nents = table->orig_nents += sg_size;
  294. /*
  295. * If this is the first mapping, assign the sg table header.
  296. * If this is not the first mapping, chain previous part.
  297. */
  298. if (prv)
  299. sg_chain(prv, max_ents, sg);
  300. else
  301. table->sgl = sg;
  302. /*
  303. * If no more entries after this one, mark the end
  304. */
  305. if (!left)
  306. sg_mark_end(&sg[sg_size - 1]);
  307. prv = sg;
  308. } while (left);
  309. return 0;
  310. }
  311. EXPORT_SYMBOL(__sg_alloc_table);
  312. /**
  313. * sg_alloc_table - Allocate and initialize an sg table
  314. * @table: The sg table header to use
  315. * @nents: Number of entries in sg list
  316. * @gfp_mask: GFP allocation mask
  317. *
  318. * Description:
  319. * Allocate and initialize an sg table. If @nents@ is larger than
  320. * SG_MAX_SINGLE_ALLOC a chained sg table will be setup.
  321. *
  322. **/
  323. int sg_alloc_table(struct sg_table *table, unsigned int nents, gfp_t gfp_mask)
  324. {
  325. int ret;
  326. ret = __sg_alloc_table(table, nents, SG_MAX_SINGLE_ALLOC,
  327. NULL, gfp_mask, sg_kmalloc);
  328. if (unlikely(ret))
  329. __sg_free_table(table, SG_MAX_SINGLE_ALLOC, false, sg_kfree);
  330. return ret;
  331. }
  332. EXPORT_SYMBOL(sg_alloc_table);
  333. /**
  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
  342. *
  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.
  349. *
  350. * Returns:
  351. * 0 on success, negative error on failure
  352. */
  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)
  357. {
  358. unsigned int chunks;
  359. unsigned int i;
  360. unsigned int cur_page;
  361. int ret;
  362. struct scatterlist *s;
  363. /* compute number of contiguous chunks */
  364. chunks = 1;
  365. for (i = 1; i < n_pages; ++i)
  366. if (page_to_pfn(pages[i]) != page_to_pfn(pages[i - 1]) + 1)
  367. ++chunks;
  368. ret = sg_alloc_table(sgt, chunks, gfp_mask);
  369. if (unlikely(ret))
  370. return ret;
  371. /* merging chunks and putting them into the scatterlist */
  372. cur_page = 0;
  373. for_each_sg(sgt->sgl, s, sgt->orig_nents, i) {
  374. unsigned long chunk_size;
  375. unsigned int j;
  376. /* look for the end of the current chunk */
  377. for (j = cur_page + 1; j < n_pages; ++j)
  378. if (page_to_pfn(pages[j]) !=
  379. page_to_pfn(pages[j - 1]) + 1)
  380. break;
  381. chunk_size = ((j - cur_page) << PAGE_SHIFT) - offset;
  382. sg_set_page(s, pages[cur_page], min(size, chunk_size), offset);
  383. size -= chunk_size;
  384. offset = 0;
  385. cur_page = j;
  386. }
  387. return 0;
  388. }
  389. EXPORT_SYMBOL(sg_alloc_table_from_pages);
  390. void __sg_page_iter_start(struct sg_page_iter *piter,
  391. struct scatterlist *sglist, unsigned int nents,
  392. unsigned long pgoffset)
  393. {
  394. piter->__pg_advance = 0;
  395. piter->__nents = nents;
  396. piter->sg = sglist;
  397. piter->sg_pgoffset = pgoffset;
  398. }
  399. EXPORT_SYMBOL(__sg_page_iter_start);
  400. static int sg_page_count(struct scatterlist *sg)
  401. {
  402. return PAGE_ALIGN(sg->offset + sg->length) >> PAGE_SHIFT;
  403. }
  404. bool __sg_page_iter_next(struct sg_page_iter *piter)
  405. {
  406. if (!piter->__nents || !piter->sg)
  407. return false;
  408. piter->sg_pgoffset += piter->__pg_advance;
  409. piter->__pg_advance = 1;
  410. while (piter->sg_pgoffset >= sg_page_count(piter->sg)) {
  411. piter->sg_pgoffset -= sg_page_count(piter->sg);
  412. piter->sg = sg_next(piter->sg);
  413. if (!--piter->__nents || !piter->sg)
  414. return false;
  415. }
  416. return true;
  417. }
  418. EXPORT_SYMBOL(__sg_page_iter_next);
  419. /**
  420. * sg_miter_start - start mapping iteration over a sg list
  421. * @miter: sg mapping iter to be started
  422. * @sgl: sg list to iterate over
  423. * @nents: number of sg entries
  424. *
  425. * Description:
  426. * Starts mapping iterator @miter.
  427. *
  428. * Context:
  429. * Don't care.
  430. */
  431. void sg_miter_start(struct sg_mapping_iter *miter, struct scatterlist *sgl,
  432. unsigned int nents, unsigned int flags)
  433. {
  434. memset(miter, 0, sizeof(struct sg_mapping_iter));
  435. __sg_page_iter_start(&miter->piter, sgl, nents, 0);
  436. WARN_ON(!(flags & (SG_MITER_TO_SG | SG_MITER_FROM_SG)));
  437. miter->__flags = flags;
  438. }
  439. EXPORT_SYMBOL(sg_miter_start);
  440. static bool sg_miter_get_next_page(struct sg_mapping_iter *miter)
  441. {
  442. if (!miter->__remaining) {
  443. struct scatterlist *sg;
  444. unsigned long pgoffset;
  445. if (!__sg_page_iter_next(&miter->piter))
  446. return false;
  447. sg = miter->piter.sg;
  448. pgoffset = miter->piter.sg_pgoffset;
  449. miter->__offset = pgoffset ? 0 : sg->offset;
  450. miter->__remaining = sg->offset + sg->length -
  451. (pgoffset << PAGE_SHIFT) - miter->__offset;
  452. miter->__remaining = min_t(unsigned long, miter->__remaining,
  453. PAGE_SIZE - miter->__offset);
  454. }
  455. return true;
  456. }
  457. /**
  458. * sg_miter_skip - reposition mapping iterator
  459. * @miter: sg mapping iter to be skipped
  460. * @offset: number of bytes to plus the current location
  461. *
  462. * Description:
  463. * Sets the offset of @miter to its current location plus @offset bytes.
  464. * If mapping iterator @miter has been proceeded by sg_miter_next(), this
  465. * stops @miter.
  466. *
  467. * Context:
  468. * Don't care if @miter is stopped, or not proceeded yet.
  469. * Otherwise, preemption disabled if the SG_MITER_ATOMIC is set.
  470. *
  471. * Returns:
  472. * true if @miter contains the valid mapping. false if end of sg
  473. * list is reached.
  474. */
  475. bool sg_miter_skip(struct sg_mapping_iter *miter, off_t offset)
  476. {
  477. sg_miter_stop(miter);
  478. while (offset) {
  479. off_t consumed;
  480. if (!sg_miter_get_next_page(miter))
  481. return false;
  482. consumed = min_t(off_t, offset, miter->__remaining);
  483. miter->__offset += consumed;
  484. miter->__remaining -= consumed;
  485. offset -= consumed;
  486. }
  487. return true;
  488. }
  489. EXPORT_SYMBOL(sg_miter_skip);
  490. /**
  491. * sg_miter_next - proceed mapping iterator to the next mapping
  492. * @miter: sg mapping iter to proceed
  493. *
  494. * Description:
  495. * Proceeds @miter to the next mapping. @miter should have been started
  496. * using sg_miter_start(). On successful return, @miter->page,
  497. * @miter->addr and @miter->length point to the current mapping.
  498. *
  499. * Context:
  500. * Preemption disabled if SG_MITER_ATOMIC. Preemption must stay disabled
  501. * till @miter is stopped. May sleep if !SG_MITER_ATOMIC.
  502. *
  503. * Returns:
  504. * true if @miter contains the next mapping. false if end of sg
  505. * list is reached.
  506. */
  507. bool sg_miter_next(struct sg_mapping_iter *miter)
  508. {
  509. sg_miter_stop(miter);
  510. /*
  511. * Get to the next page if necessary.
  512. * __remaining, __offset is adjusted by sg_miter_stop
  513. */
  514. if (!sg_miter_get_next_page(miter))
  515. return false;
  516. miter->page = sg_page_iter_page(&miter->piter);
  517. miter->consumed = miter->length = miter->__remaining;
  518. if (miter->__flags & SG_MITER_ATOMIC)
  519. miter->addr = kmap_atomic(miter->page) + miter->__offset;
  520. else
  521. miter->addr = kmap(miter->page) + miter->__offset;
  522. return true;
  523. }
  524. EXPORT_SYMBOL(sg_miter_next);
  525. /**
  526. * sg_miter_stop - stop mapping iteration
  527. * @miter: sg mapping iter to be stopped
  528. *
  529. * Description:
  530. * Stops mapping iterator @miter. @miter should have been started
  531. * using sg_miter_start(). A stopped iteration can be resumed by
  532. * calling sg_miter_next() on it. This is useful when resources (kmap)
  533. * need to be released during iteration.
  534. *
  535. * Context:
  536. * Preemption disabled if the SG_MITER_ATOMIC is set. Don't care
  537. * otherwise.
  538. */
  539. void sg_miter_stop(struct sg_mapping_iter *miter)
  540. {
  541. WARN_ON(miter->consumed > miter->length);
  542. /* drop resources from the last iteration */
  543. if (miter->addr) {
  544. miter->__offset += miter->consumed;
  545. miter->__remaining -= miter->consumed;
  546. if ((miter->__flags & SG_MITER_TO_SG) &&
  547. !PageSlab(miter->page))
  548. flush_kernel_dcache_page(miter->page);
  549. if (miter->__flags & SG_MITER_ATOMIC) {
  550. WARN_ON_ONCE(preemptible());
  551. kunmap_atomic(miter->addr);
  552. } else
  553. kunmap(miter->page);
  554. miter->page = NULL;
  555. miter->addr = NULL;
  556. miter->length = 0;
  557. miter->consumed = 0;
  558. }
  559. }
  560. EXPORT_SYMBOL(sg_miter_stop);
  561. /**
  562. * sg_copy_buffer - Copy data between a linear buffer and an SG list
  563. * @sgl: The SG list
  564. * @nents: Number of SG entries
  565. * @buf: Where to copy from
  566. * @buflen: The number of bytes to copy
  567. * @skip: Number of bytes to skip before copying
  568. * @to_buffer: transfer direction (true == from an sg list to a
  569. * buffer, false == from a buffer to an sg list
  570. *
  571. * Returns the number of copied bytes.
  572. *
  573. **/
  574. size_t sg_copy_buffer(struct scatterlist *sgl, unsigned int nents, void *buf,
  575. size_t buflen, off_t skip, bool to_buffer)
  576. {
  577. unsigned int offset = 0;
  578. struct sg_mapping_iter miter;
  579. unsigned long flags;
  580. unsigned int sg_flags = SG_MITER_ATOMIC;
  581. if (to_buffer)
  582. sg_flags |= SG_MITER_FROM_SG;
  583. else
  584. sg_flags |= SG_MITER_TO_SG;
  585. sg_miter_start(&miter, sgl, nents, sg_flags);
  586. if (!sg_miter_skip(&miter, skip))
  587. return false;
  588. local_irq_save(flags);
  589. while (sg_miter_next(&miter) && offset < buflen) {
  590. unsigned int len;
  591. len = min(miter.length, buflen - offset);
  592. if (to_buffer)
  593. memcpy(buf + offset, miter.addr, len);
  594. else
  595. memcpy(miter.addr, buf + offset, len);
  596. offset += len;
  597. }
  598. sg_miter_stop(&miter);
  599. local_irq_restore(flags);
  600. return offset;
  601. }
  602. EXPORT_SYMBOL(sg_copy_buffer);
  603. /**
  604. * sg_copy_from_buffer - Copy from a linear buffer to an SG list
  605. * @sgl: The SG list
  606. * @nents: Number of SG entries
  607. * @buf: Where to copy from
  608. * @buflen: The number of bytes to copy
  609. *
  610. * Returns the number of copied bytes.
  611. *
  612. **/
  613. size_t sg_copy_from_buffer(struct scatterlist *sgl, unsigned int nents,
  614. const void *buf, size_t buflen)
  615. {
  616. return sg_copy_buffer(sgl, nents, (void *)buf, buflen, 0, false);
  617. }
  618. EXPORT_SYMBOL(sg_copy_from_buffer);
  619. /**
  620. * sg_copy_to_buffer - Copy from an SG list to a linear buffer
  621. * @sgl: The SG list
  622. * @nents: Number of SG entries
  623. * @buf: Where to copy to
  624. * @buflen: The number of bytes to copy
  625. *
  626. * Returns the number of copied bytes.
  627. *
  628. **/
  629. size_t sg_copy_to_buffer(struct scatterlist *sgl, unsigned int nents,
  630. void *buf, size_t buflen)
  631. {
  632. return sg_copy_buffer(sgl, nents, buf, buflen, 0, true);
  633. }
  634. EXPORT_SYMBOL(sg_copy_to_buffer);
  635. /**
  636. * sg_pcopy_from_buffer - Copy from a linear buffer to an SG list
  637. * @sgl: The SG list
  638. * @nents: Number of SG entries
  639. * @buf: Where to copy from
  640. * @buflen: The number of bytes to copy
  641. * @skip: Number of bytes to skip before copying
  642. *
  643. * Returns the number of copied bytes.
  644. *
  645. **/
  646. size_t sg_pcopy_from_buffer(struct scatterlist *sgl, unsigned int nents,
  647. const void *buf, size_t buflen, off_t skip)
  648. {
  649. return sg_copy_buffer(sgl, nents, (void *)buf, buflen, skip, false);
  650. }
  651. EXPORT_SYMBOL(sg_pcopy_from_buffer);
  652. /**
  653. * sg_pcopy_to_buffer - Copy from an SG list to a linear buffer
  654. * @sgl: The SG list
  655. * @nents: Number of SG entries
  656. * @buf: Where to copy to
  657. * @buflen: The number of bytes to copy
  658. * @skip: Number of bytes to skip before copying
  659. *
  660. * Returns the number of copied bytes.
  661. *
  662. **/
  663. size_t sg_pcopy_to_buffer(struct scatterlist *sgl, unsigned int nents,
  664. void *buf, size_t buflen, off_t skip)
  665. {
  666. return sg_copy_buffer(sgl, nents, buf, buflen, skip, true);
  667. }
  668. EXPORT_SYMBOL(sg_pcopy_to_buffer);