]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
btrfs: disable defrag on pre-content watched files
authorJosef Bacik <josef@toxicpanda.com>
Fri, 15 Nov 2024 15:30:31 +0000 (10:30 -0500)
committerJan Kara <jack@suse.cz>
Wed, 11 Dec 2024 16:28:41 +0000 (17:28 +0100)
We queue up inodes to be defrag'ed asynchronously, which means we do not
have their original file for readahead.  This means that the code to
skip readahead on pre-content watched files will not run, and we could
potentially read in empty pages.

Handle this corner case by disabling defrag on files that are currently
being watched for pre-content events.

Signed-off-by: Josef Bacik <josef@toxicpanda.com>
Signed-off-by: Jan Kara <jack@suse.cz>
Link: https://patch.msgid.link/4cc5bcea13db7904174353d08e85157356282a59.1731684329.git.josef@toxicpanda.com
fs/btrfs/ioctl.c

index c9302d1931870dca5916ac66b218eeb28a4357ff..51b439537049c833fed59d77b3b26641cfde2f9b 100644 (file)
@@ -2635,6 +2635,15 @@ static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
                        goto out;
                }
 
+               /*
+                * Don't allow defrag on pre-content watched files, as it could
+                * populate the page cache with 0's via readahead.
+                */
+               if (unlikely(FMODE_FSNOTIFY_HSM(file->f_mode))) {
+                       ret = -EINVAL;
+                       goto out;
+               }
+
                if (argp) {
                        if (copy_from_user(&range, argp, sizeof(range))) {
                                ret = -EFAULT;