scatterwalk.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * Cryptographic API.
  3. *
  4. * Cipher operations.
  5. *
  6. * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
  7. * 2002 Adam J. Richter <adam@yggdrasil.com>
  8. * 2004 Jean-Luc Cooke <jlcooke@certainkey.com>
  9. *
  10. * This program is free software; you can redistribute it and/or modify it
  11. * under the terms of the GNU General Public License as published by the Free
  12. * Software Foundation; either version 2 of the License, or (at your option)
  13. * any later version.
  14. *
  15. */
  16. #include <crypto/scatterwalk.h>
  17. #include <linux/kernel.h>
  18. #include <linux/mm.h>
  19. #include <linux/module.h>
  20. #include <linux/pagemap.h>
  21. #include <linux/highmem.h>
  22. #include <linux/scatterlist.h>
  23. static inline void memcpy_dir(void *buf, void *sgdata, size_t nbytes, int out)
  24. {
  25. void *src = out ? buf : sgdata;
  26. void *dst = out ? sgdata : buf;
  27. memcpy(dst, src, nbytes);
  28. }
  29. void scatterwalk_start(struct scatter_walk *walk, struct scatterlist *sg)
  30. {
  31. walk->sg = sg;
  32. BUG_ON(!sg->length);
  33. walk->offset = sg->offset;
  34. }
  35. EXPORT_SYMBOL_GPL(scatterwalk_start);
  36. void *scatterwalk_map(struct scatter_walk *walk, int out)
  37. {
  38. return crypto_kmap(scatterwalk_page(walk), out) +
  39. offset_in_page(walk->offset);
  40. }
  41. EXPORT_SYMBOL_GPL(scatterwalk_map);
  42. static void scatterwalk_pagedone(struct scatter_walk *walk, int out,
  43. unsigned int more)
  44. {
  45. if (out) {
  46. struct page *page;
  47. page = sg_page(walk->sg) + ((walk->offset - 1) >> PAGE_SHIFT);
  48. if (!PageSlab(page))
  49. flush_dcache_page(page);
  50. }
  51. if (more) {
  52. walk->offset += PAGE_SIZE - 1;
  53. walk->offset &= PAGE_MASK;
  54. if (walk->offset >= walk->sg->offset + walk->sg->length)
  55. scatterwalk_start(walk, scatterwalk_sg_next(walk->sg));
  56. }
  57. }
  58. void scatterwalk_done(struct scatter_walk *walk, int out, int more)
  59. {
  60. if (!(scatterwalk_pagelen(walk) & (PAGE_SIZE - 1)) || !more)
  61. scatterwalk_pagedone(walk, out, more);
  62. }
  63. EXPORT_SYMBOL_GPL(scatterwalk_done);
  64. void scatterwalk_copychunks(void *buf, struct scatter_walk *walk,
  65. size_t nbytes, int out)
  66. {
  67. for (;;) {
  68. unsigned int len_this_page = scatterwalk_pagelen(walk);
  69. u8 *vaddr;
  70. if (len_this_page > nbytes)
  71. len_this_page = nbytes;
  72. vaddr = scatterwalk_map(walk, out);
  73. memcpy_dir(buf, vaddr, len_this_page, out);
  74. scatterwalk_unmap(vaddr, out);
  75. scatterwalk_advance(walk, len_this_page);
  76. if (nbytes == len_this_page)
  77. break;
  78. buf += len_this_page;
  79. nbytes -= len_this_page;
  80. scatterwalk_pagedone(walk, out, 1);
  81. }
  82. }
  83. EXPORT_SYMBOL_GPL(scatterwalk_copychunks);
  84. void scatterwalk_map_and_copy(void *buf, struct scatterlist *sg,
  85. unsigned int start, unsigned int nbytes, int out)
  86. {
  87. struct scatter_walk walk;
  88. unsigned int offset = 0;
  89. if (!nbytes)
  90. return;
  91. for (;;) {
  92. scatterwalk_start(&walk, sg);
  93. if (start < offset + sg->length)
  94. break;
  95. offset += sg->length;
  96. sg = scatterwalk_sg_next(sg);
  97. }
  98. scatterwalk_advance(&walk, start - offset);
  99. scatterwalk_copychunks(buf, &walk, nbytes, out);
  100. scatterwalk_done(&walk, out, 0);
  101. }
  102. EXPORT_SYMBOL_GPL(scatterwalk_map_and_copy);