fbarray: add reverse finding
[dpdk.git] / lib / librte_eal / common / eal_common_fbarray.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2017-2018 Intel Corporation
3  */
4
5 #include <inttypes.h>
6 #include <limits.h>
7 #include <sys/mman.h>
8 #include <stdint.h>
9 #include <errno.h>
10 #include <sys/file.h>
11 #include <string.h>
12
13 #include <rte_common.h>
14 #include <rte_log.h>
15 #include <rte_errno.h>
16 #include <rte_spinlock.h>
17 #include <rte_tailq.h>
18
19 #include "eal_filesystem.h"
20 #include "eal_private.h"
21
22 #include "rte_fbarray.h"
23
24 #define MASK_SHIFT 6ULL
25 #define MASK_ALIGN (1ULL << MASK_SHIFT)
26 #define MASK_LEN_TO_IDX(x) ((x) >> MASK_SHIFT)
27 #define MASK_LEN_TO_MOD(x) ((x) - RTE_ALIGN_FLOOR(x, MASK_ALIGN))
28 #define MASK_GET_IDX(idx, mod) ((idx << MASK_SHIFT) + mod)
29
30 /*
31  * This is a mask that is always stored at the end of array, to provide fast
32  * way of finding free/used spots without looping through each element.
33  */
34
35 struct used_mask {
36         unsigned int n_masks;
37         uint64_t data[];
38 };
39
40 static size_t
41 calc_mask_size(unsigned int len)
42 {
43         /* mask must be multiple of MASK_ALIGN, even though length of array
44          * itself may not be aligned on that boundary.
45          */
46         len = RTE_ALIGN_CEIL(len, MASK_ALIGN);
47         return sizeof(struct used_mask) +
48                         sizeof(uint64_t) * MASK_LEN_TO_IDX(len);
49 }
50
51 static size_t
52 calc_data_size(size_t page_sz, unsigned int elt_sz, unsigned int len)
53 {
54         size_t data_sz = elt_sz * len;
55         size_t msk_sz = calc_mask_size(len);
56         return RTE_ALIGN_CEIL(data_sz + msk_sz, page_sz);
57 }
58
59 static struct used_mask *
60 get_used_mask(void *data, unsigned int elt_sz, unsigned int len)
61 {
62         return (struct used_mask *) RTE_PTR_ADD(data, elt_sz * len);
63 }
64
65 static int
66 resize_and_map(int fd, void *addr, size_t len)
67 {
68         char path[PATH_MAX];
69         void *map_addr;
70
71         if (ftruncate(fd, len)) {
72                 RTE_LOG(ERR, EAL, "Cannot truncate %s\n", path);
73                 /* pass errno up the chain */
74                 rte_errno = errno;
75                 return -1;
76         }
77
78         map_addr = mmap(addr, len, PROT_READ | PROT_WRITE,
79                         MAP_SHARED | MAP_FIXED, fd, 0);
80         if (map_addr != addr) {
81                 RTE_LOG(ERR, EAL, "mmap() failed: %s\n", strerror(errno));
82                 /* pass errno up the chain */
83                 rte_errno = errno;
84                 return -1;
85         }
86         return 0;
87 }
88
89 static int
90 find_next_n(const struct rte_fbarray *arr, unsigned int start, unsigned int n,
91             bool used)
92 {
93         const struct used_mask *msk = get_used_mask(arr->data, arr->elt_sz,
94                         arr->len);
95         unsigned int msk_idx, lookahead_idx, first, first_mod;
96         unsigned int last, last_mod;
97         uint64_t last_msk, ignore_msk;
98
99         /*
100          * mask only has granularity of MASK_ALIGN, but start may not be aligned
101          * on that boundary, so construct a special mask to exclude anything we
102          * don't want to see to avoid confusing ctz.
103          */
104         first = MASK_LEN_TO_IDX(start);
105         first_mod = MASK_LEN_TO_MOD(start);
106         ignore_msk = ~((1ULL << first_mod) - 1);
107
108         /* array length may not be aligned, so calculate ignore mask for last
109          * mask index.
110          */
111         last = MASK_LEN_TO_IDX(arr->len);
112         last_mod = MASK_LEN_TO_MOD(arr->len);
113         last_msk = ~(-1ULL << last_mod);
114
115         for (msk_idx = first; msk_idx < msk->n_masks; msk_idx++) {
116                 uint64_t cur_msk, lookahead_msk;
117                 unsigned int run_start, clz, left;
118                 bool found = false;
119                 /*
120                  * The process of getting n consecutive bits for arbitrary n is
121                  * a bit involved, but here it is in a nutshell:
122                  *
123                  *  1. let n be the number of consecutive bits we're looking for
124                  *  2. check if n can fit in one mask, and if so, do n-1
125                  *     rshift-ands to see if there is an appropriate run inside
126                  *     our current mask
127                  *    2a. if we found a run, bail out early
128                  *    2b. if we didn't find a run, proceed
129                  *  3. invert the mask and count leading zeroes (that is, count
130                  *     how many consecutive set bits we had starting from the
131                  *     end of current mask) as k
132                  *    3a. if k is 0, continue to next mask
133                  *    3b. if k is not 0, we have a potential run
134                  *  4. to satisfy our requirements, next mask must have n-k
135                  *     consecutive set bits right at the start, so we will do
136                  *     (n-k-1) rshift-ands and check if first bit is set.
137                  *
138                  * Step 4 will need to be repeated if (n-k) > MASK_ALIGN until
139                  * we either run out of masks, lose the run, or find what we
140                  * were looking for.
141                  */
142                 cur_msk = msk->data[msk_idx];
143                 left = n;
144
145                 /* if we're looking for free spaces, invert the mask */
146                 if (!used)
147                         cur_msk = ~cur_msk;
148
149                 /* combine current ignore mask with last index ignore mask */
150                 if (msk_idx == last)
151                         ignore_msk |= last_msk;
152
153                 /* if we have an ignore mask, ignore once */
154                 if (ignore_msk) {
155                         cur_msk &= ignore_msk;
156                         ignore_msk = 0;
157                 }
158
159                 /* if n can fit in within a single mask, do a search */
160                 if (n <= MASK_ALIGN) {
161                         uint64_t tmp_msk = cur_msk;
162                         unsigned int s_idx;
163                         for (s_idx = 0; s_idx < n - 1; s_idx++)
164                                 tmp_msk &= tmp_msk >> 1ULL;
165                         /* we found what we were looking for */
166                         if (tmp_msk != 0) {
167                                 run_start = __builtin_ctzll(tmp_msk);
168                                 return MASK_GET_IDX(msk_idx, run_start);
169                         }
170                 }
171
172                 /*
173                  * we didn't find our run within the mask, or n > MASK_ALIGN,
174                  * so we're going for plan B.
175                  */
176
177                 /* count leading zeroes on inverted mask */
178                 if (~cur_msk == 0)
179                         clz = sizeof(cur_msk) * 8;
180                 else
181                         clz = __builtin_clzll(~cur_msk);
182
183                 /* if there aren't any runs at the end either, just continue */
184                 if (clz == 0)
185                         continue;
186
187                 /* we have a partial run at the end, so try looking ahead */
188                 run_start = MASK_ALIGN - clz;
189                 left -= clz;
190
191                 for (lookahead_idx = msk_idx + 1; lookahead_idx < msk->n_masks;
192                                 lookahead_idx++) {
193                         unsigned int s_idx, need;
194                         lookahead_msk = msk->data[lookahead_idx];
195
196                         /* if we're looking for free space, invert the mask */
197                         if (!used)
198                                 lookahead_msk = ~lookahead_msk;
199
200                         /* figure out how many consecutive bits we need here */
201                         need = RTE_MIN(left, MASK_ALIGN);
202
203                         for (s_idx = 0; s_idx < need - 1; s_idx++)
204                                 lookahead_msk &= lookahead_msk >> 1ULL;
205
206                         /* if first bit is not set, we've lost the run */
207                         if ((lookahead_msk & 1) == 0) {
208                                 /*
209                                  * we've scanned this far, so we know there are
210                                  * no runs in the space we've lookahead-scanned
211                                  * as well, so skip that on next iteration.
212                                  */
213                                 ignore_msk = ~((1ULL << need) - 1);
214                                 msk_idx = lookahead_idx;
215                                 break;
216                         }
217
218                         left -= need;
219
220                         /* check if we've found what we were looking for */
221                         if (left == 0) {
222                                 found = true;
223                                 break;
224                         }
225                 }
226
227                 /* we didn't find anything, so continue */
228                 if (!found)
229                         continue;
230
231                 return MASK_GET_IDX(msk_idx, run_start);
232         }
233         /* we didn't find anything */
234         rte_errno = used ? ENOENT : ENOSPC;
235         return -1;
236 }
237
238 static int
239 find_next(const struct rte_fbarray *arr, unsigned int start, bool used)
240 {
241         const struct used_mask *msk = get_used_mask(arr->data, arr->elt_sz,
242                         arr->len);
243         unsigned int idx, first, first_mod;
244         unsigned int last, last_mod;
245         uint64_t last_msk, ignore_msk;
246
247         /*
248          * mask only has granularity of MASK_ALIGN, but start may not be aligned
249          * on that boundary, so construct a special mask to exclude anything we
250          * don't want to see to avoid confusing ctz.
251          */
252         first = MASK_LEN_TO_IDX(start);
253         first_mod = MASK_LEN_TO_MOD(start);
254         ignore_msk = ~((1ULL << first_mod) - 1ULL);
255
256         /* array length may not be aligned, so calculate ignore mask for last
257          * mask index.
258          */
259         last = MASK_LEN_TO_IDX(arr->len);
260         last_mod = MASK_LEN_TO_MOD(arr->len);
261         last_msk = ~(-(1ULL) << last_mod);
262
263         for (idx = first; idx < msk->n_masks; idx++) {
264                 uint64_t cur = msk->data[idx];
265                 int found;
266
267                 /* if we're looking for free entries, invert mask */
268                 if (!used)
269                         cur = ~cur;
270
271                 if (idx == last)
272                         cur &= last_msk;
273
274                 /* ignore everything before start on first iteration */
275                 if (idx == first)
276                         cur &= ignore_msk;
277
278                 /* check if we have any entries */
279                 if (cur == 0)
280                         continue;
281
282                 /*
283                  * find first set bit - that will correspond to whatever it is
284                  * that we're looking for.
285                  */
286                 found = __builtin_ctzll(cur);
287                 return MASK_GET_IDX(idx, found);
288         }
289         /* we didn't find anything */
290         rte_errno = used ? ENOENT : ENOSPC;
291         return -1;
292 }
293
294 static int
295 find_contig(const struct rte_fbarray *arr, unsigned int start, bool used)
296 {
297         const struct used_mask *msk = get_used_mask(arr->data, arr->elt_sz,
298                         arr->len);
299         unsigned int idx, first, first_mod;
300         unsigned int last, last_mod;
301         uint64_t last_msk;
302         unsigned int need_len, result = 0;
303
304         /* array length may not be aligned, so calculate ignore mask for last
305          * mask index.
306          */
307         last = MASK_LEN_TO_IDX(arr->len);
308         last_mod = MASK_LEN_TO_MOD(arr->len);
309         last_msk = ~(-(1ULL) << last_mod);
310
311         first = MASK_LEN_TO_IDX(start);
312         first_mod = MASK_LEN_TO_MOD(start);
313         for (idx = first; idx < msk->n_masks; idx++, result += need_len) {
314                 uint64_t cur = msk->data[idx];
315                 unsigned int run_len;
316
317                 need_len = MASK_ALIGN;
318
319                 /* if we're looking for free entries, invert mask */
320                 if (!used)
321                         cur = ~cur;
322
323                 /* if this is last mask, ignore everything after last bit */
324                 if (idx == last)
325                         cur &= last_msk;
326
327                 /* ignore everything before start on first iteration */
328                 if (idx == first) {
329                         cur >>= first_mod;
330                         /* at the start, we don't need the full mask len */
331                         need_len -= first_mod;
332                 }
333
334                 /* we will be looking for zeroes, so invert the mask */
335                 cur = ~cur;
336
337                 /* if mask is zero, we have a complete run */
338                 if (cur == 0)
339                         continue;
340
341                 /*
342                  * see if current run ends before mask end.
343                  */
344                 run_len = __builtin_ctzll(cur);
345
346                 /* add however many zeroes we've had in the last run and quit */
347                 if (run_len < need_len) {
348                         result += run_len;
349                         break;
350                 }
351         }
352         return result;
353 }
354
355 static int
356 find_prev(const struct rte_fbarray *arr, unsigned int start, bool used)
357 {
358         const struct used_mask *msk = get_used_mask(arr->data, arr->elt_sz,
359                         arr->len);
360         unsigned int idx, first, first_mod;
361         uint64_t ignore_msk;
362
363         /*
364          * mask only has granularity of MASK_ALIGN, but start may not be aligned
365          * on that boundary, so construct a special mask to exclude anything we
366          * don't want to see to avoid confusing clz.
367          */
368         first = MASK_LEN_TO_IDX(start);
369         first_mod = MASK_LEN_TO_MOD(start);
370         /* we're going backwards, so mask must start from the top */
371         ignore_msk = first_mod == MASK_ALIGN - 1 ?
372                                 -1ULL : /* prevent overflow */
373                                 ~(-1ULL << (first_mod + 1));
374
375         /* go backwards, include zero */
376         idx = first;
377         do {
378                 uint64_t cur = msk->data[idx];
379                 int found;
380
381                 /* if we're looking for free entries, invert mask */
382                 if (!used)
383                         cur = ~cur;
384
385                 /* ignore everything before start on first iteration */
386                 if (idx == first)
387                         cur &= ignore_msk;
388
389                 /* check if we have any entries */
390                 if (cur == 0)
391                         continue;
392
393                 /*
394                  * find last set bit - that will correspond to whatever it is
395                  * that we're looking for. we're counting trailing zeroes, thus
396                  * the value we get is counted from end of mask, so calculate
397                  * position from start of mask.
398                  */
399                 found = MASK_ALIGN - __builtin_clzll(cur) - 1;
400
401                 return MASK_GET_IDX(idx, found);
402         } while (idx-- != 0); /* decrement after check  to include zero*/
403
404         /* we didn't find anything */
405         rte_errno = used ? ENOENT : ENOSPC;
406         return -1;
407 }
408
409 static int
410 set_used(struct rte_fbarray *arr, unsigned int idx, bool used)
411 {
412         struct used_mask *msk;
413         uint64_t msk_bit = 1ULL << MASK_LEN_TO_MOD(idx);
414         unsigned int msk_idx = MASK_LEN_TO_IDX(idx);
415         bool already_used;
416         int ret = -1;
417
418         if (arr == NULL || idx >= arr->len) {
419                 rte_errno = EINVAL;
420                 return -1;
421         }
422         msk = get_used_mask(arr->data, arr->elt_sz, arr->len);
423         ret = 0;
424
425         /* prevent array from changing under us */
426         rte_rwlock_write_lock(&arr->rwlock);
427
428         already_used = (msk->data[msk_idx] & msk_bit) != 0;
429
430         /* nothing to be done */
431         if (used == already_used)
432                 goto out;
433
434         if (used) {
435                 msk->data[msk_idx] |= msk_bit;
436                 arr->count++;
437         } else {
438                 msk->data[msk_idx] &= ~msk_bit;
439                 arr->count--;
440         }
441 out:
442         rte_rwlock_write_unlock(&arr->rwlock);
443
444         return ret;
445 }
446
447 static int
448 fully_validate(const char *name, unsigned int elt_sz, unsigned int len)
449 {
450         if (name == NULL || elt_sz == 0 || len == 0 || len > INT_MAX) {
451                 rte_errno = EINVAL;
452                 return -1;
453         }
454
455         if (strnlen(name, RTE_FBARRAY_NAME_LEN) == RTE_FBARRAY_NAME_LEN) {
456                 rte_errno = ENAMETOOLONG;
457                 return -1;
458         }
459         return 0;
460 }
461
462 int __rte_experimental
463 rte_fbarray_init(struct rte_fbarray *arr, const char *name, unsigned int len,
464                 unsigned int elt_sz)
465 {
466         size_t page_sz, mmap_len;
467         char path[PATH_MAX];
468         struct used_mask *msk;
469         void *data = NULL;
470         int fd = -1;
471
472         if (arr == NULL) {
473                 rte_errno = EINVAL;
474                 return -1;
475         }
476
477         if (fully_validate(name, elt_sz, len))
478                 return -1;
479
480         page_sz = sysconf(_SC_PAGESIZE);
481         if (page_sz == (size_t)-1)
482                 goto fail;
483
484         /* calculate our memory limits */
485         mmap_len = calc_data_size(page_sz, elt_sz, len);
486
487         data = eal_get_virtual_area(NULL, &mmap_len, page_sz, 0, 0);
488         if (data == NULL)
489                 goto fail;
490
491         eal_get_fbarray_path(path, sizeof(path), name);
492
493         /*
494          * Each fbarray is unique to process namespace, i.e. the filename
495          * depends on process prefix. Try to take out a lock and see if we
496          * succeed. If we don't, someone else is using it already.
497          */
498         fd = open(path, O_CREAT | O_RDWR, 0600);
499         if (fd < 0) {
500                 RTE_LOG(DEBUG, EAL, "%s(): couldn't open %s: %s\n", __func__,
501                                 path, strerror(errno));
502                 rte_errno = errno;
503                 goto fail;
504         } else if (flock(fd, LOCK_EX | LOCK_NB)) {
505                 RTE_LOG(DEBUG, EAL, "%s(): couldn't lock %s: %s\n", __func__,
506                                 path, strerror(errno));
507                 rte_errno = EBUSY;
508                 goto fail;
509         }
510
511         /* take out a non-exclusive lock, so that other processes could still
512          * attach to it, but no other process could reinitialize it.
513          */
514         if (flock(fd, LOCK_SH | LOCK_NB)) {
515                 rte_errno = errno;
516                 goto fail;
517         }
518
519         if (resize_and_map(fd, data, mmap_len))
520                 goto fail;
521
522         /* we've mmap'ed the file, we can now close the fd */
523         close(fd);
524
525         /* initialize the data */
526         memset(data, 0, mmap_len);
527
528         /* populate data structure */
529         strlcpy(arr->name, name, sizeof(arr->name));
530         arr->data = data;
531         arr->len = len;
532         arr->elt_sz = elt_sz;
533         arr->count = 0;
534
535         msk = get_used_mask(data, elt_sz, len);
536         msk->n_masks = MASK_LEN_TO_IDX(RTE_ALIGN_CEIL(len, MASK_ALIGN));
537
538         rte_rwlock_init(&arr->rwlock);
539
540         return 0;
541 fail:
542         if (data)
543                 munmap(data, mmap_len);
544         if (fd >= 0)
545                 close(fd);
546         return -1;
547 }
548
549 int __rte_experimental
550 rte_fbarray_attach(struct rte_fbarray *arr)
551 {
552         size_t page_sz, mmap_len;
553         char path[PATH_MAX];
554         void *data = NULL;
555         int fd = -1;
556
557         if (arr == NULL) {
558                 rte_errno = EINVAL;
559                 return -1;
560         }
561
562         /*
563          * we don't need to synchronize attach as two values we need (element
564          * size and array length) are constant for the duration of life of
565          * the array, so the parts we care about will not race.
566          */
567
568         if (fully_validate(arr->name, arr->elt_sz, arr->len))
569                 return -1;
570
571         page_sz = sysconf(_SC_PAGESIZE);
572         if (page_sz == (size_t)-1)
573                 goto fail;
574
575         mmap_len = calc_data_size(page_sz, arr->elt_sz, arr->len);
576
577         data = eal_get_virtual_area(arr->data, &mmap_len, page_sz, 0, 0);
578         if (data == NULL)
579                 goto fail;
580
581         eal_get_fbarray_path(path, sizeof(path), arr->name);
582
583         fd = open(path, O_RDWR);
584         if (fd < 0) {
585                 rte_errno = errno;
586                 goto fail;
587         }
588
589         /* lock the file, to let others know we're using it */
590         if (flock(fd, LOCK_SH | LOCK_NB)) {
591                 rte_errno = errno;
592                 goto fail;
593         }
594
595         if (resize_and_map(fd, data, mmap_len))
596                 goto fail;
597
598         close(fd);
599
600         /* we're done */
601
602         return 0;
603 fail:
604         if (data)
605                 munmap(data, mmap_len);
606         if (fd >= 0)
607                 close(fd);
608         return -1;
609 }
610
611 int __rte_experimental
612 rte_fbarray_detach(struct rte_fbarray *arr)
613 {
614         if (arr == NULL) {
615                 rte_errno = EINVAL;
616                 return -1;
617         }
618
619         /*
620          * we don't need to synchronize detach as two values we need (element
621          * size and total capacity) are constant for the duration of life of
622          * the array, so the parts we care about will not race. if the user is
623          * detaching while doing something else in the same process, we can't
624          * really do anything about it, things will blow up either way.
625          */
626
627         size_t page_sz = sysconf(_SC_PAGESIZE);
628
629         if (page_sz == (size_t)-1)
630                 return -1;
631
632         /* this may already be unmapped (e.g. repeated call from previously
633          * failed destroy(), but this is on user, we can't (easily) know if this
634          * is still mapped.
635          */
636         munmap(arr->data, calc_data_size(page_sz, arr->elt_sz, arr->len));
637
638         return 0;
639 }
640
641 int __rte_experimental
642 rte_fbarray_destroy(struct rte_fbarray *arr)
643 {
644         int fd, ret;
645         char path[PATH_MAX];
646
647         ret = rte_fbarray_detach(arr);
648         if (ret)
649                 return ret;
650
651         /* try deleting the file */
652         eal_get_fbarray_path(path, sizeof(path), arr->name);
653
654         fd = open(path, O_RDONLY);
655         if (fd < 0) {
656                 RTE_LOG(ERR, EAL, "Could not open fbarray file: %s\n",
657                         strerror(errno));
658                 return -1;
659         }
660         if (flock(fd, LOCK_EX | LOCK_NB)) {
661                 RTE_LOG(DEBUG, EAL, "Cannot destroy fbarray - another process is using it\n");
662                 rte_errno = EBUSY;
663                 ret = -1;
664         } else {
665                 ret = 0;
666                 unlink(path);
667                 memset(arr, 0, sizeof(*arr));
668         }
669         close(fd);
670
671         return ret;
672 }
673
674 void * __rte_experimental
675 rte_fbarray_get(const struct rte_fbarray *arr, unsigned int idx)
676 {
677         void *ret = NULL;
678         if (arr == NULL) {
679                 rte_errno = EINVAL;
680                 return NULL;
681         }
682
683         if (idx >= arr->len) {
684                 rte_errno = EINVAL;
685                 return NULL;
686         }
687
688         ret = RTE_PTR_ADD(arr->data, idx * arr->elt_sz);
689
690         return ret;
691 }
692
693 int __rte_experimental
694 rte_fbarray_set_used(struct rte_fbarray *arr, unsigned int idx)
695 {
696         return set_used(arr, idx, true);
697 }
698
699 int __rte_experimental
700 rte_fbarray_set_free(struct rte_fbarray *arr, unsigned int idx)
701 {
702         return set_used(arr, idx, false);
703 }
704
705 int __rte_experimental
706 rte_fbarray_is_used(struct rte_fbarray *arr, unsigned int idx)
707 {
708         struct used_mask *msk;
709         int msk_idx;
710         uint64_t msk_bit;
711         int ret = -1;
712
713         if (arr == NULL || idx >= arr->len) {
714                 rte_errno = EINVAL;
715                 return -1;
716         }
717
718         /* prevent array from changing under us */
719         rte_rwlock_read_lock(&arr->rwlock);
720
721         msk = get_used_mask(arr->data, arr->elt_sz, arr->len);
722         msk_idx = MASK_LEN_TO_IDX(idx);
723         msk_bit = 1ULL << MASK_LEN_TO_MOD(idx);
724
725         ret = (msk->data[msk_idx] & msk_bit) != 0;
726
727         rte_rwlock_read_unlock(&arr->rwlock);
728
729         return ret;
730 }
731
732 static int
733 fbarray_find(struct rte_fbarray *arr, unsigned int start, bool next, bool used)
734 {
735         int ret = -1;
736
737         if (arr == NULL || start >= arr->len) {
738                 rte_errno = EINVAL;
739                 return -1;
740         }
741
742         /* prevent array from changing under us */
743         rte_rwlock_read_lock(&arr->rwlock);
744
745         /* cheap checks to prevent doing useless work */
746         if (!used) {
747                 if (arr->len == arr->count) {
748                         rte_errno = ENOSPC;
749                         goto out;
750                 }
751                 if (arr->count == 0) {
752                         ret = start;
753                         goto out;
754                 }
755         } else {
756                 if (arr->count == 0) {
757                         rte_errno = ENOENT;
758                         goto out;
759                 }
760                 if (arr->len == arr->count) {
761                         ret = start;
762                         goto out;
763                 }
764         }
765         if (next)
766                 ret = find_next(arr, start, used);
767         else
768                 ret = find_prev(arr, start, used);
769 out:
770         rte_rwlock_read_unlock(&arr->rwlock);
771         return ret;
772 }
773
774 int __rte_experimental
775 rte_fbarray_find_next_free(struct rte_fbarray *arr, unsigned int start)
776 {
777         return fbarray_find(arr, start, true, false);
778 }
779
780 int __rte_experimental
781 rte_fbarray_find_next_used(struct rte_fbarray *arr, unsigned int start)
782 {
783         return fbarray_find(arr, start, true, true);
784 }
785
786 int __rte_experimental
787 rte_fbarray_find_prev_free(struct rte_fbarray *arr, unsigned int start)
788 {
789         return fbarray_find(arr, start, false, false);
790 }
791
792 int __rte_experimental
793 rte_fbarray_find_prev_used(struct rte_fbarray *arr, unsigned int start)
794 {
795         return fbarray_find(arr, start, false, true);
796 }
797
798 static int
799 fbarray_find_n(struct rte_fbarray *arr, unsigned int start, unsigned int n,
800                 bool used)
801 {
802         int ret = -1;
803
804         if (arr == NULL || start >= arr->len || n > arr->len || n == 0) {
805                 rte_errno = EINVAL;
806                 return -1;
807         }
808         if (arr->len - start < n) {
809                 rte_errno = used ? ENOENT : ENOSPC;
810                 return -1;
811         }
812
813         /* prevent array from changing under us */
814         rte_rwlock_read_lock(&arr->rwlock);
815
816         /* cheap checks to prevent doing useless work */
817         if (!used) {
818                 if (arr->len == arr->count || arr->len - arr->count < n) {
819                         rte_errno = ENOSPC;
820                         goto out;
821                 }
822                 if (arr->count == 0) {
823                         ret = start;
824                         goto out;
825                 }
826         } else {
827                 if (arr->count < n) {
828                         rte_errno = ENOENT;
829                         goto out;
830                 }
831                 if (arr->count == arr->len) {
832                         ret = start;
833                         goto out;
834                 }
835         }
836
837         ret = find_next_n(arr, start, n, used);
838 out:
839         rte_rwlock_read_unlock(&arr->rwlock);
840         return ret;
841 }
842
843 int __rte_experimental
844 rte_fbarray_find_next_n_free(struct rte_fbarray *arr, unsigned int start,
845                 unsigned int n)
846 {
847         return fbarray_find_n(arr, start, n, false);
848 }
849
850 int __rte_experimental
851 rte_fbarray_find_next_n_used(struct rte_fbarray *arr, unsigned int start,
852                 unsigned int n)
853 {
854         return fbarray_find_n(arr, start, n, true);
855 }
856
857 static int
858 fbarray_find_contig(struct rte_fbarray *arr, unsigned int start, bool used)
859 {
860         int ret = -1;
861
862         if (arr == NULL || start >= arr->len) {
863                 rte_errno = EINVAL;
864                 return -1;
865         }
866
867         /* prevent array from changing under us */
868         rte_rwlock_read_lock(&arr->rwlock);
869
870         /* cheap checks to prevent doing useless work */
871         if (used) {
872                 if (arr->count == 0) {
873                         ret = 0;
874                         goto out;
875                 }
876                 if (arr->len == arr->count) {
877                         ret = arr->len - start;
878                         goto out;
879                 }
880         } else {
881                 if (arr->len == arr->count) {
882                         ret = 0;
883                         goto out;
884                 }
885                 if (arr->count == 0) {
886                         ret = arr->len - start;
887                         goto out;
888                 }
889         }
890
891         ret = find_contig(arr, start, false);
892 out:
893         rte_rwlock_read_unlock(&arr->rwlock);
894         return ret;
895 }
896
897 int __rte_experimental
898 rte_fbarray_find_contig_free(struct rte_fbarray *arr, unsigned int start)
899 {
900         return fbarray_find_contig(arr, start, false);
901 }
902
903 int __rte_experimental
904 rte_fbarray_find_contig_used(struct rte_fbarray *arr, unsigned int start)
905 {
906         return fbarray_find_contig(arr, start, true);
907 }
908
909 int __rte_experimental
910 rte_fbarray_find_idx(const struct rte_fbarray *arr, const void *elt)
911 {
912         void *end;
913         int ret = -1;
914
915         /*
916          * no need to synchronize as it doesn't matter if underlying data
917          * changes - we're doing pointer arithmetic here.
918          */
919
920         if (arr == NULL || elt == NULL) {
921                 rte_errno = EINVAL;
922                 return -1;
923         }
924         end = RTE_PTR_ADD(arr->data, arr->elt_sz * arr->len);
925         if (elt < arr->data || elt >= end) {
926                 rte_errno = EINVAL;
927                 return -1;
928         }
929
930         ret = RTE_PTR_DIFF(elt, arr->data) / arr->elt_sz;
931
932         return ret;
933 }
934
935 void __rte_experimental
936 rte_fbarray_dump_metadata(struct rte_fbarray *arr, FILE *f)
937 {
938         struct used_mask *msk;
939         unsigned int i;
940
941         if (arr == NULL || f == NULL) {
942                 rte_errno = EINVAL;
943                 return;
944         }
945
946         if (fully_validate(arr->name, arr->elt_sz, arr->len)) {
947                 fprintf(f, "Invalid file-backed array\n");
948                 goto out;
949         }
950
951         /* prevent array from changing under us */
952         rte_rwlock_read_lock(&arr->rwlock);
953
954         fprintf(f, "File-backed array: %s\n", arr->name);
955         fprintf(f, "size: %i occupied: %i elt_sz: %i\n",
956                         arr->len, arr->count, arr->elt_sz);
957
958         msk = get_used_mask(arr->data, arr->elt_sz, arr->len);
959
960         for (i = 0; i < msk->n_masks; i++)
961                 fprintf(f, "msk idx %i: 0x%016" PRIx64 "\n", i, msk->data[i]);
962 out:
963         rte_rwlock_read_unlock(&arr->rwlock);
964 }