pagevec.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 void __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. while (got < num_pages) {
  22. rc = get_user_pages_unlocked(current, current->mm,
  23. (unsigned long)data + ((unsigned long)got * PAGE_SIZE),
  24. num_pages - got, write_page, 0, pages + got);
  25. if (rc < 0)
  26. break;
  27. BUG_ON(rc == 0);
  28. got += rc;
  29. }
  30. if (rc < 0)
  31. goto fail;
  32. return pages;
  33. fail:
  34. ceph_put_page_vector(pages, got, false);
  35. return ERR_PTR(rc);
  36. }
  37. EXPORT_SYMBOL(ceph_get_direct_page_vector);
  38. void ceph_put_page_vector(struct page **pages, int num_pages, bool dirty)
  39. {
  40. int i;
  41. for (i = 0; i < num_pages; i++) {
  42. if (dirty)
  43. set_page_dirty_lock(pages[i]);
  44. put_page(pages[i]);
  45. }
  46. kvfree(pages);
  47. }
  48. EXPORT_SYMBOL(ceph_put_page_vector);
  49. void ceph_release_page_vector(struct page **pages, int num_pages)
  50. {
  51. int i;
  52. for (i = 0; i < num_pages; i++)
  53. __free_pages(pages[i], 0);
  54. kfree(pages);
  55. }
  56. EXPORT_SYMBOL(ceph_release_page_vector);
  57. /*
  58. * allocate a vector new pages
  59. */
  60. struct page **ceph_alloc_page_vector(int num_pages, gfp_t flags)
  61. {
  62. struct page **pages;
  63. int i;
  64. pages = kmalloc(sizeof(*pages) * num_pages, flags);
  65. if (!pages)
  66. return ERR_PTR(-ENOMEM);
  67. for (i = 0; i < num_pages; i++) {
  68. pages[i] = __page_cache_alloc(flags);
  69. if (pages[i] == NULL) {
  70. ceph_release_page_vector(pages, i);
  71. return ERR_PTR(-ENOMEM);
  72. }
  73. }
  74. return pages;
  75. }
  76. EXPORT_SYMBOL(ceph_alloc_page_vector);
  77. /*
  78. * copy user data into a page vector
  79. */
  80. int ceph_copy_user_to_page_vector(struct page **pages,
  81. const void __user *data,
  82. loff_t off, size_t len)
  83. {
  84. int i = 0;
  85. int po = off & ~PAGE_CACHE_MASK;
  86. int left = len;
  87. int l, bad;
  88. while (left > 0) {
  89. l = min_t(int, PAGE_CACHE_SIZE-po, left);
  90. bad = copy_from_user(page_address(pages[i]) + po, data, l);
  91. if (bad == l)
  92. return -EFAULT;
  93. data += l - bad;
  94. left -= l - bad;
  95. po += l - bad;
  96. if (po == PAGE_CACHE_SIZE) {
  97. po = 0;
  98. i++;
  99. }
  100. }
  101. return len;
  102. }
  103. EXPORT_SYMBOL(ceph_copy_user_to_page_vector);
  104. void ceph_copy_to_page_vector(struct page **pages,
  105. const void *data,
  106. loff_t off, size_t len)
  107. {
  108. int i = 0;
  109. size_t po = off & ~PAGE_CACHE_MASK;
  110. size_t left = len;
  111. while (left > 0) {
  112. size_t l = min_t(size_t, PAGE_CACHE_SIZE-po, left);
  113. memcpy(page_address(pages[i]) + po, data, l);
  114. data += l;
  115. left -= l;
  116. po += l;
  117. if (po == PAGE_CACHE_SIZE) {
  118. po = 0;
  119. i++;
  120. }
  121. }
  122. }
  123. EXPORT_SYMBOL(ceph_copy_to_page_vector);
  124. void ceph_copy_from_page_vector(struct page **pages,
  125. void *data,
  126. loff_t off, size_t len)
  127. {
  128. int i = 0;
  129. size_t po = off & ~PAGE_CACHE_MASK;
  130. size_t left = len;
  131. while (left > 0) {
  132. size_t l = min_t(size_t, PAGE_CACHE_SIZE-po, left);
  133. memcpy(data, page_address(pages[i]) + po, l);
  134. data += l;
  135. left -= l;
  136. po += l;
  137. if (po == PAGE_CACHE_SIZE) {
  138. po = 0;
  139. i++;
  140. }
  141. }
  142. }
  143. EXPORT_SYMBOL(ceph_copy_from_page_vector);
  144. /*
  145. * Zero an extent within a page vector. Offset is relative to the
  146. * start of the first page.
  147. */
  148. void ceph_zero_page_vector_range(int off, int len, struct page **pages)
  149. {
  150. int i = off >> PAGE_CACHE_SHIFT;
  151. off &= ~PAGE_CACHE_MASK;
  152. dout("zero_page_vector_page %u~%u\n", off, len);
  153. /* leading partial page? */
  154. if (off) {
  155. int end = min((int)PAGE_CACHE_SIZE, off + len);
  156. dout("zeroing %d %p head from %d\n", i, pages[i],
  157. (int)off);
  158. zero_user_segment(pages[i], off, end);
  159. len -= (end - off);
  160. i++;
  161. }
  162. while (len >= PAGE_CACHE_SIZE) {
  163. dout("zeroing %d %p len=%d\n", i, pages[i], len);
  164. zero_user_segment(pages[i], 0, PAGE_CACHE_SIZE);
  165. len -= PAGE_CACHE_SIZE;
  166. i++;
  167. }
  168. /* trailing partial page? */
  169. if (len) {
  170. dout("zeroing %d %p tail to %d\n", i, pages[i], (int)len);
  171. zero_user_segment(pages[i], 0, len);
  172. }
  173. }
  174. EXPORT_SYMBOL(ceph_zero_page_vector_range);