Bug #79517
closedlibcephfs/client: ceph_readdirplus_r() skips entry at a dirfrag hash boundary
0%
Description
Summary¶
While testing a fresh libcephfs directory stream, I found that a snapshot directory ceph_readdirplus_r() missed entries.
I added logs in Client.cc to follow the entry. They show that e_9_84 is decoded from the MDS reply and added to dirp->buffer, but std::lower_bound() skips it because the entry and cursor use different offset formats:
dirp->offset -> fragment-format cursor entry.offset -> hash-order cursor
No error is returned; the API caller simply never sees the entry.
Environment¶
Base: v18.2.7 API: ceph_readdirplus_r() mds_bal_split_size = 3 mds_bal_merge_size = 0
Directory:
/root7/.snap/snap0/e_0_0/e_1_2/e_2_2/e_3_0/e_4_0/e_5_0/e_6_0/e_7_0/e_8_0
I used the small split size intentionally to create many empty and deeply split fragments. This makes the issue easy to reproduce, but the fragment tree itself is legal.
Reproduction outline¶
- Configure the MDS with the values above.
- Create a directory tree with the following fan-out at each level:
4 x 4 x 100 x 1 x 1 x 1 x 1 x 1 x 1 x 100. - Name the levels
e_0_*throughe_9_*. The last level containse_9_0throughe_9_99. - Create snapshot
snap0after the tree is populated. - Enumerate a final-level directory from
snap0directly withceph_readdirplus_r(). - Compare the 100 source names with the names returned by the API.
Expected result¶
All 100 entries, including e_9_84, are returned exactly once.
Actual result¶
Only 99 entries are returned. e_9_84 is silently omitted.
Relevant logs¶
The client first walks through several empty fragments:
_readdir_r_cb-->1--> fg=000001001010100110000000*, buffer.size()=0, dirp->offset=108168407752376322 _readdir_r_cb-->1--> fg=000001001010100110000001*, buffer.size()=0, dirp->offset=108168408020811778 _readdir_r_cb-->1--> fg=000001001010100110000010*, buffer.size()=0, dirp->offset=108168408289247234
It then receives e_9_84 and adds it to the readdir buffer, but does not return it:
insert_readdir_results-->1--> fg=000001001010100110000011*, dname=e_9_84 insert_readdir_results-->2--> fg=000001001010100110000011*, dname=e_9_84, dn->offset=1148499922480267266 _readdir_r_cb-->1--> fg=000001001010100110000011*, buffer.size()=1, dirp->offset=108168408557682690 _readdir_r_cb-->2--> fg=000001001010100110000011*, dname=e_9_84, dn->offset=1148499922480267266 _readdir_r_cb-->1--> fg=000001001010100110000100*, buffer.size()=0, dirp->offset=108168408826118146
The -->3 log is printed only after an entry passes lower_bound(). There is no such log for e_9_84; the next message is already for the next fragment.
For comparison, a later entry, e_9_22, does pass the same check:
_readdir_r_cb-->1--> fg=000010010*, buffer.size()=1, dirp->offset=40690726320734210 _readdir_r_cb-->2--> dname=e_9_22, dn->offset=1148584734662590466 _readdir_r_cb-->3--> dname=e_9_22, dn->offset=1148584734662590466 _readdir_r_cb-->1--> dirp->offset=1148584734662590467
Offset discrepancy¶
The relevant dir_result_t constants are:
SHIFT = 28 HASH = 0x0ff0000000000000
Ceph builds these offsets with:
static uint64_t make_fpos(unsigned h, unsigned l, bool hash) {
uint64_t v = ((uint64_t)h << SHIFT) | (uint64_t)l;
if (hash) {
v |= HASH;
}
return v;
}
h = encoded fragment or filename hash l = low position (2 for the first real entry) hash = whether to add the HASH-format marker
These are the two values seen in the failing log:
entry offset = 1148499922480267266 = 0x0ff04a9830000002 cursor offset = 108168408557682690 = 0x01804a9830000002
Constructing the hash-formatted entry offset¶
For e_9_84, the client uses the filename hash and asks make_fpos() to add the HASH marker:
h = filename hash 0x04a983 l = 2 hash = true ((uint64_t)h << 28) | l = 0x00004a9830000002 0x00004a9830000002 | 0x0ff0000000000000 HASH marker -------------------- 0x0ff04a9830000002
The result is decimal 1148499922480267266, exactly matching the entry offset in the log.
Constructing the fragment-formatted cursor¶
fragment = 000001001010100110000011
= 0000 0100 1010 1001 1000 0011
= 0x04a983
fragment bits = 24 decimal = 0x18
encoded h = [0x18][0x04a983] = 0x1804a983
l = 2
hash = false
((uint64_t)0x1804a983 << 28) | 2
= 0x01804a9830000002
The result is decimal 108168408557682690, exactly matching dirp->offset in the log.
Field breakdown¶
Entry offset:
0x0ff | 04a983 | 0000002
^ ^ ^
| | +-- low position = 2
| +------------ filename hash = 0x04a983
+-------------------- HASH marker
Cursor offset:
0x018 | 04a983 | 0000002
^ ^ ^
| | +-- low position = 2
| +------------ fragment value = 0x04a983
+-------------------- fragment bits = 0x18 = 24
The important point can be seen by adding the HASH marker to the cursor:
0x01804a9830000002 cursor | 0x0ff0000000000000 HASH marker -------------------- 0x0ff04a9830000002 entry offset
The result is exactly the entry offset. In other words, both values describe the same logical position, but one is marked as a fragment cursor and the other as a hash-order cursor.
Why lower_bound() skips it¶
To find the first unread entry, lower_bound() eventually calls ceph_frag_compare() through fpos_cmp(). The two offsets are decoded as follows:
| Side | Value | Fragment bits |
|---|---|---|
| Hash-formatted entry | 0x04a983 |
0 |
| Fragment-formatted cursor | 0x04a983 |
24 |
The hash/fragment values are equal, so ceph_frag_compare() moves on to the bit counts:
0 < 24 -> entry is considered before the cursor
This makes the entry look as though it is already behind the cursor. Since it is the only buffered entry, lower_bound() returns dirp->buffer.end() and e_9_84 is skipped.
Root cause¶
The MDS correctly tells the client that the reply uses hash order:
CEPH_READDIR_HASH_ORDER | CEPH_READDIR_OFFSET_HASH
Client::insert_readdir_results() reads that flag and uses it when creating each dentry offset:
bool hash_order = ((unsigned)flags & CEPH_READDIR_HASH_ORDER);
...
dn->offset = dir_result_t::make_fpos(hash, readdir_offset++, true);
However, it does not apply the same mode to dirp->offset. At this point, the entries are in hash format while the stream cursor is still in fragment format.
This is normally hidden because the first successfully returned dentry updates the cursor using its hash-formatted offset:
uint64_t next_off = entry.offset + 1;
...
dirp->offset = next_off;
An empty fragment has no entry that can perform this update. In this case the first candidate is in a 24-bit fragment, where the entry hash is exactly equal to the fragment boundary. It is therefore skipped before it gets a chance to switch the cursor into hash mode.
Proposed fix¶
The cursor should be switched to hash-order mode as soon as the reply advertises that mode:
bool end = ((unsigned)flags & CEPH_READDIR_FRAG_END);
bool hash_order = ((unsigned)flags & CEPH_READDIR_HASH_ORDER);
+ if (hash_order) {
+ dirp->set_hash_order();
+ }
Suggested location: Client::insert_readdir_results() in src/client/Client.cc.
For this particular failure, that change converts the cursor from:
0x01804a9830000002 -> 0x0ff04a9830000002
The cursor then matches the entry's format, so the entry compares equal and is returned. The change is guarded by the MDS reply flag, and set_hash_order() is idempotent.
I do not think changing only lower_bound() or fpos_cmp() would be sufficient. dirp->hash_order() is also used for fragment advancement, offset-hash requests, cached readdir, telldir(), and seekdir().
Files
MS Updated by Md Mahamudur Rahaman Sajib 4 months ago
- Category set to Correctness/Safety
MS Updated by Md Mahamudur Rahaman Sajib 4 months ago
- Description updated (diff)
MS Updated by Md Mahamudur Rahaman Sajib 4 months ago
- Status changed from New to Fix Under Review
- Pull request ID set to 71075
VS Updated by Venky Shankar 4 months ago
- Target version set to v22.0.0
- Source set to Community (dev)
- Backport set to umbrella,tentacle,squid
- Component(FS) Client added
VS Updated by Venky Shankar 3 months ago
- Component(FS) libcephfs added
UB Updated by Upkeep Bot 3 months ago
- Status changed from Fix Under Review to Pending Backport
- Merge Commit set to 6fcbb53dda983650d44e91a3210621ab90e08d95
- Fixed In set to v21.3.0-3031-g6fcbb53dda
- Upkeep Timestamp set to 2026-09-08T05:43:46+00:00
UB Updated by Upkeep Bot 3 months ago
- Copied to Backport #80378: umbrella: libcephfs/client: ceph_readdirplus_r() skips entry at a dirfrag hash boundary added
UB Updated by Upkeep Bot 3 months ago
- Copied to Backport #80379: squid: libcephfs/client: ceph_readdirplus_r() skips entry at a dirfrag hash boundary added
UB Updated by Upkeep Bot 3 months ago
- Copied to Backport #80380: tentacle: libcephfs/client: ceph_readdirplus_r() skips entry at a dirfrag hash boundary added
UB Updated by Upkeep Bot 3 months ago
- Status changed from Pending Backport to Backporting
- Upkeep Timestamp changed from 2026-09-08T05:43:46+00:00 to 2026-09-08T12:29:51+00:00