Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 1 | /* |
| 2 | * (C) 2001 Clemson University and The University of Chicago |
| 3 | * |
| 4 | * See COPYING in top-level directory. |
| 5 | */ |
| 6 | #include "protocol.h" |
Mike Marshall | 575e946 | 2015-12-04 12:56:14 -0500 | [diff] [blame] | 7 | #include "orangefs-kernel.h" |
| 8 | #include "orangefs-bufmap.h" |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 9 | |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 10 | DECLARE_WAIT_QUEUE_HEAD(orangefs_bufmap_init_waitq); |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 11 | |
Martin Brandenburg | bf89f58 | 2015-12-15 14:45:12 -0500 | [diff] [blame] | 12 | /* used to describe mapped buffers */ |
| 13 | struct orangefs_bufmap_desc { |
| 14 | void *uaddr; /* user space address pointer */ |
| 15 | struct page **page_array; /* array of mapped pages */ |
| 16 | int array_count; /* size of above arrays */ |
| 17 | struct list_head list_link; |
| 18 | }; |
| 19 | |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 20 | static struct orangefs_bufmap { |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 21 | atomic_t refcnt; |
| 22 | |
| 23 | int desc_size; |
| 24 | int desc_shift; |
| 25 | int desc_count; |
| 26 | int total_size; |
| 27 | int page_count; |
| 28 | |
| 29 | struct page **page_array; |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 30 | struct orangefs_bufmap_desc *desc_array; |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 31 | |
| 32 | /* array to track usage of buffer descriptors */ |
| 33 | int *buffer_index_array; |
| 34 | spinlock_t buffer_index_lock; |
| 35 | |
| 36 | /* array to track usage of buffer descriptors for readdir */ |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 37 | int readdir_index_array[ORANGEFS_READDIR_DEFAULT_DESC_COUNT]; |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 38 | spinlock_t readdir_index_lock; |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 39 | } *__orangefs_bufmap; |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 40 | |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 41 | static DEFINE_SPINLOCK(orangefs_bufmap_lock); |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 42 | |
| 43 | static void |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 44 | orangefs_bufmap_unmap(struct orangefs_bufmap *bufmap) |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 45 | { |
| 46 | int i; |
| 47 | |
| 48 | for (i = 0; i < bufmap->page_count; i++) |
| 49 | page_cache_release(bufmap->page_array[i]); |
| 50 | } |
| 51 | |
| 52 | static void |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 53 | orangefs_bufmap_free(struct orangefs_bufmap *bufmap) |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 54 | { |
| 55 | kfree(bufmap->page_array); |
| 56 | kfree(bufmap->desc_array); |
| 57 | kfree(bufmap->buffer_index_array); |
| 58 | kfree(bufmap); |
| 59 | } |
| 60 | |
Martin Brandenburg | bf89f58 | 2015-12-15 14:45:12 -0500 | [diff] [blame] | 61 | static struct orangefs_bufmap *orangefs_bufmap_ref(void) |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 62 | { |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 63 | struct orangefs_bufmap *bufmap = NULL; |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 64 | |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 65 | spin_lock(&orangefs_bufmap_lock); |
| 66 | if (__orangefs_bufmap) { |
| 67 | bufmap = __orangefs_bufmap; |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 68 | atomic_inc(&bufmap->refcnt); |
| 69 | } |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 70 | spin_unlock(&orangefs_bufmap_lock); |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 71 | return bufmap; |
| 72 | } |
| 73 | |
Martin Brandenburg | bf89f58 | 2015-12-15 14:45:12 -0500 | [diff] [blame] | 74 | static void orangefs_bufmap_unref(struct orangefs_bufmap *bufmap) |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 75 | { |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 76 | if (atomic_dec_and_lock(&bufmap->refcnt, &orangefs_bufmap_lock)) { |
| 77 | __orangefs_bufmap = NULL; |
| 78 | spin_unlock(&orangefs_bufmap_lock); |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 79 | |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 80 | orangefs_bufmap_unmap(bufmap); |
| 81 | orangefs_bufmap_free(bufmap); |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 82 | } |
| 83 | } |
| 84 | |
Martin Brandenburg | b09d10d | 2015-12-15 14:54:27 -0500 | [diff] [blame] | 85 | /* |
| 86 | * XXX: Can the size and shift change while the caller gives up the |
| 87 | * XXX: lock between calling this and doing something useful? |
| 88 | */ |
| 89 | |
Martin Brandenburg | 765a75b | 2015-12-15 14:48:17 -0500 | [diff] [blame] | 90 | int orangefs_bufmap_size_query(void) |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 91 | { |
Martin Brandenburg | b09d10d | 2015-12-15 14:54:27 -0500 | [diff] [blame] | 92 | struct orangefs_bufmap *bufmap; |
| 93 | int size = 0; |
Al Viro | 1780418 | 2016-02-13 11:16:37 -0500 | [diff] [blame^] | 94 | spin_lock(&orangefs_bufmap_lock); |
| 95 | bufmap = __orangefs_bufmap; |
| 96 | if (bufmap) |
Martin Brandenburg | b09d10d | 2015-12-15 14:54:27 -0500 | [diff] [blame] | 97 | size = bufmap->desc_size; |
Al Viro | 1780418 | 2016-02-13 11:16:37 -0500 | [diff] [blame^] | 98 | spin_unlock(&orangefs_bufmap_lock); |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 99 | return size; |
| 100 | } |
| 101 | |
Martin Brandenburg | 765a75b | 2015-12-15 14:48:17 -0500 | [diff] [blame] | 102 | int orangefs_bufmap_shift_query(void) |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 103 | { |
Martin Brandenburg | b09d10d | 2015-12-15 14:54:27 -0500 | [diff] [blame] | 104 | struct orangefs_bufmap *bufmap; |
| 105 | int shift = 0; |
Al Viro | 1780418 | 2016-02-13 11:16:37 -0500 | [diff] [blame^] | 106 | spin_lock(&orangefs_bufmap_lock); |
| 107 | bufmap = __orangefs_bufmap; |
| 108 | if (bufmap) |
Martin Brandenburg | b09d10d | 2015-12-15 14:54:27 -0500 | [diff] [blame] | 109 | shift = bufmap->desc_shift; |
Al Viro | 1780418 | 2016-02-13 11:16:37 -0500 | [diff] [blame^] | 110 | spin_unlock(&orangefs_bufmap_lock); |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 111 | return shift; |
| 112 | } |
| 113 | |
| 114 | static DECLARE_WAIT_QUEUE_HEAD(bufmap_waitq); |
| 115 | static DECLARE_WAIT_QUEUE_HEAD(readdir_waitq); |
| 116 | |
| 117 | /* |
Martin Brandenburg | 7d22148 | 2016-01-04 15:05:28 -0500 | [diff] [blame] | 118 | * orangefs_get_bufmap_init |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 119 | * |
| 120 | * If bufmap_init is 1, then the shared memory system, including the |
| 121 | * buffer_index_array, is available. Otherwise, it is not. |
| 122 | * |
| 123 | * returns the value of bufmap_init |
| 124 | */ |
Martin Brandenburg | 7d22148 | 2016-01-04 15:05:28 -0500 | [diff] [blame] | 125 | int orangefs_get_bufmap_init(void) |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 126 | { |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 127 | return __orangefs_bufmap ? 1 : 0; |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 131 | static struct orangefs_bufmap * |
| 132 | orangefs_bufmap_alloc(struct ORANGEFS_dev_map_desc *user_desc) |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 133 | { |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 134 | struct orangefs_bufmap *bufmap; |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 135 | |
| 136 | bufmap = kzalloc(sizeof(*bufmap), GFP_KERNEL); |
| 137 | if (!bufmap) |
| 138 | goto out; |
| 139 | |
| 140 | atomic_set(&bufmap->refcnt, 1); |
| 141 | bufmap->total_size = user_desc->total_size; |
| 142 | bufmap->desc_count = user_desc->count; |
| 143 | bufmap->desc_size = user_desc->size; |
| 144 | bufmap->desc_shift = ilog2(bufmap->desc_size); |
| 145 | |
| 146 | spin_lock_init(&bufmap->buffer_index_lock); |
| 147 | bufmap->buffer_index_array = |
| 148 | kcalloc(bufmap->desc_count, sizeof(int), GFP_KERNEL); |
| 149 | if (!bufmap->buffer_index_array) { |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 150 | gossip_err("orangefs: could not allocate %d buffer indices\n", |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 151 | bufmap->desc_count); |
| 152 | goto out_free_bufmap; |
| 153 | } |
| 154 | spin_lock_init(&bufmap->readdir_index_lock); |
| 155 | |
| 156 | bufmap->desc_array = |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 157 | kcalloc(bufmap->desc_count, sizeof(struct orangefs_bufmap_desc), |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 158 | GFP_KERNEL); |
| 159 | if (!bufmap->desc_array) { |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 160 | gossip_err("orangefs: could not allocate %d descriptors\n", |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 161 | bufmap->desc_count); |
| 162 | goto out_free_index_array; |
| 163 | } |
| 164 | |
| 165 | bufmap->page_count = bufmap->total_size / PAGE_SIZE; |
| 166 | |
| 167 | /* allocate storage to track our page mappings */ |
| 168 | bufmap->page_array = |
| 169 | kcalloc(bufmap->page_count, sizeof(struct page *), GFP_KERNEL); |
| 170 | if (!bufmap->page_array) |
| 171 | goto out_free_desc_array; |
| 172 | |
| 173 | return bufmap; |
| 174 | |
| 175 | out_free_desc_array: |
| 176 | kfree(bufmap->desc_array); |
| 177 | out_free_index_array: |
| 178 | kfree(bufmap->buffer_index_array); |
| 179 | out_free_bufmap: |
| 180 | kfree(bufmap); |
| 181 | out: |
| 182 | return NULL; |
| 183 | } |
| 184 | |
| 185 | static int |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 186 | orangefs_bufmap_map(struct orangefs_bufmap *bufmap, |
| 187 | struct ORANGEFS_dev_map_desc *user_desc) |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 188 | { |
| 189 | int pages_per_desc = bufmap->desc_size / PAGE_SIZE; |
| 190 | int offset = 0, ret, i; |
| 191 | |
| 192 | /* map the pages */ |
Al Viro | 16742f2 | 2015-10-08 20:10:00 -0400 | [diff] [blame] | 193 | ret = get_user_pages_fast((unsigned long)user_desc->ptr, |
| 194 | bufmap->page_count, 1, bufmap->page_array); |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 195 | |
| 196 | if (ret < 0) |
| 197 | return ret; |
| 198 | |
| 199 | if (ret != bufmap->page_count) { |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 200 | gossip_err("orangefs error: asked for %d pages, only got %d.\n", |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 201 | bufmap->page_count, ret); |
| 202 | |
| 203 | for (i = 0; i < ret; i++) { |
| 204 | SetPageError(bufmap->page_array[i]); |
| 205 | page_cache_release(bufmap->page_array[i]); |
| 206 | } |
| 207 | return -ENOMEM; |
| 208 | } |
| 209 | |
| 210 | /* |
| 211 | * ideally we want to get kernel space pointers for each page, but |
| 212 | * we can't kmap that many pages at once if highmem is being used. |
| 213 | * so instead, we just kmap/kunmap the page address each time the |
| 214 | * kaddr is needed. |
| 215 | */ |
| 216 | for (i = 0; i < bufmap->page_count; i++) |
| 217 | flush_dcache_page(bufmap->page_array[i]); |
| 218 | |
| 219 | /* build a list of available descriptors */ |
| 220 | for (offset = 0, i = 0; i < bufmap->desc_count; i++) { |
| 221 | bufmap->desc_array[i].page_array = &bufmap->page_array[offset]; |
| 222 | bufmap->desc_array[i].array_count = pages_per_desc; |
| 223 | bufmap->desc_array[i].uaddr = |
| 224 | (user_desc->ptr + (i * pages_per_desc * PAGE_SIZE)); |
| 225 | offset += pages_per_desc; |
| 226 | } |
| 227 | |
| 228 | return 0; |
| 229 | } |
| 230 | |
| 231 | /* |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 232 | * orangefs_bufmap_initialize() |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 233 | * |
| 234 | * initializes the mapped buffer interface |
| 235 | * |
| 236 | * returns 0 on success, -errno on failure |
| 237 | */ |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 238 | int orangefs_bufmap_initialize(struct ORANGEFS_dev_map_desc *user_desc) |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 239 | { |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 240 | struct orangefs_bufmap *bufmap; |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 241 | int ret = -EINVAL; |
| 242 | |
| 243 | gossip_debug(GOSSIP_BUFMAP_DEBUG, |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 244 | "orangefs_bufmap_initialize: called (ptr (" |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 245 | "%p) sz (%d) cnt(%d).\n", |
| 246 | user_desc->ptr, |
| 247 | user_desc->size, |
| 248 | user_desc->count); |
| 249 | |
| 250 | /* |
| 251 | * sanity check alignment and size of buffer that caller wants to |
| 252 | * work with |
| 253 | */ |
| 254 | if (PAGE_ALIGN((unsigned long)user_desc->ptr) != |
| 255 | (unsigned long)user_desc->ptr) { |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 256 | gossip_err("orangefs error: memory alignment (front). %p\n", |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 257 | user_desc->ptr); |
| 258 | goto out; |
| 259 | } |
| 260 | |
| 261 | if (PAGE_ALIGN(((unsigned long)user_desc->ptr + user_desc->total_size)) |
| 262 | != (unsigned long)(user_desc->ptr + user_desc->total_size)) { |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 263 | gossip_err("orangefs error: memory alignment (back).(%p + %d)\n", |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 264 | user_desc->ptr, |
| 265 | user_desc->total_size); |
| 266 | goto out; |
| 267 | } |
| 268 | |
| 269 | if (user_desc->total_size != (user_desc->size * user_desc->count)) { |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 270 | 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] | 271 | user_desc->total_size, |
| 272 | user_desc->size, |
| 273 | user_desc->count); |
| 274 | goto out; |
| 275 | } |
| 276 | |
| 277 | if ((user_desc->size % PAGE_SIZE) != 0) { |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 278 | gossip_err("orangefs error: bufmap size not page size divisible (%d).\n", |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 279 | user_desc->size); |
| 280 | goto out; |
| 281 | } |
| 282 | |
| 283 | ret = -ENOMEM; |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 284 | bufmap = orangefs_bufmap_alloc(user_desc); |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 285 | if (!bufmap) |
| 286 | goto out; |
| 287 | |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 288 | ret = orangefs_bufmap_map(bufmap, user_desc); |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 289 | if (ret) |
| 290 | goto out_free_bufmap; |
| 291 | |
| 292 | |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 293 | spin_lock(&orangefs_bufmap_lock); |
| 294 | if (__orangefs_bufmap) { |
| 295 | spin_unlock(&orangefs_bufmap_lock); |
| 296 | gossip_err("orangefs: error: bufmap already initialized.\n"); |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 297 | ret = -EALREADY; |
| 298 | goto out_unmap_bufmap; |
| 299 | } |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 300 | __orangefs_bufmap = bufmap; |
| 301 | spin_unlock(&orangefs_bufmap_lock); |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 302 | |
| 303 | /* |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 304 | * If there are operations in orangefs_bufmap_init_waitq, wake them up. |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 305 | * This scenario occurs when the client-core is restarted and I/O |
| 306 | * requests in the in-progress or waiting tables are restarted. I/O |
| 307 | * requests cannot be restarted until the shared memory system is |
| 308 | * completely re-initialized, so we put the I/O requests in this |
| 309 | * waitq until initialization has completed. NOTE: the I/O requests |
| 310 | * are also on a timer, so they don't wait forever just in case the |
| 311 | * client-core doesn't come back up. |
| 312 | */ |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 313 | wake_up_interruptible(&orangefs_bufmap_init_waitq); |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 314 | |
| 315 | gossip_debug(GOSSIP_BUFMAP_DEBUG, |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 316 | "orangefs_bufmap_initialize: exiting normally\n"); |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 317 | return 0; |
| 318 | |
| 319 | out_unmap_bufmap: |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 320 | orangefs_bufmap_unmap(bufmap); |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 321 | out_free_bufmap: |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 322 | orangefs_bufmap_free(bufmap); |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 323 | out: |
| 324 | return ret; |
| 325 | } |
| 326 | |
| 327 | /* |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 328 | * orangefs_bufmap_finalize() |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 329 | * |
| 330 | * shuts down the mapped buffer interface and releases any resources |
| 331 | * associated with it |
| 332 | * |
| 333 | * no return value |
| 334 | */ |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 335 | void orangefs_bufmap_finalize(void) |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 336 | { |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 337 | gossip_debug(GOSSIP_BUFMAP_DEBUG, "orangefs_bufmap_finalize: called\n"); |
| 338 | BUG_ON(!__orangefs_bufmap); |
| 339 | orangefs_bufmap_unref(__orangefs_bufmap); |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 340 | gossip_debug(GOSSIP_BUFMAP_DEBUG, |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 341 | "orangefs_bufmap_finalize: exiting normally\n"); |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | struct slot_args { |
| 345 | int slot_count; |
| 346 | int *slot_array; |
| 347 | spinlock_t *slot_lock; |
| 348 | wait_queue_head_t *slot_wq; |
| 349 | }; |
| 350 | |
| 351 | static int wait_for_a_slot(struct slot_args *slargs, int *buffer_index) |
| 352 | { |
| 353 | int ret = -1; |
| 354 | int i = 0; |
Mike Marshall | ce6c414 | 2015-12-14 14:54:46 -0500 | [diff] [blame] | 355 | DEFINE_WAIT(wait_entry); |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 356 | |
| 357 | while (1) { |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 358 | /* |
| 359 | * check for available desc, slot_lock is the appropriate |
| 360 | * index_lock |
| 361 | */ |
| 362 | spin_lock(slargs->slot_lock); |
Mike Marshall | ce6c414 | 2015-12-14 14:54:46 -0500 | [diff] [blame] | 363 | prepare_to_wait_exclusive(slargs->slot_wq, |
| 364 | &wait_entry, |
| 365 | TASK_INTERRUPTIBLE); |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 366 | for (i = 0; i < slargs->slot_count; i++) |
| 367 | if (slargs->slot_array[i] == 0) { |
| 368 | slargs->slot_array[i] = 1; |
| 369 | *buffer_index = i; |
| 370 | ret = 0; |
| 371 | break; |
| 372 | } |
| 373 | spin_unlock(slargs->slot_lock); |
| 374 | |
| 375 | /* if we acquired a buffer, then break out of while */ |
| 376 | if (ret == 0) |
| 377 | break; |
| 378 | |
| 379 | if (!signal_pending(current)) { |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 380 | gossip_debug(GOSSIP_BUFMAP_DEBUG, |
| 381 | "[BUFMAP]: waiting %d " |
| 382 | "seconds for a slot\n", |
| 383 | slot_timeout_secs); |
Al Viro | 727cbfe | 2016-01-23 13:17:55 -0500 | [diff] [blame] | 384 | if (!schedule_timeout(slot_timeout_secs * HZ)) { |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 385 | gossip_debug(GOSSIP_BUFMAP_DEBUG, |
| 386 | "*** wait_for_a_slot timed out\n"); |
| 387 | ret = -ETIMEDOUT; |
| 388 | break; |
| 389 | } |
| 390 | gossip_debug(GOSSIP_BUFMAP_DEBUG, |
| 391 | "[BUFMAP]: woken up by a slot becoming available.\n"); |
| 392 | continue; |
| 393 | } |
| 394 | |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 395 | gossip_debug(GOSSIP_BUFMAP_DEBUG, "orangefs: %s interrupted.\n", |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 396 | __func__); |
| 397 | ret = -EINTR; |
| 398 | break; |
| 399 | } |
| 400 | |
Mike Marshall | ce6c414 | 2015-12-14 14:54:46 -0500 | [diff] [blame] | 401 | spin_lock(slargs->slot_lock); |
| 402 | finish_wait(slargs->slot_wq, &wait_entry); |
| 403 | spin_unlock(slargs->slot_lock); |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 404 | return ret; |
| 405 | } |
| 406 | |
| 407 | static void put_back_slot(struct slot_args *slargs, int buffer_index) |
| 408 | { |
| 409 | /* slot_lock is the appropriate index_lock */ |
| 410 | spin_lock(slargs->slot_lock); |
| 411 | if (buffer_index < 0 || buffer_index >= slargs->slot_count) { |
| 412 | spin_unlock(slargs->slot_lock); |
| 413 | return; |
| 414 | } |
| 415 | |
| 416 | /* put the desc back on the queue */ |
| 417 | slargs->slot_array[buffer_index] = 0; |
| 418 | spin_unlock(slargs->slot_lock); |
| 419 | |
| 420 | /* wake up anyone who may be sleeping on the queue */ |
| 421 | wake_up_interruptible(slargs->slot_wq); |
| 422 | } |
| 423 | |
| 424 | /* |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 425 | * orangefs_bufmap_get() |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 426 | * |
| 427 | * gets a free mapped buffer descriptor, will sleep until one becomes |
| 428 | * available if necessary |
| 429 | * |
| 430 | * returns 0 on success, -errno on failure |
| 431 | */ |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 432 | int orangefs_bufmap_get(struct orangefs_bufmap **mapp, int *buffer_index) |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 433 | { |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 434 | struct orangefs_bufmap *bufmap = orangefs_bufmap_ref(); |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 435 | struct slot_args slargs; |
| 436 | int ret; |
| 437 | |
| 438 | if (!bufmap) { |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 439 | gossip_err("orangefs: please confirm that pvfs2-client daemon is running.\n"); |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 440 | return -EIO; |
| 441 | } |
| 442 | |
| 443 | slargs.slot_count = bufmap->desc_count; |
| 444 | slargs.slot_array = bufmap->buffer_index_array; |
| 445 | slargs.slot_lock = &bufmap->buffer_index_lock; |
| 446 | slargs.slot_wq = &bufmap_waitq; |
| 447 | ret = wait_for_a_slot(&slargs, buffer_index); |
| 448 | if (ret) |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 449 | orangefs_bufmap_unref(bufmap); |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 450 | *mapp = bufmap; |
| 451 | return ret; |
| 452 | } |
| 453 | |
| 454 | /* |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 455 | * orangefs_bufmap_put() |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 456 | * |
| 457 | * returns a mapped buffer descriptor to the collection |
| 458 | * |
| 459 | * no return value |
| 460 | */ |
Al Viro | 1357d06 | 2016-02-11 21:34:52 -0500 | [diff] [blame] | 461 | void orangefs_bufmap_put(int buffer_index) |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 462 | { |
| 463 | struct slot_args slargs; |
Al Viro | 1357d06 | 2016-02-11 21:34:52 -0500 | [diff] [blame] | 464 | struct orangefs_bufmap *bufmap = __orangefs_bufmap; |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 465 | |
| 466 | slargs.slot_count = bufmap->desc_count; |
| 467 | slargs.slot_array = bufmap->buffer_index_array; |
| 468 | slargs.slot_lock = &bufmap->buffer_index_lock; |
| 469 | slargs.slot_wq = &bufmap_waitq; |
| 470 | put_back_slot(&slargs, buffer_index); |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 471 | orangefs_bufmap_unref(bufmap); |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | /* |
Martin Brandenburg | 7d22148 | 2016-01-04 15:05:28 -0500 | [diff] [blame] | 475 | * orangefs_readdir_index_get() |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 476 | * |
| 477 | * gets a free descriptor, will sleep until one becomes |
| 478 | * available if necessary. |
| 479 | * Although the readdir buffers are not mapped into kernel space |
| 480 | * we could do that at a later point of time. Regardless, these |
| 481 | * indices are used by the client-core. |
| 482 | * |
| 483 | * returns 0 on success, -errno on failure |
| 484 | */ |
Martin Brandenburg | 7d22148 | 2016-01-04 15:05:28 -0500 | [diff] [blame] | 485 | int orangefs_readdir_index_get(struct orangefs_bufmap **mapp, int *buffer_index) |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 486 | { |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 487 | struct orangefs_bufmap *bufmap = orangefs_bufmap_ref(); |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 488 | struct slot_args slargs; |
| 489 | int ret; |
| 490 | |
| 491 | if (!bufmap) { |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 492 | gossip_err("orangefs: please confirm that pvfs2-client daemon is running.\n"); |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 493 | return -EIO; |
| 494 | } |
| 495 | |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 496 | slargs.slot_count = ORANGEFS_READDIR_DEFAULT_DESC_COUNT; |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 497 | slargs.slot_array = bufmap->readdir_index_array; |
| 498 | slargs.slot_lock = &bufmap->readdir_index_lock; |
| 499 | slargs.slot_wq = &readdir_waitq; |
| 500 | ret = wait_for_a_slot(&slargs, buffer_index); |
| 501 | if (ret) |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 502 | orangefs_bufmap_unref(bufmap); |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 503 | *mapp = bufmap; |
| 504 | return ret; |
| 505 | } |
| 506 | |
Martin Brandenburg | 7d22148 | 2016-01-04 15:05:28 -0500 | [diff] [blame] | 507 | void orangefs_readdir_index_put(struct orangefs_bufmap *bufmap, int buffer_index) |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 508 | { |
| 509 | struct slot_args slargs; |
| 510 | |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 511 | slargs.slot_count = ORANGEFS_READDIR_DEFAULT_DESC_COUNT; |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 512 | slargs.slot_array = bufmap->readdir_index_array; |
| 513 | slargs.slot_lock = &bufmap->readdir_index_lock; |
| 514 | slargs.slot_wq = &readdir_waitq; |
| 515 | put_back_slot(&slargs, buffer_index); |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 516 | orangefs_bufmap_unref(bufmap); |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 517 | } |
| 518 | |
Mike Marshall | b5e376e | 2015-12-11 10:50:42 -0500 | [diff] [blame] | 519 | /* |
| 520 | * we've been handed an iovec, we need to copy it to |
| 521 | * the shared memory descriptor at "buffer_index". |
| 522 | */ |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 523 | int orangefs_bufmap_copy_from_iovec(struct orangefs_bufmap *bufmap, |
Mike Marshall | 5480494 | 2015-10-05 13:44:24 -0400 | [diff] [blame] | 524 | struct iov_iter *iter, |
| 525 | int buffer_index, |
| 526 | size_t size) |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 527 | { |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 528 | struct orangefs_bufmap_desc *to = &bufmap->desc_array[buffer_index]; |
Mike Marshall | 4d1c440 | 2015-09-04 10:31:16 -0400 | [diff] [blame] | 529 | int i; |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 530 | |
| 531 | gossip_debug(GOSSIP_BUFMAP_DEBUG, |
Al Viro | 34204fd | 2015-10-08 17:47:44 -0400 | [diff] [blame] | 532 | "%s: buffer_index:%d: size:%zu:\n", |
Mike Marshall | 4d1c440 | 2015-09-04 10:31:16 -0400 | [diff] [blame] | 533 | __func__, buffer_index, size); |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 534 | |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 535 | |
Mike Marshall | 4d1c440 | 2015-09-04 10:31:16 -0400 | [diff] [blame] | 536 | for (i = 0; size; i++) { |
Al Viro | 34204fd | 2015-10-08 17:47:44 -0400 | [diff] [blame] | 537 | struct page *page = to->page_array[i]; |
| 538 | size_t n = size; |
| 539 | if (n > PAGE_SIZE) |
| 540 | n = PAGE_SIZE; |
| 541 | n = copy_page_from_iter(page, 0, n, iter); |
| 542 | if (!n) |
| 543 | return -EFAULT; |
| 544 | size -= n; |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 545 | } |
Al Viro | 34204fd | 2015-10-08 17:47:44 -0400 | [diff] [blame] | 546 | return 0; |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 547 | |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | /* |
Mike Marshall | b5e376e | 2015-12-11 10:50:42 -0500 | [diff] [blame] | 551 | * we've been handed an iovec, we need to fill it from |
| 552 | * the shared memory descriptor at "buffer_index". |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 553 | */ |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 554 | int orangefs_bufmap_copy_to_iovec(struct orangefs_bufmap *bufmap, |
Mike Marshall | 4d1c440 | 2015-09-04 10:31:16 -0400 | [diff] [blame] | 555 | struct iov_iter *iter, |
Al Viro | 5c27822 | 2015-10-08 17:43:58 -0400 | [diff] [blame] | 556 | int buffer_index, |
| 557 | size_t size) |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 558 | { |
Yi Liu | 8bb8aef | 2015-11-24 15:12:14 -0500 | [diff] [blame] | 559 | struct orangefs_bufmap_desc *from = &bufmap->desc_array[buffer_index]; |
Mike Marshall | 4d1c440 | 2015-09-04 10:31:16 -0400 | [diff] [blame] | 560 | int i; |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 561 | |
| 562 | gossip_debug(GOSSIP_BUFMAP_DEBUG, |
Al Viro | 5c27822 | 2015-10-08 17:43:58 -0400 | [diff] [blame] | 563 | "%s: buffer_index:%d: size:%zu:\n", |
| 564 | __func__, buffer_index, size); |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 565 | |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 566 | |
Al Viro | 5c27822 | 2015-10-08 17:43:58 -0400 | [diff] [blame] | 567 | for (i = 0; size; i++) { |
| 568 | struct page *page = from->page_array[i]; |
| 569 | size_t n = size; |
| 570 | if (n > PAGE_SIZE) |
| 571 | n = PAGE_SIZE; |
| 572 | n = copy_page_to_iter(page, 0, n, iter); |
| 573 | if (!n) |
| 574 | return -EFAULT; |
| 575 | size -= n; |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 576 | } |
Al Viro | 5c27822 | 2015-10-08 17:43:58 -0400 | [diff] [blame] | 577 | return 0; |
Mike Marshall | 274dcf5 | 2015-07-17 10:38:13 -0400 | [diff] [blame] | 578 | } |