linux-btrfs.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Joan Bruguera <joanbrugueram@gmail.com>
To: fdmanana@suse.com
Cc: linux-btrfs@vger.kernel.org,
	"Joan Bruguera Micó" <joanbrugueram@gmail.com>
Subject: Repression on lseek (holes) on 1-byte files since Linux 6.1-rc1
Date: Fri, 23 Dec 2022 02:05:09 +0000	[thread overview]
Message-ID: <20221223020509.457113-1-joanbrugueram@gmail.com> (raw)

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 2706 bytes --]

From: Joan Bruguera Micó <joanbrugueram@gmail.com>

Hello,

I believe I have found a regression related to seeking file data and holes on
1-byte files since Linux 6.1-rc1:

Since Linux 6.1-rc1 I observe that, if I create a (non-sparse) 1-byte file and
immediately run `lseek(SEEK_DATA, 0)` or `lseek(SEEK_HOLE, 0)` on it, it will
act as if it was a sparse file, i.e. as if it had a hole from offset 0 to 1.

If I wait a while or run `sync`, the results are what I expect, i.e. no hole.

A simple reproducer is:
    #!/usr/bin/env sh
    set -eu

    rm -f test.bin
    echo "-----BEFORE SYNC-----"
    printf "x" > test.bin
    xfs_io -c "seek -d 0" test.bin
    echo "-----AFTER SYNC-----"
    sync
    xfs_io -c "seek -d 0" test.bin

Expected results (Linux <6.1-rc1):
    -----BEFORE SYNC-----
    Whence  Result
    DATA    0
    -----AFTER SYNC-----
    Whence  Result
    DATA    0

Actual results (Linux >=6.1-rc1):
    -----BEFORE SYNC-----
    Whence  Result
    DATA    EOF
    -----AFTER SYNC-----
    Whence  Result
    DATA    0

It doesn't reproduce 100% of the time due to the race between lseek and
background sync, but it's consistent and also reproduces on a clean VM or UML.
It also doesn't reproduce on larger (e.g. 2 bytes) files.

I've bisected the change in behaviour to commit
b6e833567ea12bc47d91e4b6497d49ba60d4f95f
"btrfs: make hole and data seeking a lot more efficient".

I tried to see if I could figure out the cause by looking at the btrfs kernel
code for lseek in fs/btrfs/file.c.

I believe everything is fine until this snippet (line 3947 in v6.1),
which is supposed to find whether there's a hole at the last extent:

    /* We have an implicit hole from the last extent found up to i_size. */
    if (!found && start < i_size) {
        found = find_desired_extent_in_hole(inode, whence, start,
                            i_size - 1, &start);
        if (!found)
            start = i_size;
    }

Here it calls `find_desired_extent_in_hole`, which immediately calls
`btrfs_find_delalloc_in_range` with a range of start=0 and end=0 (inclusive).
Supposedly it should find that there's a delalloc extent, but one can easily
see by looking at the function that it always returns false if start==end.
This explains why it only happens with 1-byte files, as on e.g. a 2 byte file
start=0 and end=1 (inclusive) and the function loops and finds the delalloc.

I'm not sure what the right fix is since I'm not very familiar with the btrfs
code base, but both replacing `i_size - 1` with `lockend` in the snippet above,
OR tightening the conditionals in `btrfs_find_delalloc_in_range` (and
`count_range_bits`) for the `start==end` case seem to work.

Regards,
- Joan Bruguera

             reply	other threads:[~2022-12-23  2:05 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-12-23  2:05 Joan Bruguera [this message]
2022-12-23 12:19 ` Repression on lseek (holes) on 1-byte files since Linux 6.1-rc1 #forregzbot Thorsten Leemhuis
2023-01-02 15:42   ` David Sterba
2022-12-23 18:31 ` Repression on lseek (holes) on 1-byte files since Linux 6.1-rc1 Filipe Manana

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20221223020509.457113-1-joanbrugueram@gmail.com \
    --to=joanbrugueram@gmail.com \
    --cc=fdmanana@suse.com \
    --cc=linux-btrfs@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).