diff options
Diffstat (limited to 'lib/xarray.c')
-rw-r--r-- | lib/xarray.c | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/lib/xarray.c b/lib/xarray.c index 24494f42daa6..70e04810c8c2 100644 --- a/lib/xarray.c +++ b/lib/xarray.c | |||
@@ -1444,6 +1444,86 @@ void *xa_find_after(struct xarray *xa, unsigned long *indexp, | |||
1444 | } | 1444 | } |
1445 | EXPORT_SYMBOL(xa_find_after); | 1445 | EXPORT_SYMBOL(xa_find_after); |
1446 | 1446 | ||
1447 | static unsigned int xas_extract_present(struct xa_state *xas, void **dst, | ||
1448 | unsigned long max, unsigned int n) | ||
1449 | { | ||
1450 | void *entry; | ||
1451 | unsigned int i = 0; | ||
1452 | |||
1453 | rcu_read_lock(); | ||
1454 | xas_for_each(xas, entry, max) { | ||
1455 | if (xas_retry(xas, entry)) | ||
1456 | continue; | ||
1457 | dst[i++] = entry; | ||
1458 | if (i == n) | ||
1459 | break; | ||
1460 | } | ||
1461 | rcu_read_unlock(); | ||
1462 | |||
1463 | return i; | ||
1464 | } | ||
1465 | |||
1466 | static unsigned int xas_extract_marked(struct xa_state *xas, void **dst, | ||
1467 | unsigned long max, unsigned int n, xa_mark_t mark) | ||
1468 | { | ||
1469 | void *entry; | ||
1470 | unsigned int i = 0; | ||
1471 | |||
1472 | rcu_read_lock(); | ||
1473 | xas_for_each_marked(xas, entry, max, mark) { | ||
1474 | if (xas_retry(xas, entry)) | ||
1475 | continue; | ||
1476 | dst[i++] = entry; | ||
1477 | if (i == n) | ||
1478 | break; | ||
1479 | } | ||
1480 | rcu_read_unlock(); | ||
1481 | |||
1482 | return i; | ||
1483 | } | ||
1484 | |||
1485 | /** | ||
1486 | * xa_extract() - Copy selected entries from the XArray into a normal array. | ||
1487 | * @xa: The source XArray to copy from. | ||
1488 | * @dst: The buffer to copy entries into. | ||
1489 | * @start: The first index in the XArray eligible to be selected. | ||
1490 | * @max: The last index in the XArray eligible to be selected. | ||
1491 | * @n: The maximum number of entries to copy. | ||
1492 | * @filter: Selection criterion. | ||
1493 | * | ||
1494 | * Copies up to @n entries that match @filter from the XArray. The | ||
1495 | * copied entries will have indices between @start and @max, inclusive. | ||
1496 | * | ||
1497 | * The @filter may be an XArray mark value, in which case entries which are | ||
1498 | * marked with that mark will be copied. It may also be %XA_PRESENT, in | ||
1499 | * which case all entries which are not NULL will be copied. | ||
1500 | * | ||
1501 | * The entries returned may not represent a snapshot of the XArray at a | ||
1502 | * moment in time. For example, if another thread stores to index 5, then | ||
1503 | * index 10, calling xa_extract() may return the old contents of index 5 | ||
1504 | * and the new contents of index 10. Indices not modified while this | ||
1505 | * function is running will not be skipped. | ||
1506 | * | ||
1507 | * If you need stronger guarantees, holding the xa_lock across calls to this | ||
1508 | * function will prevent concurrent modification. | ||
1509 | * | ||
1510 | * Context: Any context. Takes and releases the RCU lock. | ||
1511 | * Return: The number of entries copied. | ||
1512 | */ | ||
1513 | unsigned int xa_extract(struct xarray *xa, void **dst, unsigned long start, | ||
1514 | unsigned long max, unsigned int n, xa_mark_t filter) | ||
1515 | { | ||
1516 | XA_STATE(xas, xa, start); | ||
1517 | |||
1518 | if (!n) | ||
1519 | return 0; | ||
1520 | |||
1521 | if ((__force unsigned int)filter < XA_MAX_MARKS) | ||
1522 | return xas_extract_marked(&xas, dst, max, n, filter); | ||
1523 | return xas_extract_present(&xas, dst, max, n); | ||
1524 | } | ||
1525 | EXPORT_SYMBOL(xa_extract); | ||
1526 | |||
1447 | #ifdef XA_DEBUG | 1527 | #ifdef XA_DEBUG |
1448 | void xa_dump_node(const struct xa_node *node) | 1528 | void xa_dump_node(const struct xa_node *node) |
1449 | { | 1529 | { |