Revision 3a8b36f378060d20062a0918e99fae39ff077bf0 authored by Filipe Manana on 01 March 2015, 20:36:00 UTC, committed by Chris Mason on 06 March 2015, 01:28:32 UTC
When using the fast file fsync code path we can miss the fact that new writes happened since the last file fsync and therefore return without waiting for the IO to finish and write the new extents to the fsync log. Here's an example scenario where the fsync will miss the fact that new file data exists that wasn't yet durably persisted: 1. fs_info->last_trans_committed == N - 1 and current transaction is transaction N (fs_info->generation == N); 2. do a buffered write; 3. fsync our inode, this clears our inode's full sync flag, starts an ordered extent and waits for it to complete - when it completes at btrfs_finish_ordered_io(), the inode's last_trans is set to the value N (via btrfs_update_inode_fallback -> btrfs_update_inode -> btrfs_set_inode_last_trans); 4. transaction N is committed, so fs_info->last_trans_committed is now set to the value N and fs_info->generation remains with the value N; 5. do another buffered write, when this happens btrfs_file_write_iter sets our inode's last_trans to the value N + 1 (that is fs_info->generation + 1 == N + 1); 6. transaction N + 1 is started and fs_info->generation now has the value N + 1; 7. transaction N + 1 is committed, so fs_info->last_trans_committed is set to the value N + 1; 8. fsync our inode - because it doesn't have the full sync flag set, we only start the ordered extent, we don't wait for it to complete (only in a later phase) therefore its last_trans field has the value N + 1 set previously by btrfs_file_write_iter(), and so we have: inode->last_trans <= fs_info->last_trans_committed (N + 1) (N + 1) Which made us not log the last buffered write and exit the fsync handler immediately, returning success (0) to user space and resulting in data loss after a crash. This can actually be triggered deterministically and the following excerpt from a testcase I made for xfstests triggers the issue. It moves a dummy file across directories and then fsyncs the old parent directory - this is just to trigger a transaction commit, so moving files around isn't directly related to the issue but it was chosen because running 'sync' for example does more than just committing the current transaction, as it flushes/waits for all file data to be persisted. The issue can also happen at random periods, since the transaction kthread periodicaly commits the current transaction (about every 30 seconds by default). The body of the test is: _scratch_mkfs >> $seqres.full 2>&1 _init_flakey _mount_flakey # Create our main test file 'foo', the one we check for data loss. # By doing an fsync against our file, it makes btrfs clear the 'needs_full_sync' # bit from its flags (btrfs inode specific flags). $XFS_IO_PROG -f -c "pwrite -S 0xaa 0 8K" \ -c "fsync" $SCRATCH_MNT/foo | _filter_xfs_io # Now create one other file and 2 directories. We will move this second file # from one directory to the other later because it forces btrfs to commit its # currently open transaction if we fsync the old parent directory. This is # necessary to trigger the data loss bug that affected btrfs. mkdir $SCRATCH_MNT/testdir_1 touch $SCRATCH_MNT/testdir_1/bar mkdir $SCRATCH_MNT/testdir_2 # Make sure everything is durably persisted. sync # Write more 8Kb of data to our file. $XFS_IO_PROG -c "pwrite -S 0xbb 8K 8K" $SCRATCH_MNT/foo | _filter_xfs_io # Move our 'bar' file into a new directory. mv $SCRATCH_MNT/testdir_1/bar $SCRATCH_MNT/testdir_2/bar # Fsync our first directory. Because it had a file moved into some other # directory, this made btrfs commit the currently open transaction. This is # a condition necessary to trigger the data loss bug. $XFS_IO_PROG -c "fsync" $SCRATCH_MNT/testdir_1 # Now fsync our main test file. If the fsync succeeds, we expect the 8Kb of # data we wrote previously to be persisted and available if a crash happens. # This did not happen with btrfs, because of the transaction commit that # happened when we fsynced the parent directory. $XFS_IO_PROG -c "fsync" $SCRATCH_MNT/foo # Simulate a crash/power loss. _load_flakey_table $FLAKEY_DROP_WRITES _unmount_flakey _load_flakey_table $FLAKEY_ALLOW_WRITES _mount_flakey # Now check that all data we wrote before are available. echo "File content after log replay:" od -t x1 $SCRATCH_MNT/foo status=0 exit The expected golden output for the test, which is what we get with this fix applied (or when running against ext3/4 and xfs), is: wrote 8192/8192 bytes at offset 0 XXX Bytes, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec) wrote 8192/8192 bytes at offset 8192 XXX Bytes, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec) File content after log replay: 0000000 aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa * 0020000 bb bb bb bb bb bb bb bb bb bb bb bb bb bb bb bb * 0040000 Without this fix applied, the output shows the test file does not have the second 8Kb extent that we successfully fsynced: wrote 8192/8192 bytes at offset 0 XXX Bytes, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec) wrote 8192/8192 bytes at offset 8192 XXX Bytes, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec) File content after log replay: 0000000 aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa * 0020000 So fix this by skipping the fsync only if we're doing a full sync and if the inode's last_trans is <= fs_info->last_trans_committed, or if the inode is already in the log. Also remove setting the inode's last_trans in btrfs_file_write_iter since it's useless/unreliable. Also because btrfs_file_write_iter no longer sets inode->last_trans to fs_info->generation + 1, don't set last_trans to 0 if we bail out and don't bail out if last_trans is 0, otherwise something as simple as the following example wouldn't log the second write on the last fsync: 1. write to file 2. fsync file 3. fsync file |--> btrfs_inode_in_log() returns true and it set last_trans to 0 4. write to file |--> btrfs_file_write_iter() no longers sets last_trans, so it remained with a value of 0 5. fsync |--> inode->last_trans == 0, so it bails out without logging the second write A test case for xfstests will be sent soon. CC: <stable@vger.kernel.org> Signed-off-by: Filipe Manana <fdmanana@suse.com> Signed-off-by: Chris Mason <clm@fb.com>
1 parent f5c0a12
File | Mode | Size |
---|---|---|
ABI | ||
DocBook | ||
EDID | ||
PCI | ||
RCU | ||
accounting | ||
acpi | ||
aoe | ||
arm | ||
arm64 | ||
auxdisplay | ||
backlight | ||
blackfin | ||
block | ||
blockdev | ||
bus-devices | ||
cdrom | ||
cgroups | ||
connector | ||
console | ||
cpu-freq | ||
cpuidle | ||
cris | ||
crypto | ||
development-process | ||
device-mapper | ||
devicetree | ||
dmaengine | ||
driver-model | ||
dvb | ||
early-userspace | ||
extcon | ||
fault-injection | ||
fb | ||
filesystems | ||
firmware_class | ||
fmc | ||
frv | ||
gpio | ||
hid | ||
hwmon | ||
i2c | ||
i2o | ||
ia64 | ||
ide | ||
infiniband | ||
input | ||
ioctl | ||
isdn | ||
ja_JP | ||
kbuild | ||
kdump | ||
ko_KR | ||
laptops | ||
leds | ||
locking | ||
m68k | ||
memory-devices | ||
metag | ||
mic | ||
mips | ||
misc-devices | ||
mmc | ||
mn10300 | ||
mtd | ||
namespaces | ||
netlabel | ||
networking | ||
nfc | ||
nios2 | ||
parisc | ||
pcmcia | ||
phy | ||
platform | ||
power | ||
powerpc | ||
pps | ||
prctl | ||
pti | ||
ptp | ||
rapidio | ||
s390 | ||
scheduler | ||
scsi | ||
security | ||
serial | ||
sh | ||
sound | ||
spi | ||
sysctl | ||
target | ||
thermal | ||
timers | ||
tpm | ||
trace | ||
usb | ||
vDSO | ||
video4linux | ||
virtual | ||
vm | ||
w1 | ||
watchdog | ||
wimax | ||
x86 | ||
xtensa | ||
zh_CN | ||
00-INDEX | -rw-r--r-- | 17.1 KB |
BUG-HUNTING | -rw-r--r-- | 8.1 KB |
Changes | -rw-r--r-- | 11.1 KB |
CodingStyle | -rw-r--r-- | 33.7 KB |
DMA-API-HOWTO.txt | -rw-r--r-- | 34.7 KB |
DMA-API.txt | -rw-r--r-- | 27.7 KB |
DMA-ISA-LPC.txt | -rw-r--r-- | 5.2 KB |
DMA-attributes.txt | -rw-r--r-- | 4.5 KB |
HOWTO | -rw-r--r-- | 27.1 KB |
IPMI.txt | -rw-r--r-- | 29.0 KB |
IRQ-affinity.txt | -rw-r--r-- | 2.5 KB |
IRQ-domain.txt | -rw-r--r-- | 10.0 KB |
IRQ.txt | -rw-r--r-- | 962 bytes |
Intel-IOMMU.txt | -rw-r--r-- | 3.8 KB |
Makefile | -rw-r--r-- | 185 bytes |
ManagementStyle | -rw-r--r-- | 12.9 KB |
SAK.txt | -rw-r--r-- | 2.8 KB |
SM501.txt | -rw-r--r-- | 2.8 KB |
SecurityBugs | -rw-r--r-- | 1.8 KB |
SubmitChecklist | -rw-r--r-- | 4.4 KB |
SubmittingDrivers | -rw-r--r-- | 6.2 KB |
SubmittingPatches | -rw-r--r-- | 32.3 KB |
VGA-softcursor.txt | -rw-r--r-- | 2.0 KB |
applying-patches.txt | -rw-r--r-- | 19.5 KB |
assoc_array.txt | -rw-r--r-- | 20.0 KB |
atomic_ops.txt | -rw-r--r-- | 21.6 KB |
bad_memory.txt | -rw-r--r-- | 1.1 KB |
basic_profiling.txt | -rw-r--r-- | 1.7 KB |
bcache.txt | -rw-r--r-- | 16.4 KB |
binfmt_misc.txt | -rw-r--r-- | 6.5 KB |
braille-console.txt | -rw-r--r-- | 1.4 KB |
bt8xxgpio.txt | -rw-r--r-- | 4.3 KB |
btmrvl.txt | -rw-r--r-- | 2.9 KB |
bus-virt-phys-mapping.txt | -rw-r--r-- | 7.9 KB |
cachetlb.txt | -rw-r--r-- | 17.1 KB |
circular-buffers.txt | -rw-r--r-- | 8.4 KB |
clk.txt | -rw-r--r-- | 11.4 KB |
coccinelle.txt | -rw-r--r-- | 9.0 KB |
cpu-hotplug.txt | -rw-r--r-- | 16.8 KB |
cpu-load.txt | -rw-r--r-- | 3.0 KB |
cputopology.txt | -rw-r--r-- | 3.8 KB |
crc32.txt | -rw-r--r-- | 8.5 KB |
dcdbas.txt | -rw-r--r-- | 3.6 KB |
debugging-modules.txt | -rw-r--r-- | 954 bytes |
debugging-via-ohci1394.txt | -rw-r--r-- | 7.4 KB |
dell_rbu.txt | -rw-r--r-- | 4.9 KB |
devices.txt | -rw-r--r-- | 116.2 KB |
digsig.txt | -rw-r--r-- | 2.8 KB |
dma-buf-sharing.txt | -rw-r--r-- | 20.8 KB |
dontdiff | -rw-r--r-- | 2.5 KB |
dynamic-debug-howto.txt | -rw-r--r-- | 12.6 KB |
edac.txt | -rw-r--r-- | 24.3 KB |
efi-stub.txt | -rw-r--r-- | 3.2 KB |
eisa.txt | -rw-r--r-- | 7.1 KB |
email-clients.txt | -rw-r--r-- | 9.1 KB |
flexible-arrays.txt | -rw-r--r-- | 5.5 KB |
futex-requeue-pi.txt | -rw-r--r-- | 5.0 KB |
gcov.txt | -rw-r--r-- | 7.6 KB |
highuid.txt | -rw-r--r-- | 2.4 KB |
hsi.txt | -rw-r--r-- | 2.9 KB |
hw_random.txt | -rw-r--r-- | 3.5 KB |
hwspinlock.txt | -rw-r--r-- | 12.1 KB |
init.txt | -rw-r--r-- | 2.5 KB |
initrd.txt | -rw-r--r-- | 14.1 KB |
intel_txt.txt | -rw-r--r-- | 10.2 KB |
io-mapping.txt | -rw-r--r-- | 3.2 KB |
io_ordering.txt | -rw-r--r-- | 1.9 KB |
iostats.txt | -rw-r--r-- | 8.0 KB |
irqflags-tracing.txt | -rw-r--r-- | 2.3 KB |
isapnp.txt | -rw-r--r-- | 433 bytes |
java.txt | -rw-r--r-- | 10.9 KB |
kernel-doc-nano-HOWTO.txt | -rw-r--r-- | 11.7 KB |
kernel-docs.txt | -rw-r--r-- | 33.1 KB |
kernel-parameters.txt | -rw-r--r-- | 138.1 KB |
kernel-per-CPU-kthreads.txt | -rw-r--r-- | 12.8 KB |
kmemcheck.txt | -rw-r--r-- | 29.9 KB |
kmemleak.txt | -rw-r--r-- | 8.5 KB |
kobject.txt | -rw-r--r-- | 18.0 KB |
kprobes.txt | -rw-r--r-- | 30.2 KB |
kref.txt | -rw-r--r-- | 8.4 KB |
kselftest.txt | -rw-r--r-- | 2.0 KB |
ldm.txt | -rw-r--r-- | 3.8 KB |
local_ops.txt | -rw-r--r-- | 6.5 KB |
lockup-watchdogs.txt | -rw-r--r-- | 3.1 KB |
logo.gif | -rw-r--r-- | 16.0 KB |
logo.txt | -rw-r--r-- | 563 bytes |
lzo.txt | -rw-r--r-- | 7.8 KB |
magic-number.txt | -rw-r--r-- | 8.8 KB |
mailbox.txt | -rw-r--r-- | 4.2 KB |
md.txt | -rw-r--r-- | 25.3 KB |
media-framework.txt | -rw-r--r-- | 14.7 KB |
memory-barriers.txt | -rw-r--r-- | 107.4 KB |
memory-hotplug.txt | -rw-r--r-- | 16.2 KB |
module-signing.txt | -rw-r--r-- | 8.7 KB |
mono.txt | -rw-r--r-- | 2.5 KB |
nommu-mmap.txt | -rw-r--r-- | 12.7 KB |
numastat.txt | -rw-r--r-- | 836 bytes |
oops-tracing.txt | -rw-r--r-- | 12.7 KB |
padata.txt | -rw-r--r-- | 7.3 KB |
parport-lowlevel.txt | -rw-r--r-- | 32.2 KB |
parport.txt | -rw-r--r-- | 8.8 KB |
percpu-rw-semaphore.txt | -rw-r--r-- | 1.1 KB |
phy.txt | -rw-r--r-- | 6.5 KB |
pi-futex.txt | -rw-r--r-- | 5.7 KB |
pinctrl.txt | -rw-r--r-- | 49.9 KB |
pnp.txt | -rw-r--r-- | 6.8 KB |
preempt-locking.txt | -rw-r--r-- | 5.2 KB |
printk-formats.txt | -rw-r--r-- | 8.3 KB |
pwm.txt | -rw-r--r-- | 4.7 KB |
ramoops.txt | -rw-r--r-- | 5.2 KB |
rbtree.txt | -rw-r--r-- | 13.3 KB |
remoteproc.txt | -rw-r--r-- | 12.3 KB |
rfkill.txt | -rw-r--r-- | 4.7 KB |
robust-futex-ABI.txt | -rw-r--r-- | 8.7 KB |
robust-futexes.txt | -rw-r--r-- | 9.4 KB |
rpmsg.txt | -rw-r--r-- | 13.5 KB |
rtc.txt | -rw-r--r-- | 15.6 KB |
serial-console.txt | -rw-r--r-- | 4.0 KB |
sgi-ioc4.txt | -rw-r--r-- | 2.0 KB |
smsc_ece1099.txt | -rw-r--r-- | 2.4 KB |
sparse.txt | -rw-r--r-- | 3.8 KB |
stable_api_nonsense.txt | -rw-r--r-- | 9.2 KB |
stable_kernel_rules.txt | -rw-r--r-- | 4.6 KB |
static-keys.txt | -rw-r--r-- | 11.9 KB |
svga.txt | -rw-r--r-- | 14.1 KB |
sysfs-rules.txt | -rw-r--r-- | 9.0 KB |
sysrq.txt | -rw-r--r-- | 11.7 KB |
this_cpu_ops.txt | -rw-r--r-- | 11.1 KB |
unaligned-memory-access.txt | -rw-r--r-- | 10.4 KB |
unicode.txt | -rw-r--r-- | 6.5 KB |
unshare.txt | -rw-r--r-- | 13.1 KB |
vfio.txt | -rw-r--r-- | 18.6 KB |
vgaarbiter.txt | -rw-r--r-- | 8.1 KB |
video-output.txt | -rw-r--r-- | 1.1 KB |
vme_api.txt | -rw-r--r-- | 13.2 KB |
volatile-considered-harmful.txt | -rw-r--r-- | 5.6 KB |
workqueue.txt | -rw-r--r-- | 14.7 KB |
xillybus.txt | -rw-r--r-- | 17.7 KB |
xz.txt | -rw-r--r-- | 5.7 KB |
zorro.txt | -rw-r--r-- | 2.9 KB |
![swh spinner](/static/img/swh-spinner.gif)
Computing file changes ...