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