Project

General

Profile

Actions

Bug #79517

closed
MS MS

libcephfs/client: ceph_readdirplus_r() skips entry at a dirfrag hash boundary

Bug #79517: libcephfs/client: ceph_readdirplus_r() skips entry at a dirfrag hash boundary

Added by Md Mahamudur Rahaman Sajib 4 months ago. Updated 3 months ago.

Status:
Backporting
Priority:
High
Category:
Correctness/Safety
Target version:
Due date:
% Done:

0%

Source:
Community (dev)
Backport:
umbrella,tentacle,squid
Regression:
No
Severity:
2 - major
Reviewed:
Affected Versions:
ceph-qa-suite:
Component(FS):
Client, libcephfs
Labels (FS):
Pull request ID:
Tags (freeform):
Fixed In:
v21.3.0-3031-g6fcbb53dda
Released In:
Upkeep Timestamp:
2026-09-08T12:29:51+00:00

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

  1. Configure the MDS with the values above.
  2. 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.
  3. Name the levels e_0_* through e_9_*. The last level contains e_9_0 through e_9_99.
  4. Create snapshot snap0 after the tree is populated.
  5. Enumerate a final-level directory from snap0 directly with ceph_readdirplus_r().
  6. 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

client_e_9_84.log (215 KB) client_e_9_84.log Md Mahamudur Rahaman Sajib, 08/15/2026 12:25 PM

Related issues 3 (1 open — 2 closed)

Copied to CephFS - Backport #80378: umbrella: libcephfs/client: ceph_readdirplus_r() skips entry at a dirfrag hash boundaryFix Under ReviewJos Collin

Actions
Copied to CephFS - Backport #80379: squid: libcephfs/client: ceph_readdirplus_r() skips entry at a dirfrag hash boundaryResolvedJos Collin

Actions
Copied to CephFS - Backport #80380: tentacle: libcephfs/client: ceph_readdirplus_r() skips entry at a dirfrag hash boundaryResolvedJos Collin

Actions

MS Updated by Md Mahamudur Rahaman Sajib 4 months ago Actions #1

  • Category set to Correctness/Safety

MS Updated by Md Mahamudur Rahaman Sajib 4 months ago Actions #2

  • Description updated (diff)

MS Updated by Md Mahamudur Rahaman Sajib 4 months ago Actions #3

  • Status changed from New to Fix Under Review
  • Pull request ID set to 71075

VS Updated by Venky Shankar 4 months ago Actions #4

  • 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 Actions #5

  • Component(FS) libcephfs added

UB Updated by Upkeep Bot 3 months ago Actions #6

  • 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 Actions #7

  • 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 Actions #8

  • 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 Actions #9

  • 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 Actions #10

  • 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
Actions

Also available in: PDF Atom