Re: Weirdness with bind mounts and moving files from tmpfs

From: Al Viro
Date: Thu Apr 13 2023 - 22:55:27 EST


On Thu, Apr 13, 2023 at 05:23:13PM -0700, Richard Cochran wrote:

> So far, so good. Now do the same, but with 'mv' instead of 'cp'...
>
> # echo three > /run/file
> # mv /run/file /home/file
> # cat /opt/file
> two
>
> At this point, the contents of /opt/file are stuck forever with "two"...
>
> # echo three > /run/file
> # cp /run/file /home/file
> # cat /opt/file
> two
> # echo three > /home/file
> # cat /opt/file
> two
>
> What is going on here? Is this a bug or a feature?

strace and compare the traces... cp(1) opens the existing
target, truncates and writes to it; mv(1) tries rename(),
falling back to unlink()+creat()+write() in case rename()
failed with -EXDEV.

The same behaviour can be seen without any mount(2) use:

; echo x > a
; ln a b
; echo y > c
; cat a
x
; cat b
x
; cat c
y
; cp c a
; cat b
y
; echo x > a
; cat a
x
; cat b
x
; mv c a
; cat a
y
; cat b
x
;

Exact same underlying cause - writing down what happens
step-by-step might be enlightening...