pagevec.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. #include <linux/ceph/ceph_debug.h>
  2. #include <linux/module.h>
  3. #include <linux/sched.h>
  4. #include <linux/slab.h>
  5. #include <linux/file.h>
  6. #include <linux/namei.h>
  7. #include <linux/writeback.h>
  8. #include <linux/ceph/libceph.h>
  9. /*
  10. * build a vector of user pages
  11. */
  12. struct page **ceph_get_direct_page_vector(const char __user *data,
  13. int num_pages, bool write_page)
  14. {
  15. struct page **pages;
  16. int got = 0;
  17. int rc = 0;
  18. pages = kmalloc(sizeof(*pages) * num_pages, GFP_NOFS);
  19. if (!pages)
  20. return ERR_PTR(-ENOMEM);
  21. down_read(&current->mm->mmap_sem);
  22. while (got < num_pages) {
  23. rc = get_user_pages(current, current->mm,
  24. (unsigned long)data + ((unsigned long)got * PAGE_SIZE),
  25. num_pages - got, write_page, 0, pages + got, NULL);
  26. if (rc < 0)
  27. break;
  28. BUG_ON(rc == 0);
  29. got += rc;
  30. }
  31. up_read(&current->mm->mmap_sem);
  32. if (rc < 0)
  33. goto fail;
  34. return pages;
  35. fail:
  36. ceph_put_page_vector(pages, got, false);
  37. return ERR_PTR(rc);
  38. }
  39. EXPORT_SYMBOL(ceph_get_direct_page_vector);
  40. void ceph_put_page_vector(struct page **pages, int num_pages, bool dirty)
  41. {
  42. int i;
  43. for (i = 0; i < num_pages; i++) {
  44. if (dirty)
  45. set_page_dirty_lock(pages[i]);
  46. put_page(pages[i]);
  47. }
  48. kfree(pages);
  49. }
  50. EXPORT_SYMBOL(ceph_put_page_vector);
  51. void ceph_release_page_vector(struct page **pages, int num_pages)
  52. {
  53. int i;
  54. for (i = 0; i < num_pages; i++)
  55. __free_pages(pages[i], 0);
  56. kfree(pages);
  57. }
  58. EXPORT_SYMBOL(ceph_release_page_vector);
  59. /*
  60. * allocate a vector new pages
  61. */
  62. struct page **ceph_alloc_page_vector(int num_pages, gfp_t flags)
  63. {
  64. struct page **pages;
  65. int i;
  66. pages = kmalloc(sizeof(*pages) * num_pages, flags);
  67. if (!pages)
  68. return ERR_PTR(-ENOMEM);
  69. for (i = 0; i < num_pages; i++) {
  70. pages[i] = __page_cache_alloc(flags);
  71. if (pages[i] == NULL) {
  72. ceph_release_page_vector(pages, i);
  73. return ERR_PTR(-ENOMEM);
  74. }
  75. }
  76. return pages;
  77. }
  78. EXPORT_SYMBOL(ceph_alloc_page_vector);
  79. /*
  80. * copy user data into a page vector
  81. */
  82. int ceph_copy_user_to_page_vector(struct page **pages,
  83. const char __user *data,
  84. loff_t off, size_t len)
  85. {
  86. int i = 0;
  87. int po = off & ~PAGE_CACHE_MASK;
  88. int left = len;
  89. int l, bad;
  90. while (left > 0) {
  91. l = min_t(int, PAGE_CACHE_SIZE-po, left);
  92. bad = copy_from_user(page_address(pages[i]) + po, data, l);
  93. if (bad == l)
  94. return -EFAULT;
  95. data += l - bad;
  96. left -= l - bad;
  97. po += l - bad;
  98. if (po == PAGE_CACHE_SIZE) {
  99. po = 0;
  100. i++;
  101. }
  102. }
  103. return len;
  104. }
  105. EXPORT_SYMBOL(ceph_copy_user_to_page_vector);
  106. int ceph_copy_to_page_vector(struct page **pages,
  107. const char *data,
  108. loff_t off, size_t len)
  109. {
  110. int i = 0;
  111. size_t po = off & ~PAGE_CACHE_MASK;
  112. size_t left = len;
  113. size_t l;
  114. while (left > 0) {
  115. l = min_t(size_t, PAGE_CACHE_SIZE-po, left);
  116. memcpy(page_address(pages[i]) + po, data, l);
  117. data += l;
  118. left -= l;
  119. po += l;
  120. if (po == PAGE_CACHE_SIZE) {
  121. po = 0;
  122. i++;
  123. }
  124. }
  125. return len;
  126. }
  127. EXPORT_SYMBOL(ceph_copy_to_page_vector);
  128. int ceph_copy_from_page_vector(struct page **pages,
  129. char *data,
  130. loff_t off, size_t len)
  131. {
  132. int i = 0;
  133. size_t po = off & ~PAGE_CACHE_MASK;
  134. size_t left = len;
  135. size_t l;
  136. while (left > 0) {
  137. l = min_t(size_t, PAGE_CACHE_SIZE-po, left);
  138. memcpy(data, page_address(pages[i]) + po, l);
  139. data += l;
  140. left -= l;
  141. po += l;
  142. if (po == PAGE_CACHE_SIZE) {
  143. po = 0;
  144. i++;
  145. }
  146. }
  147. return len;
  148. }
  149. EXPORT_SYMBOL(ceph_copy_from_page_vector);
  150. /*
  151. * copy user data from a page vector into a user pointer
  152. */
  153. int ceph_copy_page_vector_to_user(struct page **pages,
  154. char __user *data,
  155. loff_t off, size_t len)
  156. {
  157. int i = 0;
  158. int po = off & ~PAGE_CACHE_MASK;
  159. int left = len;
  160. int l, bad;
  161. while (left > 0) {
  162. l = min_t(int, left, PAGE_CACHE_SIZE-po);
  163. bad = copy_to_user(data, page_address(pages[i]) + po, l);
  164. if (bad == l)
  165. return -EFAULT;
  166. data += l - bad;
  167. left -= l - bad;
  168. if (po) {
  169. po += l - bad;
  170. if (po == PAGE_CACHE_SIZE)
  171. po = 0;
  172. }
  173. i++;
  174. }
  175. return len;
  176. }
  177. EXPORT_SYMBOL(ceph_copy_page_vector_to_user);
  178. /*
  179. * Zero an extent within a page vector. Offset is relative to the
  180. * start of the first page.
  181. */
  182. void ceph_zero_page_vector_range(int off, int len, struct page **pages)
  183. {
  184. int i = off >> PAGE_CACHE_SHIFT;
  185. off &= ~PAGE_CACHE_MASK;
  186. dout("zero_page_vector_page %u~%u\n", off, len);
  187. /* leading partial page? */
  188. if (off) {
  189. int end = min((int)PAGE_CACHE_SIZE, off + len);
  190. dout("zeroing %d %p head from %d\n", i, pages[i],
  191. (int)off);
  192. zero_user_segment(pages[i], off, end);
  193. len -= (end - off);
  194. i++;
  195. }
  196. while (len >= PAGE_CACHE_SIZE) {
  197. dout("zeroing %d %p len=%d\n", i, pages[i], len);
  198. zero_user_segment(pages[i], 0, PAGE_CACHE_SIZE);
  199. len -= PAGE_CACHE_SIZE;
  200. i++;
  201. }
  202. /* trailing partial page? */
  203. if (len) {
  204. dout("zeroing %d %p tail to %d\n", i, pages[i], (int)len);
  205. zero_user_segment(pages[i], 0, len);
  206. }
  207. }
  208. EXPORT_SYMBOL(ceph_zero_page_vector_range);