fd0292a64288112ea67ee0c3edc4c6d8ed2ceea5
[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 <fcntl.h>
6 #include <inttypes.h>
7 #include <limits.h>
8 #include <stdint.h>
9 #include <errno.h>
10 #include <string.h>
11 #include <unistd.h>
12
13 #include <rte_common.h>
14 #include <rte_eal_paging.h>
15 #include <rte_errno.h>
16 #include <rte_log.h>
17 #include <rte_memory.h>
18 #include <rte_spinlock.h>
19 #include <rte_tailq.h>
20
21 #include "eal_filesystem.h"
22 #include "eal_private.h"
23
24 #include "rte_fbarray.h"
25
26 #define MASK_SHIFT 6ULL
27 #define MASK_ALIGN (1ULL << MASK_SHIFT)
28 #define MASK_LEN_TO_IDX(x) ((x) >> MASK_SHIFT)
29 #define MASK_LEN_TO_MOD(x) ((x) - RTE_ALIGN_FLOOR(x, MASK_ALIGN))
30 #define MASK_GET_IDX(idx, mod) ((idx << MASK_SHIFT) + mod)
31
32 /*
33  * We use this to keep track of created/attached memory areas to prevent user
34  * errors in API usage.
35  */
36 struct mem_area {
37         TAILQ_ENTRY(mem_area) next;
38         void *addr;
39         size_t len;
40         int fd;
41 };
42 TAILQ_HEAD(mem_area_head, mem_area);
43 /* local per-process tailq */
44 static struct mem_area_head mem_area_tailq =
45         TAILQ_HEAD_INITIALIZER(mem_area_tailq);
46 static rte_spinlock_t mem_area_lock = RTE_SPINLOCK_INITIALIZER;
47
48 /*
49  * This is a mask that is always stored at the end of array, to provide fast
50  * way of finding free/used spots without looping through each element.
51  */
52
53 struct used_mask {
54         unsigned int n_masks;
55         uint64_t data[];
56 };
57
58 static size_t
59 calc_mask_size(unsigned int len)
60 {
61         /* mask must be multiple of MASK_ALIGN, even though length of array
62          * itself may not be aligned on that boundary.
63          */
64         len = RTE_ALIGN_CEIL(len, MASK_ALIGN);
65         return sizeof(struct used_mask) +
66                         sizeof(uint64_t) * MASK_LEN_TO_IDX(len);
67 }
68
69 static size_t
70 calc_data_size(size_t page_sz, unsigned int elt_sz, unsigned int len)
71 {
72         size_t data_sz = elt_sz * len;
73         size_t msk_sz = calc_mask_size(len);
74         return RTE_ALIGN_CEIL(data_sz + msk_sz, page_sz);
75 }
76
77 static struct used_mask *
78 get_used_mask(void *data, unsigned int elt_sz, unsigned int len)
79 {
80         return (struct used_mask *) RTE_PTR_ADD(data, elt_sz * len);
81 }
82
83 static int
84 resize_and_map(int fd, void *addr, size_t len)
85 {
86         char path[PATH_MAX];
87         void *map_addr;
88
89         if (eal_file_truncate(fd, len)) {
90                 RTE_LOG(ERR, EAL, "Cannot truncate %s\n", path);
91                 return -1;
92         }
93
94         map_addr = rte_mem_map(addr, len, RTE_PROT_READ | RTE_PROT_WRITE,
95                         RTE_MAP_SHARED | RTE_MAP_FORCE_ADDRESS, fd, 0);
96         if (map_addr != addr) {
97                 return -1;
98         }
99         return 0;
100 }
101
102 static int
103 overlap(const struct mem_area *ma, const void *start, size_t len)
104 {
105         const void *end = RTE_PTR_ADD(start, len);
106         const void *ma_start = ma->addr;
107         const void *ma_end = RTE_PTR_ADD(ma->addr, ma->len);
108
109         /* start overlap? */
110         if (start >= ma_start && start < ma_end)
111                 return 1;
112         /* end overlap? */
113         if (end >= ma_start && end < ma_end)
114                 return 1;
115         return 0;
116 }
117
118 static int
119 find_next_n(const struct rte_fbarray *arr, unsigned int start, unsigned int n,
120             bool used)
121 {
122         const struct used_mask *msk = get_used_mask(arr->data, arr->elt_sz,
123                         arr->len);
124         unsigned int msk_idx, lookahead_idx, first, first_mod;
125         unsigned int last, last_mod;
126         uint64_t last_msk, ignore_msk;
127
128         /*
129          * mask only has granularity of MASK_ALIGN, but start may not be aligned
130          * on that boundary, so construct a special mask to exclude anything we
131          * don't want to see to avoid confusing ctz.
132          */
133         first = MASK_LEN_TO_IDX(start);
134         first_mod = MASK_LEN_TO_MOD(start);
135         ignore_msk = ~((1ULL << first_mod) - 1);
136
137         /* array length may not be aligned, so calculate ignore mask for last
138          * mask index.
139          */
140         last = MASK_LEN_TO_IDX(arr->len);
141         last_mod = MASK_LEN_TO_MOD(arr->len);
142         last_msk = ~(-1ULL << last_mod);
143
144         for (msk_idx = first; msk_idx < msk->n_masks; msk_idx++) {
145                 uint64_t cur_msk, lookahead_msk;
146                 unsigned int run_start, clz, left;
147                 bool found = false;
148                 /*
149                  * The process of getting n consecutive bits for arbitrary n is
150                  * a bit involved, but here it is in a nutshell:
151                  *
152                  *  1. let n be the number of consecutive bits we're looking for
153                  *  2. check if n can fit in one mask, and if so, do n-1
154                  *     rshift-ands to see if there is an appropriate run inside
155                  *     our current mask
156                  *    2a. if we found a run, bail out early
157                  *    2b. if we didn't find a run, proceed
158                  *  3. invert the mask and count leading zeroes (that is, count
159                  *     how many consecutive set bits we had starting from the
160                  *     end of current mask) as k
161                  *    3a. if k is 0, continue to next mask
162                  *    3b. if k is not 0, we have a potential run
163                  *  4. to satisfy our requirements, next mask must have n-k
164                  *     consecutive set bits right at the start, so we will do
165                  *     (n-k-1) rshift-ands and check if first bit is set.
166                  *
167                  * Step 4 will need to be repeated if (n-k) > MASK_ALIGN until
168                  * we either run out of masks, lose the run, or find what we
169                  * were looking for.
170                  */
171                 cur_msk = msk->data[msk_idx];
172                 left = n;
173
174                 /* if we're looking for free spaces, invert the mask */
175                 if (!used)
176                         cur_msk = ~cur_msk;
177
178                 /* combine current ignore mask with last index ignore mask */
179                 if (msk_idx == last)
180                         ignore_msk |= last_msk;
181
182                 /* if we have an ignore mask, ignore once */
183                 if (ignore_msk) {
184                         cur_msk &= ignore_msk;
185                         ignore_msk = 0;
186                 }
187
188                 /* if n can fit in within a single mask, do a search */
189                 if (n <= MASK_ALIGN) {
190                         uint64_t tmp_msk = cur_msk;
191                         unsigned int s_idx;
192                         for (s_idx = 0; s_idx < n - 1; s_idx++)
193                                 tmp_msk &= tmp_msk >> 1ULL;
194                         /* we found what we were looking for */
195                         if (tmp_msk != 0) {
196                                 run_start = __builtin_ctzll(tmp_msk);
197                                 return MASK_GET_IDX(msk_idx, run_start);
198                         }
199                 }
200
201                 /*
202                  * we didn't find our run within the mask, or n > MASK_ALIGN,
203                  * so we're going for plan B.
204                  */
205
206                 /* count leading zeroes on inverted mask */
207                 if (~cur_msk == 0)
208                         clz = sizeof(cur_msk) * 8;
209                 else
210                         clz = __builtin_clzll(~cur_msk);
211
212                 /* if there aren't any runs at the end either, just continue */
213                 if (clz == 0)
214                         continue;
215
216                 /* we have a partial run at the end, so try looking ahead */
217                 run_start = MASK_ALIGN - clz;
218                 left -= clz;
219
220                 for (lookahead_idx = msk_idx + 1; lookahead_idx < msk->n_masks;
221                                 lookahead_idx++) {
222                         unsigned int s_idx, need;
223                         lookahead_msk = msk->data[lookahead_idx];
224
225                         /* if we're looking for free space, invert the mask */
226                         if (!used)
227                                 lookahead_msk = ~lookahead_msk;
228
229                         /* figure out how many consecutive bits we need here */
230                         need = RTE_MIN(left, MASK_ALIGN);
231
232                         for (s_idx = 0; s_idx < need - 1; s_idx++)
233                                 lookahead_msk &= lookahead_msk >> 1ULL;
234
235                         /* if first bit is not set, we've lost the run */
236                         if ((lookahead_msk & 1) == 0) {
237                                 /*
238                                  * we've scanned this far, so we know there are
239                                  * no runs in the space we've lookahead-scanned
240                                  * as well, so skip that on next iteration.
241                                  */
242                                 ignore_msk = ~((1ULL << need) - 1);
243                                 msk_idx = lookahead_idx;
244                                 break;
245                         }
246
247                         left -= need;
248
249                         /* check if we've found what we were looking for */
250                         if (left == 0) {
251                                 found = true;
252                                 break;
253                         }
254                 }
255
256                 /* we didn't find anything, so continue */
257                 if (!found)
258                         continue;
259
260                 return MASK_GET_IDX(msk_idx, run_start);
261         }
262         /* we didn't find anything */
263         rte_errno = used ? ENOENT : ENOSPC;
264         return -1;
265 }
266
267 static int
268 find_next(const struct rte_fbarray *arr, unsigned int start, bool used)
269 {
270         const struct used_mask *msk = get_used_mask(arr->data, arr->elt_sz,
271                         arr->len);
272         unsigned int idx, first, first_mod;
273         unsigned int last, last_mod;
274         uint64_t last_msk, ignore_msk;
275
276         /*
277          * mask only has granularity of MASK_ALIGN, but start may not be aligned
278          * on that boundary, so construct a special mask to exclude anything we
279          * don't want to see to avoid confusing ctz.
280          */
281         first = MASK_LEN_TO_IDX(start);
282         first_mod = MASK_LEN_TO_MOD(start);
283         ignore_msk = ~((1ULL << first_mod) - 1ULL);
284
285         /* array length may not be aligned, so calculate ignore mask for last
286          * mask index.
287          */
288         last = MASK_LEN_TO_IDX(arr->len);
289         last_mod = MASK_LEN_TO_MOD(arr->len);
290         last_msk = ~(-(1ULL) << last_mod);
291
292         for (idx = first; idx < msk->n_masks; idx++) {
293                 uint64_t cur = msk->data[idx];
294                 int found;
295
296                 /* if we're looking for free entries, invert mask */
297                 if (!used)
298                         cur = ~cur;
299
300                 if (idx == last)
301                         cur &= last_msk;
302
303                 /* ignore everything before start on first iteration */
304                 if (idx == first)
305                         cur &= ignore_msk;
306
307                 /* check if we have any entries */
308                 if (cur == 0)
309                         continue;
310
311                 /*
312                  * find first set bit - that will correspond to whatever it is
313                  * that we're looking for.
314                  */
315                 found = __builtin_ctzll(cur);
316                 return MASK_GET_IDX(idx, found);
317         }
318         /* we didn't find anything */
319         rte_errno = used ? ENOENT : ENOSPC;
320         return -1;
321 }
322
323 static int
324 find_contig(const struct rte_fbarray *arr, unsigned int start, bool used)
325 {
326         const struct used_mask *msk = get_used_mask(arr->data, arr->elt_sz,
327                         arr->len);
328         unsigned int idx, first, first_mod;
329         unsigned int last, last_mod;
330         uint64_t last_msk;
331         unsigned int need_len, result = 0;
332
333         /* array length may not be aligned, so calculate ignore mask for last
334          * mask index.
335          */
336         last = MASK_LEN_TO_IDX(arr->len);
337         last_mod = MASK_LEN_TO_MOD(arr->len);
338         last_msk = ~(-(1ULL) << last_mod);
339
340         first = MASK_LEN_TO_IDX(start);
341         first_mod = MASK_LEN_TO_MOD(start);
342         for (idx = first; idx < msk->n_masks; idx++, result += need_len) {
343                 uint64_t cur = msk->data[idx];
344                 unsigned int run_len;
345
346                 need_len = MASK_ALIGN;
347
348                 /* if we're looking for free entries, invert mask */
349                 if (!used)
350                         cur = ~cur;
351
352                 /* if this is last mask, ignore everything after last bit */
353                 if (idx == last)
354                         cur &= last_msk;
355
356                 /* ignore everything before start on first iteration */
357                 if (idx == first) {
358                         cur >>= first_mod;
359                         /* at the start, we don't need the full mask len */
360                         need_len -= first_mod;
361                 }
362
363                 /* we will be looking for zeroes, so invert the mask */
364                 cur = ~cur;
365
366                 /* if mask is zero, we have a complete run */
367                 if (cur == 0)
368                         continue;
369
370                 /*
371                  * see if current run ends before mask end.
372                  */
373                 run_len = __builtin_ctzll(cur);
374
375                 /* add however many zeroes we've had in the last run and quit */
376                 if (run_len < need_len) {
377                         result += run_len;
378                         break;
379                 }
380         }
381         return result;
382 }
383
384 static int
385 find_prev_n(const struct rte_fbarray *arr, unsigned int start, unsigned int n,
386                 bool used)
387 {
388         const struct used_mask *msk = get_used_mask(arr->data, arr->elt_sz,
389                         arr->len);
390         unsigned int msk_idx, lookbehind_idx, first, first_mod;
391         uint64_t ignore_msk;
392
393         /*
394          * mask only has granularity of MASK_ALIGN, but start may not be aligned
395          * on that boundary, so construct a special mask to exclude anything we
396          * don't want to see to avoid confusing ctz.
397          */
398         first = MASK_LEN_TO_IDX(start);
399         first_mod = MASK_LEN_TO_MOD(start);
400         /* we're going backwards, so mask must start from the top */
401         ignore_msk = first_mod == MASK_ALIGN - 1 ?
402                                 -1ULL : /* prevent overflow */
403                                 ~(-1ULL << (first_mod + 1));
404
405         /* go backwards, include zero */
406         msk_idx = first;
407         do {
408                 uint64_t cur_msk, lookbehind_msk;
409                 unsigned int run_start, run_end, ctz, left;
410                 bool found = false;
411                 /*
412                  * The process of getting n consecutive bits from the top for
413                  * arbitrary n is a bit involved, but here it is in a nutshell:
414                  *
415                  *  1. let n be the number of consecutive bits we're looking for
416                  *  2. check if n can fit in one mask, and if so, do n-1
417                  *     lshift-ands to see if there is an appropriate run inside
418                  *     our current mask
419                  *    2a. if we found a run, bail out early
420                  *    2b. if we didn't find a run, proceed
421                  *  3. invert the mask and count trailing zeroes (that is, count
422                  *     how many consecutive set bits we had starting from the
423                  *     start of current mask) as k
424                  *    3a. if k is 0, continue to next mask
425                  *    3b. if k is not 0, we have a potential run
426                  *  4. to satisfy our requirements, next mask must have n-k
427                  *     consecutive set bits at the end, so we will do (n-k-1)
428                  *     lshift-ands and check if last bit is set.
429                  *
430                  * Step 4 will need to be repeated if (n-k) > MASK_ALIGN until
431                  * we either run out of masks, lose the run, or find what we
432                  * were looking for.
433                  */
434                 cur_msk = msk->data[msk_idx];
435                 left = n;
436
437                 /* if we're looking for free spaces, invert the mask */
438                 if (!used)
439                         cur_msk = ~cur_msk;
440
441                 /* if we have an ignore mask, ignore once */
442                 if (ignore_msk) {
443                         cur_msk &= ignore_msk;
444                         ignore_msk = 0;
445                 }
446
447                 /* if n can fit in within a single mask, do a search */
448                 if (n <= MASK_ALIGN) {
449                         uint64_t tmp_msk = cur_msk;
450                         unsigned int s_idx;
451                         for (s_idx = 0; s_idx < n - 1; s_idx++)
452                                 tmp_msk &= tmp_msk << 1ULL;
453                         /* we found what we were looking for */
454                         if (tmp_msk != 0) {
455                                 /* clz will give us offset from end of mask, and
456                                  * we only get the end of our run, not start,
457                                  * so adjust result to point to where start
458                                  * would have been.
459                                  */
460                                 run_start = MASK_ALIGN -
461                                                 __builtin_clzll(tmp_msk) - n;
462                                 return MASK_GET_IDX(msk_idx, run_start);
463                         }
464                 }
465
466                 /*
467                  * we didn't find our run within the mask, or n > MASK_ALIGN,
468                  * so we're going for plan B.
469                  */
470
471                 /* count trailing zeroes on inverted mask */
472                 if (~cur_msk == 0)
473                         ctz = sizeof(cur_msk) * 8;
474                 else
475                         ctz = __builtin_ctzll(~cur_msk);
476
477                 /* if there aren't any runs at the start either, just
478                  * continue
479                  */
480                 if (ctz == 0)
481                         continue;
482
483                 /* we have a partial run at the start, so try looking behind */
484                 run_end = MASK_GET_IDX(msk_idx, ctz);
485                 left -= ctz;
486
487                 /* go backwards, include zero */
488                 lookbehind_idx = msk_idx - 1;
489
490                 /* we can't lookbehind as we've run out of masks, so stop */
491                 if (msk_idx == 0)
492                         break;
493
494                 do {
495                         const uint64_t last_bit = 1ULL << (MASK_ALIGN - 1);
496                         unsigned int s_idx, need;
497
498                         lookbehind_msk = msk->data[lookbehind_idx];
499
500                         /* if we're looking for free space, invert the mask */
501                         if (!used)
502                                 lookbehind_msk = ~lookbehind_msk;
503
504                         /* figure out how many consecutive bits we need here */
505                         need = RTE_MIN(left, MASK_ALIGN);
506
507                         for (s_idx = 0; s_idx < need - 1; s_idx++)
508                                 lookbehind_msk &= lookbehind_msk << 1ULL;
509
510                         /* if last bit is not set, we've lost the run */
511                         if ((lookbehind_msk & last_bit) == 0) {
512                                 /*
513                                  * we've scanned this far, so we know there are
514                                  * no runs in the space we've lookbehind-scanned
515                                  * as well, so skip that on next iteration.
516                                  */
517                                 ignore_msk = -1ULL << need;
518                                 msk_idx = lookbehind_idx;
519                                 break;
520                         }
521
522                         left -= need;
523
524                         /* check if we've found what we were looking for */
525                         if (left == 0) {
526                                 found = true;
527                                 break;
528                         }
529                 } while ((lookbehind_idx--) != 0); /* decrement after check to
530                                                     * include zero
531                                                     */
532
533                 /* we didn't find anything, so continue */
534                 if (!found)
535                         continue;
536
537                 /* we've found what we were looking for, but we only know where
538                  * the run ended, so calculate start position.
539                  */
540                 return run_end - n;
541         } while (msk_idx-- != 0); /* decrement after check to include zero */
542         /* we didn't find anything */
543         rte_errno = used ? ENOENT : ENOSPC;
544         return -1;
545 }
546
547 static int
548 find_prev(const struct rte_fbarray *arr, unsigned int start, bool used)
549 {
550         const struct used_mask *msk = get_used_mask(arr->data, arr->elt_sz,
551                         arr->len);
552         unsigned int idx, first, first_mod;
553         uint64_t ignore_msk;
554
555         /*
556          * mask only has granularity of MASK_ALIGN, but start may not be aligned
557          * on that boundary, so construct a special mask to exclude anything we
558          * don't want to see to avoid confusing clz.
559          */
560         first = MASK_LEN_TO_IDX(start);
561         first_mod = MASK_LEN_TO_MOD(start);
562         /* we're going backwards, so mask must start from the top */
563         ignore_msk = first_mod == MASK_ALIGN - 1 ?
564                                 -1ULL : /* prevent overflow */
565                                 ~(-1ULL << (first_mod + 1));
566
567         /* go backwards, include zero */
568         idx = first;
569         do {
570                 uint64_t cur = msk->data[idx];
571                 int found;
572
573                 /* if we're looking for free entries, invert mask */
574                 if (!used)
575                         cur = ~cur;
576
577                 /* ignore everything before start on first iteration */
578                 if (idx == first)
579                         cur &= ignore_msk;
580
581                 /* check if we have any entries */
582                 if (cur == 0)
583                         continue;
584
585                 /*
586                  * find last set bit - that will correspond to whatever it is
587                  * that we're looking for. we're counting trailing zeroes, thus
588                  * the value we get is counted from end of mask, so calculate
589                  * position from start of mask.
590                  */
591                 found = MASK_ALIGN - __builtin_clzll(cur) - 1;
592
593                 return MASK_GET_IDX(idx, found);
594         } while (idx-- != 0); /* decrement after check  to include zero*/
595
596         /* we didn't find anything */
597         rte_errno = used ? ENOENT : ENOSPC;
598         return -1;
599 }
600
601 static int
602 find_rev_contig(const struct rte_fbarray *arr, unsigned int start, bool used)
603 {
604         const struct used_mask *msk = get_used_mask(arr->data, arr->elt_sz,
605                         arr->len);
606         unsigned int idx, first, first_mod;
607         unsigned int need_len, result = 0;
608
609         first = MASK_LEN_TO_IDX(start);
610         first_mod = MASK_LEN_TO_MOD(start);
611
612         /* go backwards, include zero */
613         idx = first;
614         do {
615                 uint64_t cur = msk->data[idx];
616                 unsigned int run_len;
617
618                 need_len = MASK_ALIGN;
619
620                 /* if we're looking for free entries, invert mask */
621                 if (!used)
622                         cur = ~cur;
623
624                 /* ignore everything after start on first iteration */
625                 if (idx == first) {
626                         unsigned int end_len = MASK_ALIGN - first_mod - 1;
627                         cur <<= end_len;
628                         /* at the start, we don't need the full mask len */
629                         need_len -= end_len;
630                 }
631
632                 /* we will be looking for zeroes, so invert the mask */
633                 cur = ~cur;
634
635                 /* if mask is zero, we have a complete run */
636                 if (cur == 0)
637                         goto endloop;
638
639                 /*
640                  * see where run ends, starting from the end.
641                  */
642                 run_len = __builtin_clzll(cur);
643
644                 /* add however many zeroes we've had in the last run and quit */
645                 if (run_len < need_len) {
646                         result += run_len;
647                         break;
648                 }
649 endloop:
650                 result += need_len;
651         } while (idx-- != 0); /* decrement after check to include zero */
652         return result;
653 }
654
655 static int
656 set_used(struct rte_fbarray *arr, unsigned int idx, bool used)
657 {
658         struct used_mask *msk;
659         uint64_t msk_bit = 1ULL << MASK_LEN_TO_MOD(idx);
660         unsigned int msk_idx = MASK_LEN_TO_IDX(idx);
661         bool already_used;
662         int ret = -1;
663
664         if (arr == NULL || idx >= arr->len) {
665                 rte_errno = EINVAL;
666                 return -1;
667         }
668         msk = get_used_mask(arr->data, arr->elt_sz, arr->len);
669         ret = 0;
670
671         /* prevent array from changing under us */
672         rte_rwlock_write_lock(&arr->rwlock);
673
674         already_used = (msk->data[msk_idx] & msk_bit) != 0;
675
676         /* nothing to be done */
677         if (used == already_used)
678                 goto out;
679
680         if (used) {
681                 msk->data[msk_idx] |= msk_bit;
682                 arr->count++;
683         } else {
684                 msk->data[msk_idx] &= ~msk_bit;
685                 arr->count--;
686         }
687 out:
688         rte_rwlock_write_unlock(&arr->rwlock);
689
690         return ret;
691 }
692
693 static int
694 fully_validate(const char *name, unsigned int elt_sz, unsigned int len)
695 {
696         if (name == NULL || elt_sz == 0 || len == 0 || len > INT_MAX) {
697                 rte_errno = EINVAL;
698                 return -1;
699         }
700
701         if (strnlen(name, RTE_FBARRAY_NAME_LEN) == RTE_FBARRAY_NAME_LEN) {
702                 rte_errno = ENAMETOOLONG;
703                 return -1;
704         }
705         return 0;
706 }
707
708 int
709 rte_fbarray_init(struct rte_fbarray *arr, const char *name, unsigned int len,
710                 unsigned int elt_sz)
711 {
712         size_t page_sz, mmap_len;
713         char path[PATH_MAX];
714         struct used_mask *msk;
715         struct mem_area *ma = NULL;
716         void *data = NULL;
717         int fd = -1;
718
719         if (arr == NULL) {
720                 rte_errno = EINVAL;
721                 return -1;
722         }
723
724         if (fully_validate(name, elt_sz, len))
725                 return -1;
726
727         /* allocate mem area before doing anything */
728         ma = malloc(sizeof(*ma));
729         if (ma == NULL) {
730                 rte_errno = ENOMEM;
731                 return -1;
732         }
733
734         page_sz = rte_mem_page_size();
735         if (page_sz == (size_t)-1) {
736                 free(ma);
737                 return -1;
738         }
739
740         /* calculate our memory limits */
741         mmap_len = calc_data_size(page_sz, elt_sz, len);
742
743         data = eal_get_virtual_area(NULL, &mmap_len, page_sz, 0, 0);
744         if (data == NULL) {
745                 free(ma);
746                 return -1;
747         }
748
749         rte_spinlock_lock(&mem_area_lock);
750
751         fd = -1;
752
753         if (internal_config.no_shconf) {
754                 /* remap virtual area as writable */
755                 static const int flags = RTE_MAP_FORCE_ADDRESS |
756                         RTE_MAP_PRIVATE | RTE_MAP_ANONYMOUS;
757                 void *new_data = rte_mem_map(data, mmap_len,
758                         RTE_PROT_READ | RTE_PROT_WRITE, flags, fd, 0);
759                 if (new_data == NULL) {
760                         RTE_LOG(DEBUG, EAL, "%s(): couldn't remap anonymous memory: %s\n",
761                                         __func__, rte_strerror(rte_errno));
762                         goto fail;
763                 }
764         } else {
765                 eal_get_fbarray_path(path, sizeof(path), name);
766
767                 /*
768                  * Each fbarray is unique to process namespace, i.e. the
769                  * filename depends on process prefix. Try to take out a lock
770                  * and see if we succeed. If we don't, someone else is using it
771                  * already.
772                  */
773                 fd = eal_file_open(path, EAL_OPEN_CREATE | EAL_OPEN_READWRITE);
774                 if (fd < 0) {
775                         RTE_LOG(DEBUG, EAL, "%s(): couldn't open %s: %s\n",
776                                 __func__, path, rte_strerror(rte_errno));
777                         goto fail;
778                 } else if (eal_file_lock(
779                                 fd, EAL_FLOCK_EXCLUSIVE, EAL_FLOCK_RETURN)) {
780                         RTE_LOG(DEBUG, EAL, "%s(): couldn't lock %s: %s\n",
781                                 __func__, path, rte_strerror(rte_errno));
782                         rte_errno = EBUSY;
783                         goto fail;
784                 }
785
786                 /* take out a non-exclusive lock, so that other processes could
787                  * still attach to it, but no other process could reinitialize
788                  * it.
789                  */
790                 if (eal_file_lock(fd, EAL_FLOCK_SHARED, EAL_FLOCK_RETURN))
791                         goto fail;
792
793                 if (resize_and_map(fd, data, mmap_len))
794                         goto fail;
795         }
796         ma->addr = data;
797         ma->len = mmap_len;
798         ma->fd = fd;
799
800         /* do not close fd - keep it until detach/destroy */
801         TAILQ_INSERT_TAIL(&mem_area_tailq, ma, next);
802
803         /* initialize the data */
804         memset(data, 0, mmap_len);
805
806         /* populate data structure */
807         strlcpy(arr->name, name, sizeof(arr->name));
808         arr->data = data;
809         arr->len = len;
810         arr->elt_sz = elt_sz;
811         arr->count = 0;
812
813         msk = get_used_mask(data, elt_sz, len);
814         msk->n_masks = MASK_LEN_TO_IDX(RTE_ALIGN_CEIL(len, MASK_ALIGN));
815
816         rte_rwlock_init(&arr->rwlock);
817
818         rte_spinlock_unlock(&mem_area_lock);
819
820         return 0;
821 fail:
822         if (data)
823                 rte_mem_unmap(data, mmap_len);
824         if (fd >= 0)
825                 close(fd);
826         free(ma);
827
828         rte_spinlock_unlock(&mem_area_lock);
829         return -1;
830 }
831
832 int
833 rte_fbarray_attach(struct rte_fbarray *arr)
834 {
835         struct mem_area *ma = NULL, *tmp = NULL;
836         size_t page_sz, mmap_len;
837         char path[PATH_MAX];
838         void *data = NULL;
839         int fd = -1;
840
841         if (arr == NULL) {
842                 rte_errno = EINVAL;
843                 return -1;
844         }
845
846         /*
847          * we don't need to synchronize attach as two values we need (element
848          * size and array length) are constant for the duration of life of
849          * the array, so the parts we care about will not race.
850          */
851
852         if (fully_validate(arr->name, arr->elt_sz, arr->len))
853                 return -1;
854
855         ma = malloc(sizeof(*ma));
856         if (ma == NULL) {
857                 rte_errno = ENOMEM;
858                 return -1;
859         }
860
861         page_sz = rte_mem_page_size();
862         if (page_sz == (size_t)-1) {
863                 free(ma);
864                 return -1;
865         }
866
867         mmap_len = calc_data_size(page_sz, arr->elt_sz, arr->len);
868
869         /* check the tailq - maybe user has already mapped this address space */
870         rte_spinlock_lock(&mem_area_lock);
871
872         TAILQ_FOREACH(tmp, &mem_area_tailq, next) {
873                 if (overlap(tmp, arr->data, mmap_len)) {
874                         rte_errno = EEXIST;
875                         goto fail;
876                 }
877         }
878
879         /* we know this memory area is unique, so proceed */
880
881         data = eal_get_virtual_area(arr->data, &mmap_len, page_sz, 0, 0);
882         if (data == NULL)
883                 goto fail;
884
885         eal_get_fbarray_path(path, sizeof(path), arr->name);
886
887         fd = eal_file_open(path, EAL_OPEN_READWRITE);
888         if (fd < 0) {
889                 goto fail;
890         }
891
892         /* lock the file, to let others know we're using it */
893         if (eal_file_lock(fd, EAL_FLOCK_SHARED, EAL_FLOCK_RETURN))
894                 goto fail;
895
896         if (resize_and_map(fd, data, mmap_len))
897                 goto fail;
898
899         /* store our new memory area */
900         ma->addr = data;
901         ma->fd = fd; /* keep fd until detach/destroy */
902         ma->len = mmap_len;
903
904         TAILQ_INSERT_TAIL(&mem_area_tailq, ma, next);
905
906         /* we're done */
907
908         rte_spinlock_unlock(&mem_area_lock);
909         return 0;
910 fail:
911         if (data)
912                 rte_mem_unmap(data, mmap_len);
913         if (fd >= 0)
914                 close(fd);
915         free(ma);
916         rte_spinlock_unlock(&mem_area_lock);
917         return -1;
918 }
919
920 int
921 rte_fbarray_detach(struct rte_fbarray *arr)
922 {
923         struct mem_area *tmp = NULL;
924         size_t mmap_len;
925         int ret = -1;
926
927         if (arr == NULL) {
928                 rte_errno = EINVAL;
929                 return -1;
930         }
931
932         /*
933          * we don't need to synchronize detach as two values we need (element
934          * size and total capacity) are constant for the duration of life of
935          * the array, so the parts we care about will not race. if the user is
936          * detaching while doing something else in the same process, we can't
937          * really do anything about it, things will blow up either way.
938          */
939
940         size_t page_sz = rte_mem_page_size();
941         if (page_sz == (size_t)-1)
942                 return -1;
943
944         mmap_len = calc_data_size(page_sz, arr->elt_sz, arr->len);
945
946         /* does this area exist? */
947         rte_spinlock_lock(&mem_area_lock);
948
949         TAILQ_FOREACH(tmp, &mem_area_tailq, next) {
950                 if (tmp->addr == arr->data && tmp->len == mmap_len)
951                         break;
952         }
953         if (tmp == NULL) {
954                 rte_errno = ENOENT;
955                 ret = -1;
956                 goto out;
957         }
958
959         rte_mem_unmap(arr->data, mmap_len);
960
961         /* area is unmapped, close fd and remove the tailq entry */
962         if (tmp->fd >= 0)
963                 close(tmp->fd);
964         TAILQ_REMOVE(&mem_area_tailq, tmp, next);
965         free(tmp);
966
967         ret = 0;
968 out:
969         rte_spinlock_unlock(&mem_area_lock);
970         return ret;
971 }
972
973 int
974 rte_fbarray_destroy(struct rte_fbarray *arr)
975 {
976         struct mem_area *tmp = NULL;
977         size_t mmap_len;
978         int fd, ret;
979         char path[PATH_MAX];
980
981         if (arr == NULL) {
982                 rte_errno = EINVAL;
983                 return -1;
984         }
985
986         /*
987          * we don't need to synchronize detach as two values we need (element
988          * size and total capacity) are constant for the duration of life of
989          * the array, so the parts we care about will not race. if the user is
990          * detaching while doing something else in the same process, we can't
991          * really do anything about it, things will blow up either way.
992          */
993
994         size_t page_sz = rte_mem_page_size();
995         if (page_sz == (size_t)-1)
996                 return -1;
997
998         mmap_len = calc_data_size(page_sz, arr->elt_sz, arr->len);
999
1000         /* does this area exist? */
1001         rte_spinlock_lock(&mem_area_lock);
1002
1003         TAILQ_FOREACH(tmp, &mem_area_tailq, next) {
1004                 if (tmp->addr == arr->data && tmp->len == mmap_len)
1005                         break;
1006         }
1007         if (tmp == NULL) {
1008                 rte_errno = ENOENT;
1009                 ret = -1;
1010                 goto out;
1011         }
1012         /* with no shconf, there were never any files to begin with */
1013         if (!internal_config.no_shconf) {
1014                 /*
1015                  * attempt to get an exclusive lock on the file, to ensure it
1016                  * has been detached by all other processes
1017                  */
1018                 fd = tmp->fd;
1019                 if (eal_file_lock(fd, EAL_FLOCK_EXCLUSIVE, EAL_FLOCK_RETURN)) {
1020                         RTE_LOG(DEBUG, EAL, "Cannot destroy fbarray - another process is using it\n");
1021                         rte_errno = EBUSY;
1022                         ret = -1;
1023                         goto out;
1024                 }
1025
1026                 /* we're OK to destroy the file */
1027                 eal_get_fbarray_path(path, sizeof(path), arr->name);
1028                 if (unlink(path)) {
1029                         RTE_LOG(DEBUG, EAL, "Cannot unlink fbarray: %s\n",
1030                                 strerror(errno));
1031                         rte_errno = errno;
1032                         /*
1033                          * we're still holding an exclusive lock, so drop it to
1034                          * shared.
1035                          */
1036                         eal_file_lock(fd, EAL_FLOCK_SHARED, EAL_FLOCK_RETURN);
1037
1038                         ret = -1;
1039                         goto out;
1040                 }
1041                 close(fd);
1042         }
1043         rte_mem_unmap(arr->data, mmap_len);
1044
1045         /* area is unmapped, remove the tailq entry */
1046         TAILQ_REMOVE(&mem_area_tailq, tmp, next);
1047         free(tmp);
1048         ret = 0;
1049
1050         /* reset the fbarray structure */
1051         memset(arr, 0, sizeof(*arr));
1052 out:
1053         rte_spinlock_unlock(&mem_area_lock);
1054         return ret;
1055 }
1056
1057 void *
1058 rte_fbarray_get(const struct rte_fbarray *arr, unsigned int idx)
1059 {
1060         void *ret = NULL;
1061         if (arr == NULL) {
1062                 rte_errno = EINVAL;
1063                 return NULL;
1064         }
1065
1066         if (idx >= arr->len) {
1067                 rte_errno = EINVAL;
1068                 return NULL;
1069         }
1070
1071         ret = RTE_PTR_ADD(arr->data, idx * arr->elt_sz);
1072
1073         return ret;
1074 }
1075
1076 int
1077 rte_fbarray_set_used(struct rte_fbarray *arr, unsigned int idx)
1078 {
1079         return set_used(arr, idx, true);
1080 }
1081
1082 int
1083 rte_fbarray_set_free(struct rte_fbarray *arr, unsigned int idx)
1084 {
1085         return set_used(arr, idx, false);
1086 }
1087
1088 int
1089 rte_fbarray_is_used(struct rte_fbarray *arr, unsigned int idx)
1090 {
1091         struct used_mask *msk;
1092         int msk_idx;
1093         uint64_t msk_bit;
1094         int ret = -1;
1095
1096         if (arr == NULL || idx >= arr->len) {
1097                 rte_errno = EINVAL;
1098                 return -1;
1099         }
1100
1101         /* prevent array from changing under us */
1102         rte_rwlock_read_lock(&arr->rwlock);
1103
1104         msk = get_used_mask(arr->data, arr->elt_sz, arr->len);
1105         msk_idx = MASK_LEN_TO_IDX(idx);
1106         msk_bit = 1ULL << MASK_LEN_TO_MOD(idx);
1107
1108         ret = (msk->data[msk_idx] & msk_bit) != 0;
1109
1110         rte_rwlock_read_unlock(&arr->rwlock);
1111
1112         return ret;
1113 }
1114
1115 static int
1116 fbarray_find(struct rte_fbarray *arr, unsigned int start, bool next, bool used)
1117 {
1118         int ret = -1;
1119
1120         if (arr == NULL || start >= arr->len) {
1121                 rte_errno = EINVAL;
1122                 return -1;
1123         }
1124
1125         /* prevent array from changing under us */
1126         rte_rwlock_read_lock(&arr->rwlock);
1127
1128         /* cheap checks to prevent doing useless work */
1129         if (!used) {
1130                 if (arr->len == arr->count) {
1131                         rte_errno = ENOSPC;
1132                         goto out;
1133                 }
1134                 if (arr->count == 0) {
1135                         ret = start;
1136                         goto out;
1137                 }
1138         } else {
1139                 if (arr->count == 0) {
1140                         rte_errno = ENOENT;
1141                         goto out;
1142                 }
1143                 if (arr->len == arr->count) {
1144                         ret = start;
1145                         goto out;
1146                 }
1147         }
1148         if (next)
1149                 ret = find_next(arr, start, used);
1150         else
1151                 ret = find_prev(arr, start, used);
1152 out:
1153         rte_rwlock_read_unlock(&arr->rwlock);
1154         return ret;
1155 }
1156
1157 int
1158 rte_fbarray_find_next_free(struct rte_fbarray *arr, unsigned int start)
1159 {
1160         return fbarray_find(arr, start, true, false);
1161 }
1162
1163 int
1164 rte_fbarray_find_next_used(struct rte_fbarray *arr, unsigned int start)
1165 {
1166         return fbarray_find(arr, start, true, true);
1167 }
1168
1169 int
1170 rte_fbarray_find_prev_free(struct rte_fbarray *arr, unsigned int start)
1171 {
1172         return fbarray_find(arr, start, false, false);
1173 }
1174
1175 int
1176 rte_fbarray_find_prev_used(struct rte_fbarray *arr, unsigned int start)
1177 {
1178         return fbarray_find(arr, start, false, true);
1179 }
1180
1181 static int
1182 fbarray_find_n(struct rte_fbarray *arr, unsigned int start, unsigned int n,
1183                 bool next, bool used)
1184 {
1185         int ret = -1;
1186
1187         if (arr == NULL || start >= arr->len || n > arr->len || n == 0) {
1188                 rte_errno = EINVAL;
1189                 return -1;
1190         }
1191         if (next && (arr->len - start) < n) {
1192                 rte_errno = used ? ENOENT : ENOSPC;
1193                 return -1;
1194         }
1195         if (!next && start < (n - 1)) {
1196                 rte_errno = used ? ENOENT : ENOSPC;
1197                 return -1;
1198         }
1199
1200         /* prevent array from changing under us */
1201         rte_rwlock_read_lock(&arr->rwlock);
1202
1203         /* cheap checks to prevent doing useless work */
1204         if (!used) {
1205                 if (arr->len == arr->count || arr->len - arr->count < n) {
1206                         rte_errno = ENOSPC;
1207                         goto out;
1208                 }
1209                 if (arr->count == 0) {
1210                         ret = next ? start : start - n + 1;
1211                         goto out;
1212                 }
1213         } else {
1214                 if (arr->count < n) {
1215                         rte_errno = ENOENT;
1216                         goto out;
1217                 }
1218                 if (arr->count == arr->len) {
1219                         ret = next ? start : start - n + 1;
1220                         goto out;
1221                 }
1222         }
1223
1224         if (next)
1225                 ret = find_next_n(arr, start, n, used);
1226         else
1227                 ret = find_prev_n(arr, start, n, used);
1228 out:
1229         rte_rwlock_read_unlock(&arr->rwlock);
1230         return ret;
1231 }
1232
1233 int
1234 rte_fbarray_find_next_n_free(struct rte_fbarray *arr, unsigned int start,
1235                 unsigned int n)
1236 {
1237         return fbarray_find_n(arr, start, n, true, false);
1238 }
1239
1240 int
1241 rte_fbarray_find_next_n_used(struct rte_fbarray *arr, unsigned int start,
1242                 unsigned int n)
1243 {
1244         return fbarray_find_n(arr, start, n, true, true);
1245 }
1246
1247 int
1248 rte_fbarray_find_prev_n_free(struct rte_fbarray *arr, unsigned int start,
1249                 unsigned int n)
1250 {
1251         return fbarray_find_n(arr, start, n, false, false);
1252 }
1253
1254 int
1255 rte_fbarray_find_prev_n_used(struct rte_fbarray *arr, unsigned int start,
1256                 unsigned int n)
1257 {
1258         return fbarray_find_n(arr, start, n, false, true);
1259 }
1260
1261 static int
1262 fbarray_find_contig(struct rte_fbarray *arr, unsigned int start, bool next,
1263                 bool used)
1264 {
1265         int ret = -1;
1266
1267         if (arr == NULL || start >= arr->len) {
1268                 rte_errno = EINVAL;
1269                 return -1;
1270         }
1271
1272         /* prevent array from changing under us */
1273         rte_rwlock_read_lock(&arr->rwlock);
1274
1275         /* cheap checks to prevent doing useless work */
1276         if (used) {
1277                 if (arr->count == 0) {
1278                         ret = 0;
1279                         goto out;
1280                 }
1281                 if (next && arr->count == arr->len) {
1282                         ret = arr->len - start;
1283                         goto out;
1284                 }
1285                 if (!next && arr->count == arr->len) {
1286                         ret = start + 1;
1287                         goto out;
1288                 }
1289         } else {
1290                 if (arr->len == arr->count) {
1291                         ret = 0;
1292                         goto out;
1293                 }
1294                 if (next && arr->count == 0) {
1295                         ret = arr->len - start;
1296                         goto out;
1297                 }
1298                 if (!next && arr->count == 0) {
1299                         ret = start + 1;
1300                         goto out;
1301                 }
1302         }
1303
1304         if (next)
1305                 ret = find_contig(arr, start, used);
1306         else
1307                 ret = find_rev_contig(arr, start, used);
1308 out:
1309         rte_rwlock_read_unlock(&arr->rwlock);
1310         return ret;
1311 }
1312
1313 static int
1314 fbarray_find_biggest(struct rte_fbarray *arr, unsigned int start, bool used,
1315                 bool rev)
1316 {
1317         int cur_idx, next_idx, cur_len, biggest_idx, biggest_len;
1318         /* don't stack if conditions, use function pointers instead */
1319         int (*find_func)(struct rte_fbarray *, unsigned int);
1320         int (*find_contig_func)(struct rte_fbarray *, unsigned int);
1321
1322         if (arr == NULL || start >= arr->len) {
1323                 rte_errno = EINVAL;
1324                 return -1;
1325         }
1326         /* the other API calls already do their fair share of cheap checks, so
1327          * no need to do them here.
1328          */
1329
1330         /* the API's called are thread-safe, but something may still happen
1331          * between the API calls, so lock the fbarray. all other API's are
1332          * read-locking the fbarray, so read lock here is OK.
1333          */
1334         rte_rwlock_read_lock(&arr->rwlock);
1335
1336         /* pick out appropriate functions */
1337         if (used) {
1338                 if (rev) {
1339                         find_func = rte_fbarray_find_prev_used;
1340                         find_contig_func = rte_fbarray_find_rev_contig_used;
1341                 } else {
1342                         find_func = rte_fbarray_find_next_used;
1343                         find_contig_func = rte_fbarray_find_contig_used;
1344                 }
1345         } else {
1346                 if (rev) {
1347                         find_func = rte_fbarray_find_prev_free;
1348                         find_contig_func = rte_fbarray_find_rev_contig_free;
1349                 } else {
1350                         find_func = rte_fbarray_find_next_free;
1351                         find_contig_func = rte_fbarray_find_contig_free;
1352                 }
1353         }
1354
1355         cur_idx = start;
1356         biggest_idx = -1; /* default is error */
1357         biggest_len = 0;
1358         for (;;) {
1359                 cur_idx = find_func(arr, cur_idx);
1360
1361                 /* block found, check its length */
1362                 if (cur_idx >= 0) {
1363                         cur_len = find_contig_func(arr, cur_idx);
1364                         /* decide where we go next */
1365                         next_idx = rev ? cur_idx - cur_len : cur_idx + cur_len;
1366                         /* move current index to start of chunk */
1367                         cur_idx = rev ? next_idx + 1 : cur_idx;
1368
1369                         if (cur_len > biggest_len) {
1370                                 biggest_idx = cur_idx;
1371                                 biggest_len = cur_len;
1372                         }
1373                         cur_idx = next_idx;
1374                         /* in reverse mode, next_idx may be -1 if chunk started
1375                          * at array beginning. this means there's no more work
1376                          * to do.
1377                          */
1378                         if (cur_idx < 0)
1379                                 break;
1380                 } else {
1381                         /* nothing more to find, stop. however, a failed API
1382                          * call has set rte_errno, which we want to ignore, as
1383                          * reaching the end of fbarray is not an error.
1384                          */
1385                         rte_errno = 0;
1386                         break;
1387                 }
1388         }
1389         /* if we didn't find anything at all, set rte_errno */
1390         if (biggest_idx < 0)
1391                 rte_errno = used ? ENOENT : ENOSPC;
1392
1393         rte_rwlock_read_unlock(&arr->rwlock);
1394         return biggest_idx;
1395 }
1396
1397 int
1398 rte_fbarray_find_biggest_free(struct rte_fbarray *arr, unsigned int start)
1399 {
1400         return fbarray_find_biggest(arr, start, false, false);
1401 }
1402
1403 int
1404 rte_fbarray_find_biggest_used(struct rte_fbarray *arr, unsigned int start)
1405 {
1406         return fbarray_find_biggest(arr, start, true, false);
1407 }
1408
1409 int
1410 rte_fbarray_find_rev_biggest_free(struct rte_fbarray *arr, unsigned int start)
1411 {
1412         return fbarray_find_biggest(arr, start, false, true);
1413 }
1414
1415 int
1416 rte_fbarray_find_rev_biggest_used(struct rte_fbarray *arr, unsigned int start)
1417 {
1418         return fbarray_find_biggest(arr, start, true, true);
1419 }
1420
1421
1422 int
1423 rte_fbarray_find_contig_free(struct rte_fbarray *arr, unsigned int start)
1424 {
1425         return fbarray_find_contig(arr, start, true, false);
1426 }
1427
1428 int
1429 rte_fbarray_find_contig_used(struct rte_fbarray *arr, unsigned int start)
1430 {
1431         return fbarray_find_contig(arr, start, true, true);
1432 }
1433
1434 int
1435 rte_fbarray_find_rev_contig_free(struct rte_fbarray *arr, unsigned int start)
1436 {
1437         return fbarray_find_contig(arr, start, false, false);
1438 }
1439
1440 int
1441 rte_fbarray_find_rev_contig_used(struct rte_fbarray *arr, unsigned int start)
1442 {
1443         return fbarray_find_contig(arr, start, false, true);
1444 }
1445
1446 int
1447 rte_fbarray_find_idx(const struct rte_fbarray *arr, const void *elt)
1448 {
1449         void *end;
1450         int ret = -1;
1451
1452         /*
1453          * no need to synchronize as it doesn't matter if underlying data
1454          * changes - we're doing pointer arithmetic here.
1455          */
1456
1457         if (arr == NULL || elt == NULL) {
1458                 rte_errno = EINVAL;
1459                 return -1;
1460         }
1461         end = RTE_PTR_ADD(arr->data, arr->elt_sz * arr->len);
1462         if (elt < arr->data || elt >= end) {
1463                 rte_errno = EINVAL;
1464                 return -1;
1465         }
1466
1467         ret = RTE_PTR_DIFF(elt, arr->data) / arr->elt_sz;
1468
1469         return ret;
1470 }
1471
1472 void
1473 rte_fbarray_dump_metadata(struct rte_fbarray *arr, FILE *f)
1474 {
1475         struct used_mask *msk;
1476         unsigned int i;
1477
1478         if (arr == NULL || f == NULL) {
1479                 rte_errno = EINVAL;
1480                 return;
1481         }
1482
1483         if (fully_validate(arr->name, arr->elt_sz, arr->len)) {
1484                 fprintf(f, "Invalid file-backed array\n");
1485                 goto out;
1486         }
1487
1488         /* prevent array from changing under us */
1489         rte_rwlock_read_lock(&arr->rwlock);
1490
1491         fprintf(f, "File-backed array: %s\n", arr->name);
1492         fprintf(f, "size: %i occupied: %i elt_sz: %i\n",
1493                         arr->len, arr->count, arr->elt_sz);
1494
1495         msk = get_used_mask(arr->data, arr->elt_sz, arr->len);
1496
1497         for (i = 0; i < msk->n_masks; i++)
1498                 fprintf(f, "msk idx %i: 0x%016" PRIx64 "\n", i, msk->data[i]);
1499 out:
1500         rte_rwlock_read_unlock(&arr->rwlock);
1501 }