Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 2 | /* |
| 3 | * (C) 2001 Clemson University and The University of Chicago |
| 4 | * |
| 5 | * See COPYING in top-level directory. |
| 6 | */ |
| 7 | #include "protocol.h" |
Mike Marshall | 575e946 | 2015-12-04 12:56:14 -0500 | [diff] [blame] | 8 | #include "orangefs-kernel.h" |
| 9 | #include "orangefs-bufmap.h" |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 10 | |
Al Viro | ea2c9c9 | 2016-02-13 21:01:21 -0500 | [diff] [blame] | 11 | struct slot_map { |
| 12 | int c; |
| 13 | wait_queue_head_t q; |
| 14 | int count; |
| 15 | unsigned long *map; |
| 16 | }; |
| 17 | |
| 18 | static struct slot_map rw_map = { |
| 19 | .c = -1, |
| 20 | .q = __WAIT_QUEUE_HEAD_INITIALIZER(rw_map.q) |
| 21 | }; |
| 22 | static struct slot_map readdir_map = { |
| 23 | .c = -1, |
| 24 | .q = __WAIT_QUEUE_HEAD_INITIALIZER(readdir_map.q) |
| 25 | }; |
| 26 | |
| 27 | |
| 28 | static void install(struct slot_map *m, int count, unsigned long *map) |
| 29 | { |
| 30 | spin_lock(&m->q.lock); |
| 31 | m->c = m->count = count; |
| 32 | m->map = map; |
| 33 | wake_up_all_locked(&m->q); |
| 34 | spin_unlock(&m->q.lock); |
| 35 | } |
| 36 | |
| 37 | static void mark_killed(struct slot_map *m) |
| 38 | { |
| 39 | spin_lock(&m->q.lock); |
| 40 | m->c -= m->count + 1; |
| 41 | spin_unlock(&m->q.lock); |
| 42 | } |
| 43 | |
| 44 | static void run_down(struct slot_map *m) |
| 45 | { |
| 46 | DEFINE_WAIT(wait); |
| 47 | spin_lock(&m->q.lock); |
| 48 | if (m->c != -1) { |
| 49 | for (;;) { |
Ingo Molnar | 2055da9 | 2017-06-20 12:06:46 +0200 | [diff] [blame] | 50 | if (likely(list_empty(&wait.entry))) |
Ingo Molnar | ac6424b | 2017-06-20 12:06:13 +0200 | [diff] [blame] | 51 | __add_wait_queue_entry_tail(&m->q, &wait); |
Al Viro | ea2c9c9 | 2016-02-13 21:01:21 -0500 | [diff] [blame] | 52 | set_current_state(TASK_UNINTERRUPTIBLE); |
| 53 | |
| 54 | if (m->c == -1) |
| 55 | break; |
| 56 | |
| 57 | spin_unlock(&m->q.lock); |
| 58 | schedule(); |
| 59 | spin_lock(&m->q.lock); |
| 60 | } |
| 61 | __remove_wait_queue(&m->q, &wait); |
| 62 | __set_current_state(TASK_RUNNING); |
| 63 | } |
| 64 | m->map = NULL; |
| 65 | spin_unlock(&m->q.lock); |
| 66 | } |
| 67 | |
| 68 | static void put(struct slot_map *m, int slot) |
| 69 | { |
| 70 | int v; |
| 71 | spin_lock(&m->q.lock); |
| 72 | __clear_bit(slot, m->map); |
| 73 | v = ++m->c; |
David Reynolds | c2676ef | 2018-03-08 18:54:12 -0500 | [diff] [blame] | 74 | if (v > 0) |
Al Viro | ea2c9c9 | 2016-02-13 21:01:21 -0500 | [diff] [blame] | 75 | wake_up_locked(&m->q); |
David Reynolds | c2676ef | 2018-03-08 18:54:12 -0500 | [diff] [blame] | 76 | if (unlikely(v == -1)) /* finished dying */ |
Al Viro | ea2c9c9 | 2016-02-13 21:01:21 -0500 | [diff] [blame] | 77 | wake_up_all_locked(&m->q); |
| 78 | spin_unlock(&m->q.lock); |
| 79 | } |
| 80 | |
| 81 | static int wait_for_free(struct slot_map *m) |
| 82 | { |
| 83 | long left = slot_timeout_secs * HZ; |
| 84 | DEFINE_WAIT(wait); |
| 85 | |
| 86 | do { |
| 87 | long n = left, t; |
Ingo Molnar | 2055da9 | 2017-06-20 12:06:46 +0200 | [diff] [blame] | 88 | if (likely(list_empty(&wait.entry))) |
Ingo Molnar | ac6424b | 2017-06-20 12:06:13 +0200 | [diff] [blame] | 89 | __add_wait_queue_entry_tail_exclusive(&m->q, &wait); |
Al Viro | ea2c9c9 | 2016-02-13 21:01:21 -0500 | [diff] [blame] | 90 | set_current_state(TASK_INTERRUPTIBLE); |
| 91 | |
| 92 | if (m->c > 0) |
| 93 | break; |
| 94 | |
| 95 | if (m->c < 0) { |
| 96 | /* we are waiting for map to be installed */ |
| 97 | /* it would better be there soon, or we go away */ |
| 98 | if (n > ORANGEFS_BUFMAP_WAIT_TIMEOUT_SECS * HZ) |
| 99 | n = ORANGEFS_BUFMAP_WAIT_TIMEOUT_SECS * HZ; |
| 100 | } |
| 101 | spin_unlock(&m->q.lock); |
| 102 | t = schedule_timeout(n); |
| 103 | spin_lock(&m->q.lock); |
| 104 | if (unlikely(!t) && n != left && m->c < 0) |
| 105 | left = t; |
| 106 | else |
| 107 | left = t + (left - n); |
Davidlohr Bueso | 08d405c | 2019-01-03 15:28:58 -0800 | [diff] [blame] | 108 | if (signal_pending(current)) |
Al Viro | ea2c9c9 | 2016-02-13 21:01:21 -0500 | [diff] [blame] | 109 | left = -EINTR; |
| 110 | } while (left > 0); |
| 111 | |
Ingo Molnar | 2055da9 | 2017-06-20 12:06:46 +0200 | [diff] [blame] | 112 | if (!list_empty(&wait.entry)) |
| 113 | list_del(&wait.entry); |
Al Viro | ea2c9c9 | 2016-02-13 21:01:21 -0500 | [diff] [blame] | 114 | else if (left <= 0 && waitqueue_active(&m->q)) |
| 115 | __wake_up_locked_key(&m->q, TASK_INTERRUPTIBLE, NULL); |
| 116 | __set_current_state(TASK_RUNNING); |
| 117 | |
| 118 | if (likely(left > 0)) |
| 119 | return 0; |
| 120 | |
| 121 | return left < 0 ? -EINTR : -ETIMEDOUT; |
| 122 | } |
| 123 | |
| 124 | static int get(struct slot_map *m) |
| 125 | { |
| 126 | int res = 0; |
| 127 | spin_lock(&m->q.lock); |
| 128 | if (unlikely(m->c <= 0)) |
| 129 | res = wait_for_free(m); |
| 130 | if (likely(!res)) { |
| 131 | m->c--; |
| 132 | res = find_first_zero_bit(m->map, m->count); |
| 133 | __set_bit(res, m->map); |
| 134 | } |
| 135 | spin_unlock(&m->q.lock); |
| 136 | return res; |
| 137 | } |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 138 | |
Martin Brandenburg | bf89f58 | 2015-12-15 14:45:12 -0500 | [diff] [blame] | 139 | /* used to describe mapped buffers */ |
| 140 | struct orangefs_bufmap_desc { |
Mike Marshall | 817e9b4 | 2018-06-01 12:19:45 -0400 | [diff] [blame] | 141 | void __user *uaddr; /* user space address pointer */ |
Martin Brandenburg | bf89f58 | 2015-12-15 14:45:12 -0500 | [diff] [blame] | 142 | struct page **page_array; /* array of mapped pages */ |
| 143 | int array_count; /* size of above arrays */ |
| 144 | struct list_head list_link; |
| 145 | }; |
| 146 | |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 147 | static struct orangefs_bufmap { |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 148 | int desc_size; |
| 149 | int desc_shift; |
| 150 | int desc_count; |
| 151 | int total_size; |
| 152 | int page_count; |
| 153 | |
| 154 | struct page **page_array; |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 155 | struct orangefs_bufmap_desc *desc_array; |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 156 | |
| 157 | /* array to track usage of buffer descriptors */ |
Al Viro | ea2c9c9 | 2016-02-13 21:01:21 -0500 | [diff] [blame] | 158 | unsigned long *buffer_index_array; |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 159 | |
| 160 | /* array to track usage of buffer descriptors for readdir */ |
Al Viro | ea2c9c9 | 2016-02-13 21:01:21 -0500 | [diff] [blame] | 161 | #define N DIV_ROUND_UP(ORANGEFS_READDIR_DEFAULT_DESC_COUNT, BITS_PER_LONG) |
| 162 | unsigned long readdir_index_array[N]; |
| 163 | #undef N |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 164 | } *__orangefs_bufmap; |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 165 | |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 166 | static DEFINE_SPINLOCK(orangefs_bufmap_lock); |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 167 | |
| 168 | static void |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 169 | orangefs_bufmap_unmap(struct orangefs_bufmap *bufmap) |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 170 | { |
John Hubbard | 0df5564 | 2020-05-22 20:59:09 -0700 | [diff] [blame] | 171 | unpin_user_pages(bufmap->page_array, bufmap->page_count); |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | static void |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 175 | orangefs_bufmap_free(struct orangefs_bufmap *bufmap) |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 176 | { |
| 177 | kfree(bufmap->page_array); |
| 178 | kfree(bufmap->desc_array); |
Christophe JAILLET | 40a7487 | 2021-12-27 19:09:18 +0100 | [diff] [blame] | 179 | bitmap_free(bufmap->buffer_index_array); |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 180 | kfree(bufmap); |
| 181 | } |
| 182 | |
Martin Brandenburg | b09d10d | 2015-12-15 14:54:27 -0500 | [diff] [blame] | 183 | /* |
Mike Marshall | 95f5f88 | 2018-05-11 17:11:48 -0400 | [diff] [blame] | 184 | * XXX: Can the size and shift change while the caller gives up the |
Martin Brandenburg | b09d10d | 2015-12-15 14:54:27 -0500 | [diff] [blame] | 185 | * XXX: lock between calling this and doing something useful? |
| 186 | */ |
| 187 | |
Martin Brandenburg | 765a75b | 2015-12-15 14:48:17 -0500 | [diff] [blame] | 188 | int orangefs_bufmap_size_query(void) |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 189 | { |
Martin Brandenburg | b09d10d | 2015-12-15 14:54:27 -0500 | [diff] [blame] | 190 | struct orangefs_bufmap *bufmap; |
| 191 | int size = 0; |
Al Viro | 1780418 | 2016-02-13 11:16:37 -0500 | [diff] [blame] | 192 | spin_lock(&orangefs_bufmap_lock); |
| 193 | bufmap = __orangefs_bufmap; |
| 194 | if (bufmap) |
Martin Brandenburg | b09d10d | 2015-12-15 14:54:27 -0500 | [diff] [blame] | 195 | size = bufmap->desc_size; |
Al Viro | 1780418 | 2016-02-13 11:16:37 -0500 | [diff] [blame] | 196 | spin_unlock(&orangefs_bufmap_lock); |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 197 | return size; |
| 198 | } |
| 199 | |
Martin Brandenburg | 765a75b | 2015-12-15 14:48:17 -0500 | [diff] [blame] | 200 | int orangefs_bufmap_shift_query(void) |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 201 | { |
Martin Brandenburg | b09d10d | 2015-12-15 14:54:27 -0500 | [diff] [blame] | 202 | struct orangefs_bufmap *bufmap; |
| 203 | int shift = 0; |
Al Viro | 1780418 | 2016-02-13 11:16:37 -0500 | [diff] [blame] | 204 | spin_lock(&orangefs_bufmap_lock); |
| 205 | bufmap = __orangefs_bufmap; |
| 206 | if (bufmap) |
Martin Brandenburg | b09d10d | 2015-12-15 14:54:27 -0500 | [diff] [blame] | 207 | shift = bufmap->desc_shift; |
Al Viro | 1780418 | 2016-02-13 11:16:37 -0500 | [diff] [blame] | 208 | spin_unlock(&orangefs_bufmap_lock); |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 209 | return shift; |
| 210 | } |
| 211 | |
| 212 | static DECLARE_WAIT_QUEUE_HEAD(bufmap_waitq); |
| 213 | static DECLARE_WAIT_QUEUE_HEAD(readdir_waitq); |
| 214 | |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 215 | static struct orangefs_bufmap * |
| 216 | orangefs_bufmap_alloc(struct ORANGEFS_dev_map_desc *user_desc) |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 217 | { |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 218 | struct orangefs_bufmap *bufmap; |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 219 | |
| 220 | bufmap = kzalloc(sizeof(*bufmap), GFP_KERNEL); |
| 221 | if (!bufmap) |
| 222 | goto out; |
| 223 | |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 224 | bufmap->total_size = user_desc->total_size; |
| 225 | bufmap->desc_count = user_desc->count; |
| 226 | bufmap->desc_size = user_desc->size; |
| 227 | bufmap->desc_shift = ilog2(bufmap->desc_size); |
| 228 | |
Christophe JAILLET | 40a7487 | 2021-12-27 19:09:18 +0100 | [diff] [blame] | 229 | bufmap->buffer_index_array = bitmap_zalloc(bufmap->desc_count, GFP_KERNEL); |
Markus Elfring | 07a2585 | 2017-08-17 21:00:07 +0200 | [diff] [blame] | 230 | if (!bufmap->buffer_index_array) |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 231 | goto out_free_bufmap; |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 232 | |
| 233 | bufmap->desc_array = |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 234 | kcalloc(bufmap->desc_count, sizeof(struct orangefs_bufmap_desc), |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 235 | GFP_KERNEL); |
Markus Elfring | 07a2585 | 2017-08-17 21:00:07 +0200 | [diff] [blame] | 236 | if (!bufmap->desc_array) |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 237 | goto out_free_index_array; |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 238 | |
| 239 | bufmap->page_count = bufmap->total_size / PAGE_SIZE; |
| 240 | |
| 241 | /* allocate storage to track our page mappings */ |
| 242 | bufmap->page_array = |
| 243 | kcalloc(bufmap->page_count, sizeof(struct page *), GFP_KERNEL); |
| 244 | if (!bufmap->page_array) |
| 245 | goto out_free_desc_array; |
| 246 | |
| 247 | return bufmap; |
| 248 | |
| 249 | out_free_desc_array: |
| 250 | kfree(bufmap->desc_array); |
| 251 | out_free_index_array: |
Christophe JAILLET | 40a7487 | 2021-12-27 19:09:18 +0100 | [diff] [blame] | 252 | bitmap_free(bufmap->buffer_index_array); |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 253 | out_free_bufmap: |
| 254 | kfree(bufmap); |
| 255 | out: |
| 256 | return NULL; |
| 257 | } |
| 258 | |
| 259 | static int |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 260 | orangefs_bufmap_map(struct orangefs_bufmap *bufmap, |
| 261 | struct ORANGEFS_dev_map_desc *user_desc) |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 262 | { |
| 263 | int pages_per_desc = bufmap->desc_size / PAGE_SIZE; |
| 264 | int offset = 0, ret, i; |
| 265 | |
| 266 | /* map the pages */ |
John Hubbard | 0df5564 | 2020-05-22 20:59:09 -0700 | [diff] [blame] | 267 | ret = pin_user_pages_fast((unsigned long)user_desc->ptr, |
Ira Weiny | 73b0140 | 2019-05-13 17:17:11 -0700 | [diff] [blame] | 268 | bufmap->page_count, FOLL_WRITE, bufmap->page_array); |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 269 | |
| 270 | if (ret < 0) |
| 271 | return ret; |
| 272 | |
| 273 | if (ret != bufmap->page_count) { |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 274 | gossip_err("orangefs error: asked for %d pages, only got %d.\n", |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 275 | bufmap->page_count, ret); |
| 276 | |
| 277 | for (i = 0; i < ret; i++) { |
| 278 | SetPageError(bufmap->page_array[i]); |
John Hubbard | 0df5564 | 2020-05-22 20:59:09 -0700 | [diff] [blame] | 279 | unpin_user_page(bufmap->page_array[i]); |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 280 | } |
| 281 | return -ENOMEM; |
| 282 | } |
| 283 | |
| 284 | /* |
| 285 | * ideally we want to get kernel space pointers for each page, but |
| 286 | * we can't kmap that many pages at once if highmem is being used. |
| 287 | * so instead, we just kmap/kunmap the page address each time the |
| 288 | * kaddr is needed. |
| 289 | */ |
| 290 | for (i = 0; i < bufmap->page_count; i++) |
| 291 | flush_dcache_page(bufmap->page_array[i]); |
| 292 | |
| 293 | /* build a list of available descriptors */ |
| 294 | for (offset = 0, i = 0; i < bufmap->desc_count; i++) { |
| 295 | bufmap->desc_array[i].page_array = &bufmap->page_array[offset]; |
| 296 | bufmap->desc_array[i].array_count = pages_per_desc; |
| 297 | bufmap->desc_array[i].uaddr = |
| 298 | (user_desc->ptr + (i * pages_per_desc * PAGE_SIZE)); |
| 299 | offset += pages_per_desc; |
| 300 | } |
| 301 | |
| 302 | return 0; |
| 303 | } |
| 304 | |
| 305 | /* |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 306 | * orangefs_bufmap_initialize() |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 307 | * |
| 308 | * initializes the mapped buffer interface |
| 309 | * |
| 310 | * returns 0 on success, -errno on failure |
| 311 | */ |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 312 | int orangefs_bufmap_initialize(struct ORANGEFS_dev_map_desc *user_desc) |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 313 | { |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 314 | struct orangefs_bufmap *bufmap; |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 315 | int ret = -EINVAL; |
| 316 | |
| 317 | gossip_debug(GOSSIP_BUFMAP_DEBUG, |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 318 | "orangefs_bufmap_initialize: called (ptr (" |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 319 | "%p) sz (%d) cnt(%d).\n", |
| 320 | user_desc->ptr, |
| 321 | user_desc->size, |
| 322 | user_desc->count); |
| 323 | |
Dan Carpenter | eb82fbc | 2017-01-21 08:04:45 +0300 | [diff] [blame] | 324 | if (user_desc->total_size < 0 || |
| 325 | user_desc->size < 0 || |
| 326 | user_desc->count < 0) |
| 327 | goto out; |
| 328 | |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 329 | /* |
| 330 | * sanity check alignment and size of buffer that caller wants to |
| 331 | * work with |
| 332 | */ |
| 333 | if (PAGE_ALIGN((unsigned long)user_desc->ptr) != |
| 334 | (unsigned long)user_desc->ptr) { |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 335 | gossip_err("orangefs error: memory alignment (front). %p\n", |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 336 | user_desc->ptr); |
| 337 | goto out; |
| 338 | } |
| 339 | |
| 340 | if (PAGE_ALIGN(((unsigned long)user_desc->ptr + user_desc->total_size)) |
| 341 | != (unsigned long)(user_desc->ptr + user_desc->total_size)) { |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 342 | gossip_err("orangefs error: memory alignment (back).(%p + %d)\n", |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 343 | user_desc->ptr, |
| 344 | user_desc->total_size); |
| 345 | goto out; |
| 346 | } |
| 347 | |
| 348 | if (user_desc->total_size != (user_desc->size * user_desc->count)) { |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 349 | gossip_err("orangefs error: user provided an oddly sized buffer: (%d, %d, %d)\n", |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 350 | user_desc->total_size, |
| 351 | user_desc->size, |
| 352 | user_desc->count); |
| 353 | goto out; |
| 354 | } |
| 355 | |
| 356 | if ((user_desc->size % PAGE_SIZE) != 0) { |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 357 | gossip_err("orangefs error: bufmap size not page size divisible (%d).\n", |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 358 | user_desc->size); |
| 359 | goto out; |
| 360 | } |
| 361 | |
| 362 | ret = -ENOMEM; |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 363 | bufmap = orangefs_bufmap_alloc(user_desc); |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 364 | if (!bufmap) |
| 365 | goto out; |
| 366 | |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 367 | ret = orangefs_bufmap_map(bufmap, user_desc); |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 368 | if (ret) |
| 369 | goto out_free_bufmap; |
| 370 | |
| 371 | |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 372 | spin_lock(&orangefs_bufmap_lock); |
| 373 | if (__orangefs_bufmap) { |
| 374 | spin_unlock(&orangefs_bufmap_lock); |
| 375 | gossip_err("orangefs: error: bufmap already initialized.\n"); |
Al Viro | ea2c9c9 | 2016-02-13 21:01:21 -0500 | [diff] [blame] | 376 | ret = -EINVAL; |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 377 | goto out_unmap_bufmap; |
| 378 | } |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 379 | __orangefs_bufmap = bufmap; |
Al Viro | ea2c9c9 | 2016-02-13 21:01:21 -0500 | [diff] [blame] | 380 | install(&rw_map, |
| 381 | bufmap->desc_count, |
| 382 | bufmap->buffer_index_array); |
| 383 | install(&readdir_map, |
| 384 | ORANGEFS_READDIR_DEFAULT_DESC_COUNT, |
| 385 | bufmap->readdir_index_array); |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 386 | spin_unlock(&orangefs_bufmap_lock); |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 387 | |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 388 | gossip_debug(GOSSIP_BUFMAP_DEBUG, |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 389 | "orangefs_bufmap_initialize: exiting normally\n"); |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 390 | return 0; |
| 391 | |
| 392 | out_unmap_bufmap: |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 393 | orangefs_bufmap_unmap(bufmap); |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 394 | out_free_bufmap: |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 395 | orangefs_bufmap_free(bufmap); |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 396 | out: |
| 397 | return ret; |
| 398 | } |
| 399 | |
| 400 | /* |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 401 | * orangefs_bufmap_finalize() |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 402 | * |
| 403 | * shuts down the mapped buffer interface and releases any resources |
| 404 | * associated with it |
| 405 | * |
| 406 | * no return value |
| 407 | */ |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 408 | void orangefs_bufmap_finalize(void) |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 409 | { |
Al Viro | ea2c9c9 | 2016-02-13 21:01:21 -0500 | [diff] [blame] | 410 | struct orangefs_bufmap *bufmap = __orangefs_bufmap; |
| 411 | if (!bufmap) |
| 412 | return; |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 413 | gossip_debug(GOSSIP_BUFMAP_DEBUG, "orangefs_bufmap_finalize: called\n"); |
Al Viro | ea2c9c9 | 2016-02-13 21:01:21 -0500 | [diff] [blame] | 414 | mark_killed(&rw_map); |
| 415 | mark_killed(&readdir_map); |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 416 | gossip_debug(GOSSIP_BUFMAP_DEBUG, |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 417 | "orangefs_bufmap_finalize: exiting normally\n"); |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 418 | } |
| 419 | |
Al Viro | ea2c9c9 | 2016-02-13 21:01:21 -0500 | [diff] [blame] | 420 | void orangefs_bufmap_run_down(void) |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 421 | { |
Al Viro | ea2c9c9 | 2016-02-13 21:01:21 -0500 | [diff] [blame] | 422 | struct orangefs_bufmap *bufmap = __orangefs_bufmap; |
| 423 | if (!bufmap) |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 424 | return; |
Al Viro | ea2c9c9 | 2016-02-13 21:01:21 -0500 | [diff] [blame] | 425 | run_down(&rw_map); |
| 426 | run_down(&readdir_map); |
| 427 | spin_lock(&orangefs_bufmap_lock); |
| 428 | __orangefs_bufmap = NULL; |
| 429 | spin_unlock(&orangefs_bufmap_lock); |
| 430 | orangefs_bufmap_unmap(bufmap); |
| 431 | orangefs_bufmap_free(bufmap); |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | /* |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 435 | * orangefs_bufmap_get() |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 436 | * |
| 437 | * gets a free mapped buffer descriptor, will sleep until one becomes |
| 438 | * available if necessary |
| 439 | * |
Al Viro | b8a99a8 | 2016-02-16 20:10:26 -0500 | [diff] [blame] | 440 | * returns slot on success, -errno on failure |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 441 | */ |
Al Viro | b8a99a8 | 2016-02-16 20:10:26 -0500 | [diff] [blame] | 442 | int orangefs_bufmap_get(void) |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 443 | { |
Al Viro | b8a99a8 | 2016-02-16 20:10:26 -0500 | [diff] [blame] | 444 | return get(&rw_map); |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | /* |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 448 | * orangefs_bufmap_put() |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 449 | * |
| 450 | * returns a mapped buffer descriptor to the collection |
| 451 | * |
| 452 | * no return value |
| 453 | */ |
Al Viro | 1357d06 | 2016-02-11 21:34:52 -0500 | [diff] [blame] | 454 | void orangefs_bufmap_put(int buffer_index) |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 455 | { |
Al Viro | ea2c9c9 | 2016-02-13 21:01:21 -0500 | [diff] [blame] | 456 | put(&rw_map, buffer_index); |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | /* |
Martin Brandenburg | 7d22148 | 2016-01-04 15:05:28 -0500 | [diff] [blame] | 460 | * orangefs_readdir_index_get() |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 461 | * |
| 462 | * gets a free descriptor, will sleep until one becomes |
| 463 | * available if necessary. |
| 464 | * Although the readdir buffers are not mapped into kernel space |
| 465 | * we could do that at a later point of time. Regardless, these |
| 466 | * indices are used by the client-core. |
| 467 | * |
Al Viro | b8a99a8 | 2016-02-16 20:10:26 -0500 | [diff] [blame] | 468 | * returns slot on success, -errno on failure |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 469 | */ |
Al Viro | b8a99a8 | 2016-02-16 20:10:26 -0500 | [diff] [blame] | 470 | int orangefs_readdir_index_get(void) |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 471 | { |
Al Viro | b8a99a8 | 2016-02-16 20:10:26 -0500 | [diff] [blame] | 472 | return get(&readdir_map); |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 473 | } |
| 474 | |
Al Viro | 82d37f1 | 2016-02-13 21:04:51 -0500 | [diff] [blame] | 475 | void orangefs_readdir_index_put(int buffer_index) |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 476 | { |
Al Viro | ea2c9c9 | 2016-02-13 21:01:21 -0500 | [diff] [blame] | 477 | put(&readdir_map, buffer_index); |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 478 | } |
| 479 | |
Mike Marshall | b5e376e | 2015-12-11 10:50:42 -0500 | [diff] [blame] | 480 | /* |
Mike Marshall | 95f5f88 | 2018-05-11 17:11:48 -0400 | [diff] [blame] | 481 | * we've been handed an iovec, we need to copy it to |
Mike Marshall | b5e376e | 2015-12-11 10:50:42 -0500 | [diff] [blame] | 482 | * the shared memory descriptor at "buffer_index". |
| 483 | */ |
Al Viro | bf6bf60 | 2016-02-16 20:06:19 -0500 | [diff] [blame] | 484 | int orangefs_bufmap_copy_from_iovec(struct iov_iter *iter, |
Mike Marshall | 5480494 | 2015-10-05 13:44:24 -0400 | [diff] [blame] | 485 | int buffer_index, |
| 486 | size_t size) |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 487 | { |
Al Viro | bf6bf60 | 2016-02-16 20:06:19 -0500 | [diff] [blame] | 488 | struct orangefs_bufmap_desc *to; |
Mike Marshall | 4d1c440 | 2015-09-04 10:31:16 -0400 | [diff] [blame] | 489 | int i; |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 490 | |
| 491 | gossip_debug(GOSSIP_BUFMAP_DEBUG, |
Al Viro | 34204fd | 2015-10-08 17:47:44 -0400 | [diff] [blame] | 492 | "%s: buffer_index:%d: size:%zu:\n", |
Mike Marshall | 4d1c440 | 2015-09-04 10:31:16 -0400 | [diff] [blame] | 493 | __func__, buffer_index, size); |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 494 | |
Al Viro | bf6bf60 | 2016-02-16 20:06:19 -0500 | [diff] [blame] | 495 | to = &__orangefs_bufmap->desc_array[buffer_index]; |
Mike Marshall | 4d1c440 | 2015-09-04 10:31:16 -0400 | [diff] [blame] | 496 | for (i = 0; size; i++) { |
Al Viro | 34204fd | 2015-10-08 17:47:44 -0400 | [diff] [blame] | 497 | struct page *page = to->page_array[i]; |
| 498 | size_t n = size; |
| 499 | if (n > PAGE_SIZE) |
| 500 | n = PAGE_SIZE; |
Al Viro | 890559e | 2017-04-13 03:10:18 -0400 | [diff] [blame] | 501 | if (copy_page_from_iter(page, 0, n, iter) != n) |
Al Viro | 34204fd | 2015-10-08 17:47:44 -0400 | [diff] [blame] | 502 | return -EFAULT; |
| 503 | size -= n; |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 504 | } |
Al Viro | 34204fd | 2015-10-08 17:47:44 -0400 | [diff] [blame] | 505 | return 0; |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | /* |
Mike Marshall | b5e376e | 2015-12-11 10:50:42 -0500 | [diff] [blame] | 509 | * we've been handed an iovec, we need to fill it from |
| 510 | * the shared memory descriptor at "buffer_index". |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 511 | */ |
Al Viro | bf6bf60 | 2016-02-16 20:06:19 -0500 | [diff] [blame] | 512 | int orangefs_bufmap_copy_to_iovec(struct iov_iter *iter, |
Al Viro | 5c27822 | 2015-10-08 17:43:58 -0400 | [diff] [blame] | 513 | int buffer_index, |
| 514 | size_t size) |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 515 | { |
Al Viro | bf6bf60 | 2016-02-16 20:06:19 -0500 | [diff] [blame] | 516 | struct orangefs_bufmap_desc *from; |
Mike Marshall | 4d1c440 | 2015-09-04 10:31:16 -0400 | [diff] [blame] | 517 | int i; |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 518 | |
Al Viro | bf6bf60 | 2016-02-16 20:06:19 -0500 | [diff] [blame] | 519 | from = &__orangefs_bufmap->desc_array[buffer_index]; |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 520 | gossip_debug(GOSSIP_BUFMAP_DEBUG, |
Al Viro | 5c27822 | 2015-10-08 17:43:58 -0400 | [diff] [blame] | 521 | "%s: buffer_index:%d: size:%zu:\n", |
| 522 | __func__, buffer_index, size); |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 523 | |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 524 | |
Al Viro | 5c27822 | 2015-10-08 17:43:58 -0400 | [diff] [blame] | 525 | for (i = 0; size; i++) { |
| 526 | struct page *page = from->page_array[i]; |
| 527 | size_t n = size; |
| 528 | if (n > PAGE_SIZE) |
| 529 | n = PAGE_SIZE; |
| 530 | n = copy_page_to_iter(page, 0, n, iter); |
| 531 | if (!n) |
| 532 | return -EFAULT; |
| 533 | size -= n; |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 534 | } |
Al Viro | 5c27822 | 2015-10-08 17:43:58 -0400 | [diff] [blame] | 535 | return 0; |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 536 | } |
Mike Marshall | dd59a64 | 2019-03-25 18:59:29 -0400 | [diff] [blame] | 537 | |
| 538 | void orangefs_bufmap_page_fill(void *page_to, |
| 539 | int buffer_index, |
| 540 | int slot_index) |
| 541 | { |
| 542 | struct orangefs_bufmap_desc *from; |
| 543 | void *page_from; |
| 544 | |
| 545 | from = &__orangefs_bufmap->desc_array[buffer_index]; |
| 546 | page_from = kmap_atomic(from->page_array[slot_index]); |
| 547 | memcpy(page_to, page_from, PAGE_SIZE); |
| 548 | kunmap_atomic(page_from); |
| 549 | } |