diff mbox series

[v5,17/18] btrfs: disable defrag on pre-content watched files

Message ID 367fe83ae53839b1e88dc00b5b420035496d28ff.1725481503.git.josef@toxicpanda.com (mailing list archive)
State New, archived
Headers show
Series fanotify: add pre-content hooks | expand

Commit Message

Josef Bacik Sept. 4, 2024, 8:28 p.m. UTC
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>
---
 fs/btrfs/ioctl.c | 9 +++++++++
 1 file changed, 9 insertions(+)

Comments

Amir Goldstein Sept. 5, 2024, 8:23 a.m. UTC | #1
On Wed, Sep 4, 2024 at 10:29 PM Josef Bacik <josef@toxicpanda.com> wrote:
>
> 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.

IIUC, you are disabling the *start* of async defrag.
What happens if defrag has already started and then a pre-content
watch is set up?

Do we care about this corner of a corner case?

Thanks,
Amir.

>
> Signed-off-by: Josef Bacik <josef@toxicpanda.com>
> ---
>  fs/btrfs/ioctl.c | 9 +++++++++
>  1 file changed, 9 insertions(+)
>
> diff --git a/fs/btrfs/ioctl.c b/fs/btrfs/ioctl.c
> index e0a664b8a46a..529f7416814f 100644
> --- a/fs/btrfs/ioctl.c
> +++ b/fs/btrfs/ioctl.c
> @@ -2640,6 +2640,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 (fsnotify_file_has_pre_content_watches(file)) {
> +                       ret = -EINVAL;
> +                       goto out;
> +               }
> +
>                 if (argp) {
>                         if (copy_from_user(&range, argp, sizeof(range))) {
>                                 ret = -EFAULT;
> --
> 2.43.0
>
diff mbox series

Patch

diff --git a/fs/btrfs/ioctl.c b/fs/btrfs/ioctl.c
index e0a664b8a46a..529f7416814f 100644
--- a/fs/btrfs/ioctl.c
+++ b/fs/btrfs/ioctl.c
@@ -2640,6 +2640,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 (fsnotify_file_has_pre_content_watches(file)) {
+			ret = -EINVAL;
+			goto out;
+		}
+
 		if (argp) {
 			if (copy_from_user(&range, argp, sizeof(range))) {
 				ret = -EFAULT;