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