Bug 216738

Summary: Adding O_APPEND to O_RDWR with fcntl(fd, F_SETFL) does not work on overlayfs
Product: File System Reporter: Pierre Labastie (pierre.labastie)
Component: VFSAssignee: fs_vfs
Status: NEW ---    
Severity: normal CC: pmenzel+bugzilla.kernel.org, regressions, viro
Priority: P1    
Hardware: All   
OS: Linux   
Kernel Version: broken at commit 164f4064ca8 in torvalds tree (bisect) Subsystem:
Regression: Yes Bisected commit-id:
Attachments: C program for reproducing the bug

Description Pierre Labastie 2022-11-24 14:53:33 UTC
Created attachment 303287 [details]
C program for reproducing the bug

Not sure this is the right place to report this, but at least the offending commit is in this component... 

Steps to reproduce:
$ gcc repro.c
$ rm -f toto
$ ./a.out
$ cat toto; echo

On an ext4 fs, the output is (on all versions):
abcdefghijklmnopqr

Now, make an overlayfs:
$ mkdir -p up lo wo mnt
$ sudo mount -t overlay overlay -oupperdir=up,lowerdir=lo,workdir=wo mnt
$ cd mnt
$ rm f toto
$ ../a.out
$ cat toto; echo

before the said commit, the output is:
abcdefghijklmnopqr

after the said commit, the output is:
ghijklmnopqr

That is the file is truncated when opened with O_RDWR, with O_APPEND added later, but not when opened with both.
Comment 1 Paul Menzel 2022-11-24 15:18:47 UTC
I think you should contact the mailing list, if you do not receive any reply here in the next few days.

For others, not seeing the commit in the Linux kernel version field right away, it’s commit 164f4064ca8 (keep iocb_flags() result cached in struct file).
Comment 2 The Linux kernel's regression tracker (Thorsten Leemhuis) 2022-11-24 15:56:01 UTC
(In reply to Paul Menzel from comment #1)
> I think you should contact the mailing list, if you do not receive any reply
> here in the next few days.

Normally I'd agree, but when it comes to data corruption I'd say waiting is a bad idea. That's why I created a mail for it now:
https://lore.kernel.org/regressions/2505800d-8625-dab0-576a-3a0221954ba3@leemhuis.info/T/#u
 
> For others, not seeing the commit in the Linux kernel version field right
> away, it’s commit 164f4064ca8 (keep iocb_flags() result cached in struct
> file).

thx, I likely would have missed that. Also thx for CCing me, I would have only seen it tomorrow. 

Pierre, have you tried to revert the commit on top of master? Having the bisection log available here might be good as well if you still have it (just in case, might not be needed in the end).
Comment 3 Pierre Labastie 2022-11-24 20:43:03 UTC
I can confirm the patch proposed by Al Viro fixes the reported problem. Should I write a mail for that? And if so, to which mailing list? Sorry, I'm new to bug reporting to the kernel...
Comment 4 Paul Menzel 2022-11-24 21:46:16 UTC
Yes, reply to the email with the patch and add a `Tested-by: Pierre …`. Maybe also add

Link: https://bugzilla.kernel.org/show_bug.cgi?id=216738