Skip to contentSkip to conversation

fstests: Verify behaviour of "seek" in a directory

PATCH In review NeilBrown 5 participants 14 messages Aug 27 – Sep 1

Subsystems (2)
lore
Share & tools
lore

NeilBrown

author
#0 lore raw

Hi,

I would like to propose the following test to be added to fstests. It checks the behaviour of a directory with respect to seek. As described in the patch and the code, it expects names which are not removed or moved to have a stable position in the result of getdents64 with respect to each other and with respect to any seek offset (d_off) reported of any entry. This means that if we stop and any point while scanning a directory, remember the most recent d_off, close the directory, then later open and seek to that offset and keep reading, then we will find all the names that existed for the whole time and see those only once.

Names that are added or removed are not tests as we expect them possibly appear not at all or even multiple times.

My particular motivation is to confirm that the current tmpfs doesn't behave correctly, and to test an alternate implementation that I will propose which does pass this test.

I discovered that while xfs and ext4 pass the test, btrfs doesn't. It appears that with btrfs the target of a rename does not keep its position in the directory listing order.

Thanks,
NeilBrown

[PATCH] fstests: generic: Add test of seek in directories

author
#1 lore raw

PATCH 1/1 fstests: generic: Add test of seek in directories

From: NeilBrown <[email protected]>

Add a test for consistency of readdir (getdents64) results. The expectation is that name which exist before a readdir starts, and still exist when the readdir completes, and which have not been removed or renamed away and re-created, should appear exactly once. Also if the fd on the file is closed in the middle of a readdir and re-opened with a seek to some offset previously reported, the same sequence should be reported as if the file was kept open with no seeking. This last is important for nfsd.

Names which are the target of a rename have not been removed or renamed away, and so they should be stable too.

The test involves creating a large number of names and designating half as "stable". Those which aren't "stable" may be removed, recreated, or renamed to something else. Names which are stable might be the target of a rename, but will always exist.

The test then reads the directory to find the order of the stable name, which should never change. It also find the "d_off" of all names. These d_off should be usable as a "seek" offset to find at least all the stable names that came after that point in the original listing.

After creating the names and finding the order, a loop which repeatly:
- makes random changes to unstable names
- checks the complete order of stable names in a new readdir
- checks what appears after a seek() to a randomly chosen offet

In Linux 7.2 this test always passes for xfs and ext4 but fails for btrfs unless we suppress renames with a "stable" name as target. It also fails for tmpfs.

To support NFS it is possible to suppress RENAME_EXCHANGE tests.

---
 .gitignore            |   1 +
 src/Makefile          |   2 +-
 src/t_dir_seek.c      | 545 ++++++++++++++++++++++++++++++++++++++++++
 tests/generic/799     |  25 ++
 tests/generic/799.out |   3 +
 5 files changed, 575 insertions(+), 1 deletion(-)
 create mode 100644 src/t_dir_seek.c
 create mode 100755 tests/generic/799
 create mode 100644 tests/generic/799.out
.gitignore +1 -0
index 0b6b94529892..8645520cbe85 100644
@@ -144,6 +144,7 @@ tags
 /src/t_create_short_dirs
 /src/t_dir_offset
 /src/t_dir_offset2
+/src/t_dir_seek
 /src/t_dir_type
 /src/t_encrypted_d_revalidate
 /src/t_enospc
src/Makefile +1 -1
index 76cf50c3e169..17e7f1560033 100644
@@ -36,7 +36,7 @@ LINUX_TARGETS = xfsctl bstat t_mtab getdevicesize preallo_rw_pattern_reader \
 	fscrypt-crypt-util bulkstat_null_ocount splice-test chprojid_fail \
 	detached_mounts_propagation ext4_resize t_readdir_3 splice2pipe \
 	uuid_ioctl t_snapshot_deleted_subvolume fiemap-fault min_dio_alignment \
-	rw_hint fs-monitor btrfs_ioctl
+	rw_hint fs-monitor btrfs_ioctl t_dir_seek
 
 EXTRA_EXECS = dmerror fill2attr fill2fs fill2fs_check scaleread.sh \
 	      btrfs_crc32c_forged_name.py popdir.pl popattr.py \
src/t_dir_seek.c new file +545 -0

545 lines not on this page · raw message

tests/generic/799 new file +25 -0
new file mode 100755
index 000000000000..07279394ef43
@@ -0,0 +1,25 @@
+#! /bin/bash
+# SPDX-License-Identifier: GPL-2.0
+# Copyright (c) 2025 NeilBrown
+#
+# Check dir list and offsets are stable in the fast of churn.
+# Name that are not unlinks or renamed should rename in-order w.r.t
+# each other and any seek positions reported.
+
+. ./common/preamble
+_begin_fstest auto dir quick
+
+_require_test
+_require_test_program "t_dir_seek"
+
+extra=""
+if [ "$FSTYP" == "nfs" ]; then
+    extra="-O noexchange"
+fi
+
+# provide a seed as it is reported in the output
+$here/src/t_dir_seek -p $TEST_DIR -S 1234567 $extra
+
+# success, all done
+echo "*** done"
+status=0
tests/generic/799.out new file +3 -0
new file mode 100644
index 000000000000..3acd51a1847b
@@ -0,0 +1,3 @@
+QA output created by 799
+Passed all 100 loops with seed 1234567
+*** done

base-commit: 56c410ad0f69da5b13c5807bc47b4876dcfa02b2

Signature · 1 line
2.50.0.107.gf914562f5916.dirty
#2 lore raw

Re: [PATCH] fstests: generic: Add test of seek in directories

Quoted 8 lines · NeilBrown

Add a test for consistency of readdir (getdents64) results. The expectation is that name which exist before a readdir starts, and still exist when the readdir completes, and which have not been removed or renamed away and re-created, should appear exactly once. Also if the fd on the file is closed in the middle of a readdir and re-opened with a seek to some offset previously reported, the same sequence should be reported as if the file was kept open with no seeking. This last is important for nfsd.

I'm not sure it's required by POSIX:

The behavior is unspecified if lseek() is used to set the file offset to a value other than zero or a value returned by a previous call to lseek() on the same open file description."

https://pubs.opengroup.org/onlinepubs/9799919799/functions/posix_getdents.html

We can of course require stronger semantics from Linux filesystems than POSIX requires, We should discuss the benefits & costs of doing so (and I have no idea what the costs to btrfs or tmpfs might be).

author
#3 lore raw

Re: [PATCH] fstests: generic: Add test of seek in directories

Quoted 10 lines · Matthew Wilcox
Quoted 8 lines · NeilBrown

Add a test for consistency of readdir (getdents64) results. The expectation is that name which exist before a readdir starts, and still exist when the readdir completes, and which have not been removed or renamed away and re-created, should appear exactly once. Also if the fd on the file is closed in the middle of a readdir and re-opened with a seek to some offset previously reported, the same sequence should be reported as if the file was kept open with no seeking. This last is important for nfsd.

Quoted 7 lines

I'm not sure it's required by POSIX:

The behavior is unspecified if lseek() is used to set the file offset to a value other than zero or a value returned by a previous call to lseek() on the same open file description."

https://pubs.opengroup.org/onlinepubs/9799919799/functions/posix_getdents.html

If you happened to know the sizes of the all the names in the directory, you could make a sequence of getdents calls which each return precisely 1 entry, You could then use lseek to determine the seek offset at every point.
Linux getdents64 makes this a bit simpler by returning exactly that same number (the seek offset to the next name) in the d_off field.

So posix certainly allows, indirectly, seeking to each d_off.

Posix refers to one directory entry "immediately following" another which clearly implies a well defined sequence (if you exclude names added and removed during the read).

Also glibc currently expects exactly this behaviour for implementing telldir/seekdir.

Quoted 5 lines

We can of course require stronger semantics from Linux filesystems than POSIX requires, We should discuss the benefits & costs of doing so (and I have no idea what the costs to btrfs or tmpfs might be).

I think a strong argument for Linux needing something is that NFS needs working directory offsets to be able to support READDIR, because there is no "OPEN" request for directories.

I agree it would be good to discuss this and hear other perspectives, but I cannot see a lot of flexibility if we want a system that works consistently.

Fixing tmpfs is easy. I have a patch which makes it work much better with less code. Chuck quite reasonably wanted some objective measure of "better" and suggested I write some test code for fstests. Hence this patch.

I know less about btrfs. It appears to always add new names to the end of the directory listing. When you rename over an existing name, that existing name is relocated to the end. I can only guess why it might do that. I have no idea if it "needs" to do that.

Fun fact: This behaviour of btrfs (which I think is perfectly defensible for new names) resulted in generic/736 (which I think is an unreasonable test to impose) which btrfs "fixed" with a mechanism that doesn't work over NFS. i.e. it doesn't work if you close and re-open the file for each getdents call.
If I mount a btrfs filesystem over nfs with rsize=4096, then generic/736 fails.

The link you provided says:
If a sequence of calls to posix_getdents() is made that reads from offset zero to end-of-file and a file is removed from or added to the directory between the first and last of those calls, whether the sequence of calls returns an entry for that file is unspecified.

which unfortunately doesn't cover what happens when a file is renamed. When renamed to a non-existing name, it might be reasonable to describe this as "removed from" and "added to" so its appearance could be unspecified.
When renamed to an existing name, I don't think it reasonable to describe the target name a being either "removed from" or "added to" the directory.

Thanks for taking an interest,
NeilBrown

#4 lore raw

Re: [PATCH] fstests: generic: Add test of seek in directories

The key requirements come from readdir() and seekdir(). To quote from readdir() specification:

If a file is removed from or added to the directory after the most recent call to opendir() or rewinddir(), whether a subsequent call to readdir() returns an entry for that file is unspecified.

And then from seekdir():

The seekdir() function shall set the position of the next readdir() operation on the directory stream specified by dirp to the position specified by loc. The value of loc should have been returned from an earlier call to telldir(). The new position reverts to the one associated with the directory stream when telldir() was performed.

If the value of loc was not obtained from an earlier call to telldir(), or if a call to rewinddir() occurred between the call to telldir() and the call to seekdir(), the results of subsequent calls to readdir() are unspecified.

Telldir() is specified to return a "cookie". It does not have to be a offset, and the DIR * returned by opendir() and consumed by readdir() does not have to be backed by a file descriptor.

Historically, jfs implemented an whole an extra on-disk btree in order to implement the insane POSIX seekdir()/telldir() semantics. (Meaning every single directory insert or deletion required at least one extra disk seek.) Ext4 returns directory entries in a very sub-optimal hash tree order, requiring lots of extra disk seeks, in order to provide this guarantee. This is also something that historically has been enforced by Posix Compliance Test Suites (PCTS) for those people who believed that Posix compliance was relevant.

And, yes we also go through all of this pain and performance hit not just because of Posix compliance, but also because NFS requires it. Personally, supporting NFS is *way* more important than Posix compliance, but people of good will can have different priorities. :-)

For this reason, I would support having a test to provide the gaurantee needed for Posix and NFS compatibility. Not for Posix's sake, but for NFS's.

Cheers,

- Ted

author
#5 lore raw

Re: [PATCH] fstests: generic: Add test of seek in directories

Quoted 33 lines · Theodore Tso

The key requirements come from readdir() and seekdir(). To quote from readdir() specification:

If a file is removed from or added to the directory after the most recent call to opendir() or rewinddir(), whether a subsequent call to readdir() returns an entry for that file is unspecified.

And then from seekdir():

The seekdir() function shall set the position of the next readdir() operation on the directory stream specified by dirp to the position specified by loc. The value of loc should have been returned from an earlier call to telldir(). The new position reverts to the one associated with the directory stream when telldir() was performed.

If the value of loc was not obtained from an earlier call to telldir(), or if a call to rewinddir() occurred between the call to telldir() and the call to seekdir(), the results of subsequent calls to readdir() are unspecified.

Telldir() is specified to return a "cookie". It does not have to be a offset, and the DIR * returned by opendir() and consumed by readdir() does not have to be backed by a file descriptor.

Historically, jfs implemented an whole an extra on-disk btree in order to implement the insane POSIX seekdir()/telldir() semantics. (Meaning every single directory insert or deletion required at least one extra disk seek.) Ext4 returns directory entries in a very sub-optimal hash tree order, requiring lots of extra disk seeks, in order to provide this guarantee. This is also something that historically has been enforced by Posix Compliance Test Suites (PCTS) for those people who believed that Posix compliance was relevant.

Quoted 4 lines

And, yes we also go through all of this pain and performance hit not just because of Posix compliance, but also because NFS requires it. Personally, supporting NFS is *way* more important than Posix compliance, but people of good will can have different priorities. :-)

Given that the requirements predates the htree design, isn't this "pain and performance hit" a deliberate design choice? Or maybe an unfortunate design omission? Such things can be fixed.

Presumably as well as the "extra disk seeks" the current design prevents read-ahead from being effective.

ext4 already has two indexing schemes - indirect and extent-based. How hard would it be to add a third that effectively moved the current directory index blocks out of the "file" and into the "file-index"? Obviously the 32 block address in dx_entry would need to become a 48 bit device address etc, and there are bound to be details I've glossed over, but this approach would amortise all metadata access of a lot data access, and would allow read-ahead.

So if there is really a problem, I'm sure it can be fixed.

Quoted 4 lines

For this reason, I would support having a test to provide the gaurantee needed for Posix and NFS compatibility. Not for Posix's sake, but for NFS's.

Thanks for the support.

NeilBrown

#6 lore raw

Re: [PATCH] fstests: generic: Add test of seek in directories

Quoted 3 lines · NeilBrown

Given that the requirements predates the htree design, isn't this "pain and performance hit" a deliberate design choice? Or maybe an unfortunate design omission? Such things can be fixed.

The requirements assumed that the directory was a linear structure, much like the V7 and BSD FFS structure. That's why telldir()/seekdir() interface used a 32-biit cookie --- e.g., a seek offset. This doesn't deal well with a b-tree or equivalent structure where when you insert an item into the tree, the tree might require a node to be split or the tree to be rebalanced.

Quoted 7 lines

ext4 already has two indexing schemes - indirect and extent-based. How hard would it be to add a third that effectively moved the current directory index blocks out of the "file" and into the "file-index"? Obviously the 32 block address in dx_entry would need to become a 48 bit device address etc, and there are bound to be details I've glossed over, but this approach would amortise all metadata access of a lot data access, and would allow read-ahead.

The primary detail that you've glossed over is that the tree would have to be a B-tree. Ext4 currently uses a b+tree which means that we don't need to store the file name (key) in the index nodes. The index node in the htree is a fixed size, and compact, and this allows for a much greater fanout (340 nodes in each node). This makes the tree much shallower, and reduces the number of seeks when doing lookups.

Switching to a b-tree would almost double the size of the overhead of the tree, and the tree would be deeper, and thus require more seeks when doing a lookup.

So it's a tradeoff, and it depends on whether you think readdir or directory lookups are more frequent.

Cheers,

- Ted

#7 lore raw

Re: [PATCH] fstests: generic: Add test of seek in directories

In reply to NeilBrown
Quoted 4 lines · NeilBrown

If you happened to know the sizes of the all the names in the directory, you could make a sequence of getdents calls which each return precisely 1 entry, You could then use lseek to determine the seek offset at every point.

No, you can't. Despite the historic naming d_off is not an offset, but a cookie. You can not arithmetics on it.

Quoted 4 lines

Linux getdents64 makes this a bit simpler by returning exactly that same number (the seek offset to the next name) in the d_off field.

So posix certainly allows, indirectly, seeking to each d_off.

Posix and Linux allow to seekdir to each cookie returned from telldir, it does not allow to do arithmetics on it.

Quoted 4 lines

Posix refers to one directory entry "immediately following" another which clearly implies a well defined sequence (if you exclude names added and removed during the read).

I think you're talking about posix_getdents here, which isn't really Posix as we know it, but was added in the 2024 edition without actually having relevant implementation so far. It does however implement the syscall-level API in most modern Unixes. It does not mention d_off at all, just the lseek-able file offset, though.

Quoted 3 lines

I think a strong argument for Linux needing something is that NFS needs working directory offsets to be able to support READDIR, because there is no "OPEN" request for directories.

I don't think anyone disputes the need for stable directory offsets, and we should (*knock on wood*) have implementation of native file systems that fail this.

What the test tries to force is a specific behavior for rename onto existing file names, replacing the original file name entry with a new one of the same name. The test expects that to reuse the previous d_off, which is not required by any real or de facto standard.

Quoted 4 lines

I know less about btrfs. It appears to always add new names to the end of the directory listing. When you rename over an existing name, that existing name is relocated to the end. I can only guess why it might do that. I have no idea if it "needs" to do that.

btrfs doesn't manage freespace for the d_off space (doing so is quite complicated and requires a lot of code in XFS for example), so it simply uses a monotonically increasing counter for the value reported in d_off. This simplifies things a lot, and should work well on 64-bit systems were you are basically impossible to round out of d_off values. It might be a lot more problematic on 32-bit systems because the seekdir/telldir cookie is a long and not a guaranteed 64-bit value.

Quoted 7 lines

Fun fact: This behaviour of btrfs (which I think is perfectly defensible for new names) resulted in generic/736 (which I think is an unreasonable test to impose) which btrfs "fixed" with a mechanism that doesn't work over NFS. i.e. it doesn't work if you close and re-open the file for each getdents call.
If I mount a btrfs filesystem over nfs with rsize=4096, then generic/736 fails.

That code and the tests looks a bit questionable, as readdir by definition can't every complete in other file systems either if you add new entries faster than the pace of readdir calls. I.e. if you replace the rename there with link calls adding new entries you run into the same issue with every file system.

Quoted 6 lines

The link you provided says:
If a sequence of calls to posix_getdents() is made that reads from offset zero to end-of-file and a file is removed from or added to the directory between the first and last of those calls, whether the sequence of calls returns an entry for that file is unspecified.

Quoted 7 lines

which unfortunately doesn't cover what happens when a file is renamed. When renamed to a non-existing name, it might be reasonable to describe this as "removed from" and "added to" so its appearance could be unspecified.
When renamed to an existing name, I don't think it reasonable to describe the target name a being either "removed from" or "added to" the directory.

If we are talking about files, it is very clear that the target over which is renamed is removed from the directory. And if the source was outside the directory it also is very clearly added. The only gray area is a source file that already was in the same directory.

author
#8 lore raw

Re: [PATCH] fstests: generic: Add test of seek in directories

Quoted 9 lines · Christoph Hellwig
Quoted 4 lines · NeilBrown

If you happened to know the sizes of the all the names in the directory, you could make a sequence of getdents calls which each return precisely 1 entry, You could then use lseek to determine the seek offset at every point.

No, you can't. Despite the historic naming d_off is not an offset, but a cookie. You can not arithmetics on it.

Quoted 7 lines
Quoted 4 lines

Linux getdents64 makes this a bit simpler by returning exactly that same number (the seek offset to the next name) in the d_off field.

So posix certainly allows, indirectly, seeking to each d_off.

Posix and Linux allow to seekdir to each cookie returned from telldir, it does not allow to do arithmetics on it.

I agree. No arithmetic, but seek is allowed.
The cookies should all be unique across a single readdir pass, and none of them may be zero as d_off is a seek address *after* the current name, and the seek address zero is *before* all names.

Quoted 11 lines
Quoted 5 lines

Posix refers to one directory entry "immediately following" another which clearly implies a well defined sequence (if you exclude names added and removed during the read).

I think you're talking about posix_getdents here, which isn't really Posix as we know it, but was added in the 2024 edition without actually having relevant implementation so far. It does however implement the syscall-level API in most modern Unixes. It does not mention d_off at all, just the lseek-able file offset, though.

The d_off provided by Linux is precisely a seek-address with a granularity of one name, rather than a granularity of one getdents call. This is implicit in the VFS implementation.

Quoted 8 lines
Quoted 4 lines

I think a strong argument for Linux needing something is that NFS needs working directory offsets to be able to support READDIR, because there is no "OPEN" request for directories.

I don't think anyone disputes the need for stable directory offsets, and we should (*knock on wood*) have implementation of native file systems that fail this.

"should not" ??

Quoted 5 lines

What the test tries to force is a specific behavior for rename onto existing file names, replacing the original file name entry with a new one of the same name. The test expects that to reuse the previous d_off, which is not required by any real or de facto standard.

That's debatable. The documents that I have found don't mention rename. Does that mean anything goes, or do that mean it doesn't get an exemption from the general rule that all names must be listed?

Quoted 6 lines
Quoted 5 lines

I know less about btrfs. It appears to always add new names to the end of the directory listing. When you rename over an existing name, that existing name is relocated to the end. I can only guess why it might do that. I have no idea if it "needs" to do that.

Quoted 7 lines

btrfs doesn't manage freespace for the d_off space (doing so is quite complicated and requires a lot of code in XFS for example), so it simply uses a monotonically increasing counter for the value reported in d_off. This simplifies things a lot, and should work well on 64-bit systems were you are basically impossible to round out of d_off values. It might be a lot more problematic on 32-bit systems because the seekdir/telldir cookie is a long and not a guaranteed 64-bit value.

I think that always allocating the next unused number when adding a name to a directory is perfectly reasonable when 64bit seek addressing is available. The only question is on whether a replacing rename involves "adding a name".

Quoted 9 lines
Quoted 8 lines

Fun fact: This behaviour of btrfs (which I think is perfectly defensible for new names) resulted in generic/736 (which I think is an unreasonable test to impose) which btrfs "fixed" with a mechanism that doesn't work over NFS. i.e. it doesn't work if you close and re-open the file for each getdents call.
If I mount a btrfs filesystem over nfs with rsize=4096, then generic/736 fails.

Quoted 5 lines

That code and the tests looks a bit questionable, as readdir by definition can't every complete in other file systems either if you add new entries faster than the pace of readdir calls. I.e. if you replace the rename there with link calls adding new entries you run into the same issue with every file system.

According to
https://www.spinics.net/lists/linux-btrfs/msg138653.html
this seem to come from
https://github.com/landley/toybox/issues/306
which suggests that

toybox find testdir -type f -print0 | xargs -0 -n1 sed -i s/a/b/

runs forever on btrfs. Certainly this is unexpected behaviour. Should it be fixed in the filesystem or in toybox/find?

I can understand toybox wanting to be as simple as possible. But readdir() doesn't make any promises about terminating.

I wouldn't object to btrfs "fixing" this by never reporting new names except that:
1/ the interaction with rename/replace is problematic
2/ the current fix doesn't work over NFS.

I think the rename issue can be fixed by simply reusing the existing offset when reusing a name. I might try a patch if a find some time.

I think a fix to make it work over NFS would be to report the entries in reverse order - most recently added to least recently added. This would effectively encode the timestamp (last_index) in the seek cookie instead of in btrfs_file_private.
Changing how the seek-cookie is interpreted could cause a hiccup for an NFS client which was in the middle of a READDIR when a server we reboot to a new verion of btrfs, but that is likely not significant.

Note that
https://pubs.opengroup.org/onlinepubs/9799919799/functions/posix_getdents.html

uses the word "incremented" in

the directory entry immediately following the last entry whose information was returned

but I don't think anyone seems interested in that detail.

Quoted 15 lines
Quoted 7 lines

The link you provided says:
If a sequence of calls to posix_getdents() is made that reads from offset zero to end-of-file and a file is removed from or added to the directory between the first and last of those calls, whether the sequence of calls returns an entry for that file is unspecified.

Quoted 7 lines

which unfortunately doesn't cover what happens when a file is renamed. When renamed to a non-existing name, it might be reasonable to describe this as "removed from" and "added to" so its appearance could be unspecified.
When renamed to an existing name, I don't think it reasonable to describe the target name a being either "removed from" or "added to" the directory.

Quoted 6 lines

If we are talking about files, it is very clear that the target over which is renamed is removed from the directory. And if the source was outside the directory it also is very clearly added. The only gray area is a source file that already was in the same directory.

As I explained in another email, I don't think it is reasonable to interpret "files" as mentioned in the documentation as "inodes" but only as "names". I think the text is sloppy and should be more explicit but if you uniformly assume "file" to mean "filesystem object" is doesn't make sense.

Thanks,
NeilBrown

#9 lore raw

Re: [PATCH] fstests: generic: Add test of seek in directories

Quoted 9 lines · NeilBrown
Quoted 7 lines
Quoted 3 lines

I think a strong argument for Linux needing something is that NFS needs working directory offsets to be able to support READDIR, because there is no "OPEN" request for directories.

I don't think anyone disputes the need for stable directory offsets, and we should (*knock on wood*) have implementation of native file systems that fail this.

"should not" ??

Yes.

Quoted 8 lines
Quoted 4 lines

What the test tries to force is a specific behavior for rename onto existing file names, replacing the original file name entry with a new one of the same name. The test expects that to reuse the previous d_off, which is not required by any real or de facto standard.

That's debatable. The documents that I have found don't mention rename. Does that mean anything goes, or do that mean it doesn't get an exemption from the general rule that all names must be listed?

The Posix definition of rename is very explicit that the old directory entry shall be removed:

Otherwise, if the directory entry named by new exists, it shall be removed and old renamed to new. In this case, a directory entry named new shall remain visible to other threads throughout the renaming operation and refer either to the file referred to by new or old before the operation began.

So the previous directory entry for "new" shall be removed. If a file system reuses the same d_off for the renamed file, it just created a new entry that happens to instantly reuse the d_off in the same atomic operation.

Quoted 11 lines

I think a fix to make it work over NFS would be to report the entries in reverse order - most recently added to least recently added. This would effectively encode the timestamp (last_index) in the seek cookie instead of in btrfs_file_private.
Changing how the seek-cookie is interpreted could cause a hiccup for an NFS client which was in the middle of a READDIR when a server we reboot to a new verion of btrfs, but that is likely not significant.

Note that
https://pubs.opengroup.org/onlinepubs/9799919799/functions/posix_getdents.html

Quoted 6 lines

uses the word "incremented" in

the directory entry immediately following the last entry whose information was returned

but I don't think anyone seems interested in that detail.

I would suggest to ignore posix_getdents or the whole Issue 8 base spec, as unlike previous versions I did not try to document and norm existing behavior, but instead comes up with it's own things. If we need a justification we should look at historic Linux and Unix behaviors and older Posix specs that haven't drifted away from the purpose as much as the current one.

Reviewed-by on v1 1/1
#10 lore raw

Re: [PATCH] fstests: generic: Add test of seek in directories

In reply to NeilBrown
Quoted 1 line · NeilBrown

Add a test for consistency of readdir (getdents64) results.

Thanks for writing this!

Note that it asserts more than exactly-once: the relative order of every stable name must hold for the life of the directory, across independent opens. That is the right property needed for NFS. Either the internal documentation or the commit message should say that, and explain why: between two READDIRs there is no open state, so the server can be handed any cookie against any later state of the directory. POSIX does not require this, certainly, but NFS does.

The seek check picks n from all names, including unstable ones unlinked many loops earlier, using the d_off from the first scan. That is the "rm -rf over NFS" case and the most valuable check here, but it reads like an oversight. Can you add a comment saying it is deliberate?

The seven op classes run mixed in one invocation, so a failure says "loop 37 with seed 1234567" and not which *semantic* broke. btrfs fails only on renames onto a stable name, and nobody can tell that from the output without rerunning with -T. It might be nicer to have the wrapper invoke the binary once per class, or at least once each for churn, rename onto an existing name, and RENAME_EXCHANGE. Then put exchange in its own test gated by _require_renameat2 exchange, which replaces the FSTYP == nfs check and _notruns everywhere the flag is unsupported.

Quoted 1 line

+_begin_fstest auto dir quick

Add rename, perhaps?

Quoted 1 line

+$here/src/t_dir_seek -p $TEST_DIR -S 1234567 $extra

$TEST_DIR/testdir is not scoped to $seq and there is no _cleanup(). A run interrupted before clean_files() makes the next one fail in mkdir with exit status 1. Use $TEST_DIR/$seq-dir and rm -rf it before the run and in _cleanup().

src/t_dir_seek.c line 307

Quoted 3 lines
src/t_dir_seek.c new file +3 -0
+		case op_create:
+			n = get_file(&unused);
+			if (n > 0) {

n >= 0. Name 0 is a valid index. Drawn here it is removed from unused and never put back anywhere.

src/t_dir_seek.c line 239

Quoted 1 line
src/t_dir_seek.c new file +1 -0
+	lseek(fd, off, SEEK_SET);

For both get_order() and check_order(), a rejected cookie should fail with the errno, not proceed from 0 and report bad order.

Nits: copyright 2025 vs 2026; typos in both header comments; %lu for uint64_t; usage says "-p 10" for -o; the -n and -o range errors name the wrong flags; d_name[] rather than d_name[0].

With the wrapper and op_create fixes:

I will also note that, despite the new failures, users of NFS- exported tmpfs and btrfs filesystems have not observed or reported problems. So the severity of these failures is not high, IMO, but it would still be good to correct them. It's great to see more of the specific NFS requirements for directories materialized in a set of unit tests.

Signature · 1 line
Chuck Lever (Come to NFS bake-a-thon! https://nfsv4bat.org)
#11 lore raw

Re: [PATCH] fstests: generic: Add test of seek in directories

In reply to NeilBrown

Please include the mainling list for the file system you think is buggy or at least odd. Done now.

Quoted 5 lines · NeilBrown

The test then reads the directory to find the order of the stable name, which should never change. It also find the "d_off" of all names. These d_off should be usable as a "seek" offset to find at least all the stable names that came after that point in the original listing.

Quoted 8 lines

After creating the names and finding the order, a loop which repeatly:
- makes random changes to unstable names
- checks the complete order of stable names in a new readdir
- checks what appears after a seek() to a randomly chosen offet

In Linux 7.2 this test always passes for xfs and ext4 but fails for btrfs unless we suppress renames with a "stable" name as target. It also fails for tmpfs.

There is no requirement where the entry for a renamed entry is placed. Posix requires telldir/seekdir to provide a stable cookie, but how that cookie behaves when the directory is modified is completely undefined. The same is true for NFS which requires basically the same, but more stateless than local telldir/seekdir.

author
#12 lore raw

Re: [PATCH] fstests: generic: Add test of seek in directories

Quoted 2 lines · Christoph Hellwig

Please include the mainling list for the file system you think is buggy or at least odd. Done now.

Thanks.

Quoted 16 lines · NeilBrown
Quoted 5 lines

The test then reads the directory to find the order of the stable name, which should never change. It also find the "d_off" of all names. These d_off should be usable as a "seek" offset to find at least all the stable names that came after that point in the original listing.

Quoted 8 lines

After creating the names and finding the order, a loop which repeatly:
- makes random changes to unstable names
- checks the complete order of stable names in a new readdir
- checks what appears after a seek() to a randomly chosen offet

In Linux 7.2 this test always passes for xfs and ext4 but fails for btrfs unless we suppress renames with a "stable" name as target. It also fails for tmpfs.

Quoted 7 lines

There is no requirement where the entry for a renamed entry is placed. Posix requires telldir/seekdir to provide a stable cookie, but how that cookie behaves when the directory is modified is completely undefined. The same is true for NFS which requires basically the same, but more stateless than local telldir/seekdir.

I don't think the behaviour is "completely" undefined in the face of change.

https://pubs.opengroup.org/onlinepubs/007908799/xsh/readdir.html

says the directory stream represents "all the directory entries in a particular directory" and makes exceptions:

If a file is removed from or added to the directory after the most recent call to opendir() or rewinddir(), whether a subsequent call to readdir() returns an entry for that file is unspecified.

so "all" doesn't need to includes things that were added or removed. Does a rename over an existing file "add" or "remove"?
It depends on how you understand "file".

We are told "Directory entries represent files" so we need to understand "files" in that context.
If we considered "file" to mean "filesystem object", then the above would allow readdir to ignore multiple hard-links to a file reporting only one of them. Hopefully we all agree that would be wrong.

So I think "file" in this context must mean "name" (that is the main part of a "directory entry").

So, when we rename over an existing name, is that name added or removed? I think not. It is critical to rename(2) that the replacement is atomic.

With btrfs at present, if a name is the target of a rename while a readdir is happening, that name might not be reported. This is because btrfs iterate_shared deliberately skips any names that are "new" since the start of the readdir, and it considers a name replaced in a rename as "new". I think this is incorrect behaviour and could be harmful.

Prior to
Commit: 9b378f6ad48c ("btrfs: fix infinite directory reads")

btrfs could report the target of a rename twice in a readdir listing (but always at least once).
While I think the duplication is unnecessary it is harder to criticise.

NeilBrown

#13 lore raw

Re: [PATCH] fstests: generic: Add test of seek in directories

Quoted 21 lines · NeilBrown
Quoted 4 lines

Posix requires telldir/seekdir to provide a stable cookie, but how that cookie behaves when the directory is modified is completely undefined. The same is true for NFS which requires basically the same, but more stateless than local telldir/seekdir.

I don't think the behaviour is "completely" undefined in the face of change.

https://pubs.opengroup.org/onlinepubs/007908799/xsh/readdir.html

says the directory stream represents "all the directory entries in a particular directory" and makes exceptions:

If a file is removed from or added to the directory after the most recent call to opendir() or rewinddir(), whether a subsequent call to readdir() returns an entry for that file is unspecified.

so "all" doesn't need to includes things that were added or removed. Does a rename over an existing file "add" or "remove"?
It depends on how you understand "file".

Quoted 8 lines

We are told "Directory entries represent files" so we need to understand "files" in that context.
If we considered "file" to mean "filesystem object", then the above would allow readdir to ignore multiple hard-links to a file reporting only one of them. Hopefully we all agree that would be wrong.

So I think "file" in this context must mean "name" (that is the main part of a "directory entry").

It does not. Posix very precisely uses "directory entry" when referring to directory entries, and files refer to what is an inode in Linux. This is very clear in the rename documentation:

The rename() function shall change the name of a file. The old argument points to the pathname of the file to be renamed. The new argument points to the new pathname of the file.
If the new argument does not resolve to an existing directory entry...

https://pubs.opengroup.org/onlinepubs/9799919799/functions/rename.html

Quoted 2 lines

So, when we rename over an existing name, is that name added or removed? I think not. It is critical to rename(2) that the replacement is atomic.

The directory entry for the old file is removed, and the directory entry for the file is added in one atomic transaction.

Quoted 6 lines

With btrfs at present, if a name is the target of a rename while a readdir is happening, that name might not be reported. This is because btrfs iterate_shared deliberately skips any names that are "new" since the start of the readdir, and it considers a name replaced in a rename as "new". I think this is incorrect behaviour and could be harmful.

Quoted 6 lines

Prior to
Commit: 9b378f6ad48c ("btrfs: fix infinite directory reads")

btrfs could report the target of a rename twice in a readdir listing (but always at least once).
While I think the duplication is unnecessary it is harder to criticise.

I'm in agreement here.