^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) #include <linux/ceph/ceph_debug.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/file.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/namei.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/writeback.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/ceph/libceph.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) void ceph_put_page_vector(struct page **pages, int num_pages, bool dirty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) for (i = 0; i < num_pages; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) if (dirty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) set_page_dirty_lock(pages[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) put_page(pages[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) kvfree(pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) EXPORT_SYMBOL(ceph_put_page_vector);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) void ceph_release_page_vector(struct page **pages, int num_pages)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) for (i = 0; i < num_pages; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) __free_pages(pages[i], 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) kfree(pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) EXPORT_SYMBOL(ceph_release_page_vector);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) * allocate a vector new pages
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) struct page **ceph_alloc_page_vector(int num_pages, gfp_t flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) struct page **pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) pages = kmalloc_array(num_pages, sizeof(*pages), flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) if (!pages)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) for (i = 0; i < num_pages; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) pages[i] = __page_cache_alloc(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) if (pages[i] == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) ceph_release_page_vector(pages, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) return pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) EXPORT_SYMBOL(ceph_alloc_page_vector);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) * copy user data into a page vector
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) int ceph_copy_user_to_page_vector(struct page **pages,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) const void __user *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) loff_t off, size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) int i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) int po = off & ~PAGE_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) int left = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) int l, bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) while (left > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) l = min_t(int, PAGE_SIZE-po, left);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) bad = copy_from_user(page_address(pages[i]) + po, data, l);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) if (bad == l)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) data += l - bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) left -= l - bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) po += l - bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) if (po == PAGE_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) po = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) i++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) return len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) EXPORT_SYMBOL(ceph_copy_user_to_page_vector);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) void ceph_copy_to_page_vector(struct page **pages,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) const void *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) loff_t off, size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) int i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) size_t po = off & ~PAGE_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) size_t left = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) while (left > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) size_t l = min_t(size_t, PAGE_SIZE-po, left);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) memcpy(page_address(pages[i]) + po, data, l);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) data += l;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) left -= l;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) po += l;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) if (po == PAGE_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) po = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) i++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) EXPORT_SYMBOL(ceph_copy_to_page_vector);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) void ceph_copy_from_page_vector(struct page **pages,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) void *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) loff_t off, size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) int i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) size_t po = off & ~PAGE_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) size_t left = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) while (left > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) size_t l = min_t(size_t, PAGE_SIZE-po, left);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) memcpy(data, page_address(pages[i]) + po, l);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) data += l;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) left -= l;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) po += l;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) if (po == PAGE_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) po = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) i++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) EXPORT_SYMBOL(ceph_copy_from_page_vector);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) * Zero an extent within a page vector. Offset is relative to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) * start of the first page.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) void ceph_zero_page_vector_range(int off, int len, struct page **pages)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) int i = off >> PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) off &= ~PAGE_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) dout("zero_page_vector_page %u~%u\n", off, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) /* leading partial page? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) if (off) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) int end = min((int)PAGE_SIZE, off + len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) dout("zeroing %d %p head from %d\n", i, pages[i],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) (int)off);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) zero_user_segment(pages[i], off, end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) len -= (end - off);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) i++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) while (len >= PAGE_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) dout("zeroing %d %p len=%d\n", i, pages[i], len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) zero_user_segment(pages[i], 0, PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) len -= PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) i++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) /* trailing partial page? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) if (len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) dout("zeroing %d %p tail to %d\n", i, pages[i], (int)len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) zero_user_segment(pages[i], 0, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) EXPORT_SYMBOL(ceph_zero_page_vector_range);