blob: cd484665bf7250a83362e2ede6a3a68e9a7472bd [file] [log] [blame]
Mike Marshall274dcf52015-07-17 10:38:13 -04001/*
2 * (C) 2001 Clemson University and The University of Chicago
3 *
4 * See COPYING in top-level directory.
5 */
6#include "protocol.h"
Mike Marshall575e9462015-12-04 12:56:14 -05007#include "orangefs-kernel.h"
8#include "orangefs-bufmap.h"
Mike Marshall274dcf52015-07-17 10:38:13 -04009
Yi Liu8bb8aef2015-11-24 15:12:14 -050010DECLARE_WAIT_QUEUE_HEAD(orangefs_bufmap_init_waitq);
Mike Marshall274dcf52015-07-17 10:38:13 -040011
Martin Brandenburgbf89f582015-12-15 14:45:12 -050012/* used to describe mapped buffers */
13struct 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 Liu8bb8aef2015-11-24 15:12:14 -050020static struct orangefs_bufmap {
Mike Marshall274dcf52015-07-17 10:38:13 -040021 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 Liu8bb8aef2015-11-24 15:12:14 -050030 struct orangefs_bufmap_desc *desc_array;
Mike Marshall274dcf52015-07-17 10:38:13 -040031
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 Liu8bb8aef2015-11-24 15:12:14 -050037 int readdir_index_array[ORANGEFS_READDIR_DEFAULT_DESC_COUNT];
Mike Marshall274dcf52015-07-17 10:38:13 -040038 spinlock_t readdir_index_lock;
Yi Liu8bb8aef2015-11-24 15:12:14 -050039} *__orangefs_bufmap;
Mike Marshall274dcf52015-07-17 10:38:13 -040040
Yi Liu8bb8aef2015-11-24 15:12:14 -050041static DEFINE_SPINLOCK(orangefs_bufmap_lock);
Mike Marshall274dcf52015-07-17 10:38:13 -040042
43static void
Yi Liu8bb8aef2015-11-24 15:12:14 -050044orangefs_bufmap_unmap(struct orangefs_bufmap *bufmap)
Mike Marshall274dcf52015-07-17 10:38:13 -040045{
46 int i;
47
48 for (i = 0; i < bufmap->page_count; i++)
49 page_cache_release(bufmap->page_array[i]);
50}
51
52static void
Yi Liu8bb8aef2015-11-24 15:12:14 -050053orangefs_bufmap_free(struct orangefs_bufmap *bufmap)
Mike Marshall274dcf52015-07-17 10:38:13 -040054{
55 kfree(bufmap->page_array);
56 kfree(bufmap->desc_array);
57 kfree(bufmap->buffer_index_array);
58 kfree(bufmap);
59}
60
Martin Brandenburgbf89f582015-12-15 14:45:12 -050061static struct orangefs_bufmap *orangefs_bufmap_ref(void)
Mike Marshall274dcf52015-07-17 10:38:13 -040062{
Yi Liu8bb8aef2015-11-24 15:12:14 -050063 struct orangefs_bufmap *bufmap = NULL;
Mike Marshall274dcf52015-07-17 10:38:13 -040064
Yi Liu8bb8aef2015-11-24 15:12:14 -050065 spin_lock(&orangefs_bufmap_lock);
66 if (__orangefs_bufmap) {
67 bufmap = __orangefs_bufmap;
Mike Marshall274dcf52015-07-17 10:38:13 -040068 atomic_inc(&bufmap->refcnt);
69 }
Yi Liu8bb8aef2015-11-24 15:12:14 -050070 spin_unlock(&orangefs_bufmap_lock);
Mike Marshall274dcf52015-07-17 10:38:13 -040071 return bufmap;
72}
73
Martin Brandenburgbf89f582015-12-15 14:45:12 -050074static void orangefs_bufmap_unref(struct orangefs_bufmap *bufmap)
Mike Marshall274dcf52015-07-17 10:38:13 -040075{
Yi Liu8bb8aef2015-11-24 15:12:14 -050076 if (atomic_dec_and_lock(&bufmap->refcnt, &orangefs_bufmap_lock)) {
77 __orangefs_bufmap = NULL;
78 spin_unlock(&orangefs_bufmap_lock);
Mike Marshall274dcf52015-07-17 10:38:13 -040079
Yi Liu8bb8aef2015-11-24 15:12:14 -050080 orangefs_bufmap_unmap(bufmap);
81 orangefs_bufmap_free(bufmap);
Mike Marshall274dcf52015-07-17 10:38:13 -040082 }
83}
84
Martin Brandenburgb09d10d2015-12-15 14:54:27 -050085/*
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 Brandenburg765a75b2015-12-15 14:48:17 -050090int orangefs_bufmap_size_query(void)
Mike Marshall274dcf52015-07-17 10:38:13 -040091{
Martin Brandenburgb09d10d2015-12-15 14:54:27 -050092 struct orangefs_bufmap *bufmap;
93 int size = 0;
Al Viro17804182016-02-13 11:16:37 -050094 spin_lock(&orangefs_bufmap_lock);
95 bufmap = __orangefs_bufmap;
96 if (bufmap)
Martin Brandenburgb09d10d2015-12-15 14:54:27 -050097 size = bufmap->desc_size;
Al Viro17804182016-02-13 11:16:37 -050098 spin_unlock(&orangefs_bufmap_lock);
Mike Marshall274dcf52015-07-17 10:38:13 -040099 return size;
100}
101
Martin Brandenburg765a75b2015-12-15 14:48:17 -0500102int orangefs_bufmap_shift_query(void)
Mike Marshall274dcf52015-07-17 10:38:13 -0400103{
Martin Brandenburgb09d10d2015-12-15 14:54:27 -0500104 struct orangefs_bufmap *bufmap;
105 int shift = 0;
Al Viro17804182016-02-13 11:16:37 -0500106 spin_lock(&orangefs_bufmap_lock);
107 bufmap = __orangefs_bufmap;
108 if (bufmap)
Martin Brandenburgb09d10d2015-12-15 14:54:27 -0500109 shift = bufmap->desc_shift;
Al Viro17804182016-02-13 11:16:37 -0500110 spin_unlock(&orangefs_bufmap_lock);
Mike Marshall274dcf52015-07-17 10:38:13 -0400111 return shift;
112}
113
114static DECLARE_WAIT_QUEUE_HEAD(bufmap_waitq);
115static DECLARE_WAIT_QUEUE_HEAD(readdir_waitq);
116
117/*
Martin Brandenburg7d221482016-01-04 15:05:28 -0500118 * orangefs_get_bufmap_init
Mike Marshall274dcf52015-07-17 10:38:13 -0400119 *
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 Brandenburg7d221482016-01-04 15:05:28 -0500125int orangefs_get_bufmap_init(void)
Mike Marshall274dcf52015-07-17 10:38:13 -0400126{
Yi Liu8bb8aef2015-11-24 15:12:14 -0500127 return __orangefs_bufmap ? 1 : 0;
Mike Marshall274dcf52015-07-17 10:38:13 -0400128}
129
130
Yi Liu8bb8aef2015-11-24 15:12:14 -0500131static struct orangefs_bufmap *
132orangefs_bufmap_alloc(struct ORANGEFS_dev_map_desc *user_desc)
Mike Marshall274dcf52015-07-17 10:38:13 -0400133{
Yi Liu8bb8aef2015-11-24 15:12:14 -0500134 struct orangefs_bufmap *bufmap;
Mike Marshall274dcf52015-07-17 10:38:13 -0400135
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 Liu8bb8aef2015-11-24 15:12:14 -0500150 gossip_err("orangefs: could not allocate %d buffer indices\n",
Mike Marshall274dcf52015-07-17 10:38:13 -0400151 bufmap->desc_count);
152 goto out_free_bufmap;
153 }
154 spin_lock_init(&bufmap->readdir_index_lock);
155
156 bufmap->desc_array =
Yi Liu8bb8aef2015-11-24 15:12:14 -0500157 kcalloc(bufmap->desc_count, sizeof(struct orangefs_bufmap_desc),
Mike Marshall274dcf52015-07-17 10:38:13 -0400158 GFP_KERNEL);
159 if (!bufmap->desc_array) {
Yi Liu8bb8aef2015-11-24 15:12:14 -0500160 gossip_err("orangefs: could not allocate %d descriptors\n",
Mike Marshall274dcf52015-07-17 10:38:13 -0400161 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
175out_free_desc_array:
176 kfree(bufmap->desc_array);
177out_free_index_array:
178 kfree(bufmap->buffer_index_array);
179out_free_bufmap:
180 kfree(bufmap);
181out:
182 return NULL;
183}
184
185static int
Yi Liu8bb8aef2015-11-24 15:12:14 -0500186orangefs_bufmap_map(struct orangefs_bufmap *bufmap,
187 struct ORANGEFS_dev_map_desc *user_desc)
Mike Marshall274dcf52015-07-17 10:38:13 -0400188{
189 int pages_per_desc = bufmap->desc_size / PAGE_SIZE;
190 int offset = 0, ret, i;
191
192 /* map the pages */
Al Viro16742f22015-10-08 20:10:00 -0400193 ret = get_user_pages_fast((unsigned long)user_desc->ptr,
194 bufmap->page_count, 1, bufmap->page_array);
Mike Marshall274dcf52015-07-17 10:38:13 -0400195
196 if (ret < 0)
197 return ret;
198
199 if (ret != bufmap->page_count) {
Yi Liu8bb8aef2015-11-24 15:12:14 -0500200 gossip_err("orangefs error: asked for %d pages, only got %d.\n",
Mike Marshall274dcf52015-07-17 10:38:13 -0400201 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 Liu8bb8aef2015-11-24 15:12:14 -0500232 * orangefs_bufmap_initialize()
Mike Marshall274dcf52015-07-17 10:38:13 -0400233 *
234 * initializes the mapped buffer interface
235 *
236 * returns 0 on success, -errno on failure
237 */
Yi Liu8bb8aef2015-11-24 15:12:14 -0500238int orangefs_bufmap_initialize(struct ORANGEFS_dev_map_desc *user_desc)
Mike Marshall274dcf52015-07-17 10:38:13 -0400239{
Yi Liu8bb8aef2015-11-24 15:12:14 -0500240 struct orangefs_bufmap *bufmap;
Mike Marshall274dcf52015-07-17 10:38:13 -0400241 int ret = -EINVAL;
242
243 gossip_debug(GOSSIP_BUFMAP_DEBUG,
Yi Liu8bb8aef2015-11-24 15:12:14 -0500244 "orangefs_bufmap_initialize: called (ptr ("
Mike Marshall274dcf52015-07-17 10:38:13 -0400245 "%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 Liu8bb8aef2015-11-24 15:12:14 -0500256 gossip_err("orangefs error: memory alignment (front). %p\n",
Mike Marshall274dcf52015-07-17 10:38:13 -0400257 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 Liu8bb8aef2015-11-24 15:12:14 -0500263 gossip_err("orangefs error: memory alignment (back).(%p + %d)\n",
Mike Marshall274dcf52015-07-17 10:38:13 -0400264 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 Liu8bb8aef2015-11-24 15:12:14 -0500270 gossip_err("orangefs error: user provided an oddly sized buffer: (%d, %d, %d)\n",
Mike Marshall274dcf52015-07-17 10:38:13 -0400271 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 Liu8bb8aef2015-11-24 15:12:14 -0500278 gossip_err("orangefs error: bufmap size not page size divisible (%d).\n",
Mike Marshall274dcf52015-07-17 10:38:13 -0400279 user_desc->size);
280 goto out;
281 }
282
283 ret = -ENOMEM;
Yi Liu8bb8aef2015-11-24 15:12:14 -0500284 bufmap = orangefs_bufmap_alloc(user_desc);
Mike Marshall274dcf52015-07-17 10:38:13 -0400285 if (!bufmap)
286 goto out;
287
Yi Liu8bb8aef2015-11-24 15:12:14 -0500288 ret = orangefs_bufmap_map(bufmap, user_desc);
Mike Marshall274dcf52015-07-17 10:38:13 -0400289 if (ret)
290 goto out_free_bufmap;
291
292
Yi Liu8bb8aef2015-11-24 15:12:14 -0500293 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 Marshall274dcf52015-07-17 10:38:13 -0400297 ret = -EALREADY;
298 goto out_unmap_bufmap;
299 }
Yi Liu8bb8aef2015-11-24 15:12:14 -0500300 __orangefs_bufmap = bufmap;
301 spin_unlock(&orangefs_bufmap_lock);
Mike Marshall274dcf52015-07-17 10:38:13 -0400302
303 /*
Yi Liu8bb8aef2015-11-24 15:12:14 -0500304 * If there are operations in orangefs_bufmap_init_waitq, wake them up.
Mike Marshall274dcf52015-07-17 10:38:13 -0400305 * 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 Liu8bb8aef2015-11-24 15:12:14 -0500313 wake_up_interruptible(&orangefs_bufmap_init_waitq);
Mike Marshall274dcf52015-07-17 10:38:13 -0400314
315 gossip_debug(GOSSIP_BUFMAP_DEBUG,
Yi Liu8bb8aef2015-11-24 15:12:14 -0500316 "orangefs_bufmap_initialize: exiting normally\n");
Mike Marshall274dcf52015-07-17 10:38:13 -0400317 return 0;
318
319out_unmap_bufmap:
Yi Liu8bb8aef2015-11-24 15:12:14 -0500320 orangefs_bufmap_unmap(bufmap);
Mike Marshall274dcf52015-07-17 10:38:13 -0400321out_free_bufmap:
Yi Liu8bb8aef2015-11-24 15:12:14 -0500322 orangefs_bufmap_free(bufmap);
Mike Marshall274dcf52015-07-17 10:38:13 -0400323out:
324 return ret;
325}
326
327/*
Yi Liu8bb8aef2015-11-24 15:12:14 -0500328 * orangefs_bufmap_finalize()
Mike Marshall274dcf52015-07-17 10:38:13 -0400329 *
330 * shuts down the mapped buffer interface and releases any resources
331 * associated with it
332 *
333 * no return value
334 */
Yi Liu8bb8aef2015-11-24 15:12:14 -0500335void orangefs_bufmap_finalize(void)
Mike Marshall274dcf52015-07-17 10:38:13 -0400336{
Yi Liu8bb8aef2015-11-24 15:12:14 -0500337 gossip_debug(GOSSIP_BUFMAP_DEBUG, "orangefs_bufmap_finalize: called\n");
338 BUG_ON(!__orangefs_bufmap);
339 orangefs_bufmap_unref(__orangefs_bufmap);
Mike Marshall274dcf52015-07-17 10:38:13 -0400340 gossip_debug(GOSSIP_BUFMAP_DEBUG,
Yi Liu8bb8aef2015-11-24 15:12:14 -0500341 "orangefs_bufmap_finalize: exiting normally\n");
Mike Marshall274dcf52015-07-17 10:38:13 -0400342}
343
344struct slot_args {
345 int slot_count;
346 int *slot_array;
347 spinlock_t *slot_lock;
348 wait_queue_head_t *slot_wq;
349};
350
351static int wait_for_a_slot(struct slot_args *slargs, int *buffer_index)
352{
353 int ret = -1;
354 int i = 0;
Mike Marshallce6c4142015-12-14 14:54:46 -0500355 DEFINE_WAIT(wait_entry);
Mike Marshall274dcf52015-07-17 10:38:13 -0400356
357 while (1) {
Mike Marshall274dcf52015-07-17 10:38:13 -0400358 /*
359 * check for available desc, slot_lock is the appropriate
360 * index_lock
361 */
362 spin_lock(slargs->slot_lock);
Mike Marshallce6c4142015-12-14 14:54:46 -0500363 prepare_to_wait_exclusive(slargs->slot_wq,
364 &wait_entry,
365 TASK_INTERRUPTIBLE);
Mike Marshall274dcf52015-07-17 10:38:13 -0400366 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 Marshall274dcf52015-07-17 10:38:13 -0400380 gossip_debug(GOSSIP_BUFMAP_DEBUG,
381 "[BUFMAP]: waiting %d "
382 "seconds for a slot\n",
383 slot_timeout_secs);
Al Viro727cbfe2016-01-23 13:17:55 -0500384 if (!schedule_timeout(slot_timeout_secs * HZ)) {
Mike Marshall274dcf52015-07-17 10:38:13 -0400385 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 Liu8bb8aef2015-11-24 15:12:14 -0500395 gossip_debug(GOSSIP_BUFMAP_DEBUG, "orangefs: %s interrupted.\n",
Mike Marshall274dcf52015-07-17 10:38:13 -0400396 __func__);
397 ret = -EINTR;
398 break;
399 }
400
Mike Marshallce6c4142015-12-14 14:54:46 -0500401 spin_lock(slargs->slot_lock);
402 finish_wait(slargs->slot_wq, &wait_entry);
403 spin_unlock(slargs->slot_lock);
Mike Marshall274dcf52015-07-17 10:38:13 -0400404 return ret;
405}
406
407static 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 Liu8bb8aef2015-11-24 15:12:14 -0500425 * orangefs_bufmap_get()
Mike Marshall274dcf52015-07-17 10:38:13 -0400426 *
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 Liu8bb8aef2015-11-24 15:12:14 -0500432int orangefs_bufmap_get(struct orangefs_bufmap **mapp, int *buffer_index)
Mike Marshall274dcf52015-07-17 10:38:13 -0400433{
Yi Liu8bb8aef2015-11-24 15:12:14 -0500434 struct orangefs_bufmap *bufmap = orangefs_bufmap_ref();
Mike Marshall274dcf52015-07-17 10:38:13 -0400435 struct slot_args slargs;
436 int ret;
437
438 if (!bufmap) {
Yi Liu8bb8aef2015-11-24 15:12:14 -0500439 gossip_err("orangefs: please confirm that pvfs2-client daemon is running.\n");
Mike Marshall274dcf52015-07-17 10:38:13 -0400440 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 Liu8bb8aef2015-11-24 15:12:14 -0500449 orangefs_bufmap_unref(bufmap);
Mike Marshall274dcf52015-07-17 10:38:13 -0400450 *mapp = bufmap;
451 return ret;
452}
453
454/*
Yi Liu8bb8aef2015-11-24 15:12:14 -0500455 * orangefs_bufmap_put()
Mike Marshall274dcf52015-07-17 10:38:13 -0400456 *
457 * returns a mapped buffer descriptor to the collection
458 *
459 * no return value
460 */
Al Viro1357d062016-02-11 21:34:52 -0500461void orangefs_bufmap_put(int buffer_index)
Mike Marshall274dcf52015-07-17 10:38:13 -0400462{
463 struct slot_args slargs;
Al Viro1357d062016-02-11 21:34:52 -0500464 struct orangefs_bufmap *bufmap = __orangefs_bufmap;
Mike Marshall274dcf52015-07-17 10:38:13 -0400465
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 Liu8bb8aef2015-11-24 15:12:14 -0500471 orangefs_bufmap_unref(bufmap);
Mike Marshall274dcf52015-07-17 10:38:13 -0400472}
473
474/*
Martin Brandenburg7d221482016-01-04 15:05:28 -0500475 * orangefs_readdir_index_get()
Mike Marshall274dcf52015-07-17 10:38:13 -0400476 *
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 Brandenburg7d221482016-01-04 15:05:28 -0500485int orangefs_readdir_index_get(struct orangefs_bufmap **mapp, int *buffer_index)
Mike Marshall274dcf52015-07-17 10:38:13 -0400486{
Yi Liu8bb8aef2015-11-24 15:12:14 -0500487 struct orangefs_bufmap *bufmap = orangefs_bufmap_ref();
Mike Marshall274dcf52015-07-17 10:38:13 -0400488 struct slot_args slargs;
489 int ret;
490
491 if (!bufmap) {
Yi Liu8bb8aef2015-11-24 15:12:14 -0500492 gossip_err("orangefs: please confirm that pvfs2-client daemon is running.\n");
Mike Marshall274dcf52015-07-17 10:38:13 -0400493 return -EIO;
494 }
495
Yi Liu8bb8aef2015-11-24 15:12:14 -0500496 slargs.slot_count = ORANGEFS_READDIR_DEFAULT_DESC_COUNT;
Mike Marshall274dcf52015-07-17 10:38:13 -0400497 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 Liu8bb8aef2015-11-24 15:12:14 -0500502 orangefs_bufmap_unref(bufmap);
Mike Marshall274dcf52015-07-17 10:38:13 -0400503 *mapp = bufmap;
504 return ret;
505}
506
Martin Brandenburg7d221482016-01-04 15:05:28 -0500507void orangefs_readdir_index_put(struct orangefs_bufmap *bufmap, int buffer_index)
Mike Marshall274dcf52015-07-17 10:38:13 -0400508{
509 struct slot_args slargs;
510
Yi Liu8bb8aef2015-11-24 15:12:14 -0500511 slargs.slot_count = ORANGEFS_READDIR_DEFAULT_DESC_COUNT;
Mike Marshall274dcf52015-07-17 10:38:13 -0400512 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 Liu8bb8aef2015-11-24 15:12:14 -0500516 orangefs_bufmap_unref(bufmap);
Mike Marshall274dcf52015-07-17 10:38:13 -0400517}
518
Mike Marshallb5e376e2015-12-11 10:50:42 -0500519/*
520 * we've been handed an iovec, we need to copy it to
521 * the shared memory descriptor at "buffer_index".
522 */
Yi Liu8bb8aef2015-11-24 15:12:14 -0500523int orangefs_bufmap_copy_from_iovec(struct orangefs_bufmap *bufmap,
Mike Marshall54804942015-10-05 13:44:24 -0400524 struct iov_iter *iter,
525 int buffer_index,
526 size_t size)
Mike Marshall274dcf52015-07-17 10:38:13 -0400527{
Yi Liu8bb8aef2015-11-24 15:12:14 -0500528 struct orangefs_bufmap_desc *to = &bufmap->desc_array[buffer_index];
Mike Marshall4d1c4402015-09-04 10:31:16 -0400529 int i;
Mike Marshall274dcf52015-07-17 10:38:13 -0400530
531 gossip_debug(GOSSIP_BUFMAP_DEBUG,
Al Viro34204fd2015-10-08 17:47:44 -0400532 "%s: buffer_index:%d: size:%zu:\n",
Mike Marshall4d1c4402015-09-04 10:31:16 -0400533 __func__, buffer_index, size);
Mike Marshall274dcf52015-07-17 10:38:13 -0400534
Mike Marshall274dcf52015-07-17 10:38:13 -0400535
Mike Marshall4d1c4402015-09-04 10:31:16 -0400536 for (i = 0; size; i++) {
Al Viro34204fd2015-10-08 17:47:44 -0400537 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 Marshall274dcf52015-07-17 10:38:13 -0400545 }
Al Viro34204fd2015-10-08 17:47:44 -0400546 return 0;
Mike Marshall274dcf52015-07-17 10:38:13 -0400547
Mike Marshall274dcf52015-07-17 10:38:13 -0400548}
549
550/*
Mike Marshallb5e376e2015-12-11 10:50:42 -0500551 * we've been handed an iovec, we need to fill it from
552 * the shared memory descriptor at "buffer_index".
Mike Marshall274dcf52015-07-17 10:38:13 -0400553 */
Yi Liu8bb8aef2015-11-24 15:12:14 -0500554int orangefs_bufmap_copy_to_iovec(struct orangefs_bufmap *bufmap,
Mike Marshall4d1c4402015-09-04 10:31:16 -0400555 struct iov_iter *iter,
Al Viro5c278222015-10-08 17:43:58 -0400556 int buffer_index,
557 size_t size)
Mike Marshall274dcf52015-07-17 10:38:13 -0400558{
Yi Liu8bb8aef2015-11-24 15:12:14 -0500559 struct orangefs_bufmap_desc *from = &bufmap->desc_array[buffer_index];
Mike Marshall4d1c4402015-09-04 10:31:16 -0400560 int i;
Mike Marshall274dcf52015-07-17 10:38:13 -0400561
562 gossip_debug(GOSSIP_BUFMAP_DEBUG,
Al Viro5c278222015-10-08 17:43:58 -0400563 "%s: buffer_index:%d: size:%zu:\n",
564 __func__, buffer_index, size);
Mike Marshall274dcf52015-07-17 10:38:13 -0400565
Mike Marshall274dcf52015-07-17 10:38:13 -0400566
Al Viro5c278222015-10-08 17:43:58 -0400567 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 Marshall274dcf52015-07-17 10:38:13 -0400576 }
Al Viro5c278222015-10-08 17:43:58 -0400577 return 0;
Mike Marshall274dcf52015-07-17 10:38:13 -0400578}