stable.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
To: linux-kernel@vger.kernel.org
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	stable@vger.kernel.org, Seulbae Kim <seulbae@gatech.edu>,
	Filipe Manana <fdmanana@suse.com>,
	David Sterba <dsterba@suse.com>
Subject: [PATCH 4.14 056/107] Btrfs: fix incorrect file size after shrinking truncate and fsync
Date: Mon,  1 Apr 2019 19:02:11 +0200	[thread overview]
Message-ID: <20190401170050.862866177@linuxfoundation.org> (raw)
In-Reply-To: <20190401170045.246405031@linuxfoundation.org>

4.14-stable review patch.  If anyone has any objections, please let me know.

------------------

From: Filipe Manana <fdmanana@suse.com>

commit bf504110bc8aa05df48b0e5f0aa84bfb81e0574b upstream.

If we do a shrinking truncate against an inode which is already present
in the respective log tree and then rename it, as part of logging the new
name we end up logging an inode item that reflects the old size of the
file (the one which we previously logged) and not the new smaller size.
The decision to preserve the size previously logged was added by commit
1a4bcf470c886b ("Btrfs: fix fsync data loss after adding hard link to
inode") in order to avoid data loss after replaying the log. However that
decision is only needed for the case the logged inode size is smaller then
the current size of the inode, as explained in that commit's change log.
If the current size of the inode is smaller then the previously logged
size, we know a shrinking truncate happened and therefore need to use
that smaller size.

Example to trigger the problem:

  $ mkfs.btrfs -f /dev/sdb
  $ mount /dev/sdb /mnt

  $ xfs_io -f -c "pwrite -S 0xab 0 8000" /mnt/foo
  $ xfs_io -c "fsync" /mnt/foo
  $ xfs_io -c "truncate 3000" /mnt/foo

  $ mv /mnt/foo /mnt/bar
  $ xfs_io -c "fsync" /mnt/bar

  <power failure>

  $ mount /dev/sdb /mnt
  $ od -t x1 -A d /mnt/bar
  0000000 ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab
  *
  0008000

Once we rename the file, we log its name (and inode item), and because
the inode was already logged before in the current transaction, we log it
with a size of 8000 bytes because that is the size we previously logged
(with the first fsync). As part of the rename, besides logging the inode,
we do also sync the log, which is done since commit d4682ba03ef618
("Btrfs: sync log after logging new name"), so the next fsync against our
inode is effectively a no-op, since no new changes happened since the
rename operation. Even if did not sync the log during the rename
operation, the same problem (fize size of 8000 bytes instead of 3000
bytes) would be visible after replaying the log if the log ended up
getting synced to disk through some other means, such as for example by
fsyncing some other modified file. In the example above the fsync after
the rename operation is there just because not every filesystem may
guarantee logging/journalling the inode (and syncing the log/journal)
during the rename operation, for example it is needed for f2fs, but not
for ext4 and xfs.

Fix this scenario by, when logging a new name (which is triggered by
rename and link operations), using the current size of the inode instead
of the previously logged inode size.

A test case for fstests follows soon.

Bugzilla: https://bugzilla.kernel.org/show_bug.cgi?id=202695
CC: stable@vger.kernel.org # 4.4+
Reported-by: Seulbae Kim <seulbae@gatech.edu>
Signed-off-by: Filipe Manana <fdmanana@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

---
 fs/btrfs/tree-log.c |   13 +++++++++++++
 1 file changed, 13 insertions(+)

--- a/fs/btrfs/tree-log.c
+++ b/fs/btrfs/tree-log.c
@@ -4501,6 +4501,19 @@ static int logged_inode_size(struct btrf
 		item = btrfs_item_ptr(path->nodes[0], path->slots[0],
 				      struct btrfs_inode_item);
 		*size_ret = btrfs_inode_size(path->nodes[0], item);
+		/*
+		 * If the in-memory inode's i_size is smaller then the inode
+		 * size stored in the btree, return the inode's i_size, so
+		 * that we get a correct inode size after replaying the log
+		 * when before a power failure we had a shrinking truncate
+		 * followed by addition of a new name (rename / new hard link).
+		 * Otherwise return the inode size from the btree, to avoid
+		 * data loss when replaying a log due to previously doing a
+		 * write that expands the inode's size and logging a new name
+		 * immediately after.
+		 */
+		if (*size_ret > inode->vfs_inode.i_size)
+			*size_ret = inode->vfs_inode.i_size;
 	}
 
 	btrfs_release_path(path);



  parent reply	other threads:[~2019-04-01 17:55 UTC|newest]

Thread overview: 113+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-04-01 17:01 [PATCH 4.14 000/107] 4.14.110-stable review Greg Kroah-Hartman
2019-04-01 17:01 ` [PATCH 4.14 001/107] Bluetooth: Check L2CAP option sizes returned from l2cap_get_conf_opt Greg Kroah-Hartman
2019-04-01 17:01 ` [PATCH 4.14 002/107] Bluetooth: Verify that l2cap_get_conf_opt provides large enough buffer Greg Kroah-Hartman
2019-04-01 17:01 ` [PATCH 4.14 003/107] video: fbdev: Set pixclock = 0 in goldfishfb Greg Kroah-Hartman
2019-04-01 17:01 ` [PATCH 4.14 004/107] stmmac: copy unicast mac address to MAC registers Greg Kroah-Hartman
2019-04-01 17:01 ` [PATCH 4.14 005/107] dccp: do not use ipv6 header for ipv4 flow Greg Kroah-Hartman
2019-04-01 17:01 ` [PATCH 4.14 006/107] genetlink: Fix a memory leak on error path Greg Kroah-Hartman
2019-04-01 17:01 ` [PATCH 4.14 007/107] mISDN: hfcpci: Test both vendor & device ID for Digium HFC4S Greg Kroah-Hartman
2019-04-01 17:01 ` [PATCH 4.14 008/107] net: datagram: fix unbounded loop in __skb_try_recv_datagram() Greg Kroah-Hartman
2019-04-01 17:01 ` [PATCH 4.14 009/107] net/packet: Set __GFP_NOWARN upon allocation in alloc_pg_vec Greg Kroah-Hartman
2019-04-01 17:01 ` [PATCH 4.14 010/107] net: rose: fix a possible stack overflow Greg Kroah-Hartman
2019-04-01 17:01 ` [PATCH 4.14 011/107] net: stmmac: fix memory corruption with large MTUs Greg Kroah-Hartman
2019-04-01 17:01 ` [PATCH 4.14 012/107] net-sysfs: call dev_hold if kobject_init_and_add success Greg Kroah-Hartman
2019-04-01 17:01 ` [PATCH 4.14 013/107] packets: Always register packet sk in the same order Greg Kroah-Hartman
2019-04-01 17:01 ` [PATCH 4.14 014/107] rhashtable: Still do rehash when we get EEXIST Greg Kroah-Hartman
2019-04-01 17:01 ` [PATCH 4.14 015/107] tcp: do not use ipv6 header for ipv4 flow Greg Kroah-Hartman
2019-04-01 17:01 ` [PATCH 4.14 016/107] thunderx: enable page recycling for non-XDP case Greg Kroah-Hartman
2019-04-01 17:01 ` [PATCH 4.14 017/107] thunderx: eliminate extra calls to put_page() for pages held for recycling Greg Kroah-Hartman
2019-04-01 17:01 ` [PATCH 4.14 018/107] vxlan: Dont call gro_cells_destroy() before device is unregistered Greg Kroah-Hartman
2019-04-01 17:01 ` [PATCH 4.14 019/107] sctp: get sctphdr by offset in sctp_compute_cksum Greg Kroah-Hartman
2019-04-01 17:01 ` [PATCH 4.14 020/107] net: aquantia: fix rx checksum offload for UDP/TCP over IPv6 Greg Kroah-Hartman
2019-04-01 17:01 ` [PATCH 4.14 021/107] mac8390: Fix mmio access size probe Greg Kroah-Hartman
2019-04-01 17:01 ` [PATCH 4.14 022/107] tun: properly test for IFF_UP Greg Kroah-Hartman
2019-04-01 17:01 ` [PATCH 4.14 023/107] tun: add a missing rcu_read_unlock() in error path Greg Kroah-Hartman
2019-04-01 17:01 ` [PATCH 4.14 024/107] powerpc/64s: Add support for ori barrier_nospec patching Greg Kroah-Hartman
2019-04-01 17:01 ` [PATCH 4.14 025/107] powerpc/64s: Patch barrier_nospec in modules Greg Kroah-Hartman
2019-04-01 17:01 ` [PATCH 4.14 026/107] powerpc/64s: Enable barrier_nospec based on firmware settings Greg Kroah-Hartman
2019-04-01 17:01 ` [PATCH 4.14 027/107] powerpc: Use barrier_nospec in copy_from_user() Greg Kroah-Hartman
2019-04-01 17:01 ` [PATCH 4.14 028/107] powerpc/64: Use barrier_nospec in syscall entry Greg Kroah-Hartman
2019-04-01 17:01 ` [PATCH 4.14 029/107] powerpc/64s: Enhance the information in cpu_show_spectre_v1() Greg Kroah-Hartman
2019-04-01 17:01 ` [PATCH 4.14 030/107] powerpc64s: Show ori31 availability in spectre_v1 sysfs file not v2 Greg Kroah-Hartman
2019-04-01 17:01 ` [PATCH 4.14 031/107] powerpc/64: Disable the speculation barrier from the command line Greg Kroah-Hartman
2019-04-01 17:01 ` [PATCH 4.14 032/107] powerpc/64: Make stf barrier PPC_BOOK3S_64 specific Greg Kroah-Hartman
2019-04-01 17:01 ` [PATCH 4.14 033/107] powerpc/64: Add CONFIG_PPC_BARRIER_NOSPEC Greg Kroah-Hartman
2019-04-01 17:01 ` [PATCH 4.14 034/107] powerpc/64: Call setup_barrier_nospec() from setup_arch() Greg Kroah-Hartman
2019-04-01 17:01 ` [PATCH 4.14 035/107] powerpc/64: Make meltdown reporting Book3S 64 specific Greg Kroah-Hartman
2019-04-01 17:01 ` [PATCH 4.14 036/107] powerpc/fsl: Add barrier_nospec implementation for NXP PowerPC Book3E Greg Kroah-Hartman
2019-04-01 17:01 ` [PATCH 4.14 037/107] powerpc/fsl: Sanitize the syscall table for NXP PowerPC 32 bit platforms Greg Kroah-Hartman
2019-04-01 17:01 ` [PATCH 4.14 038/107] powerpc/asm: Add a patch_site macro & helpers for patching instructions Greg Kroah-Hartman
2019-04-01 17:01 ` [PATCH 4.14 039/107] powerpc/64s: Add new security feature flags for count cache flush Greg Kroah-Hartman
2019-04-01 17:01 ` [PATCH 4.14 040/107] powerpc/64s: Add support for software " Greg Kroah-Hartman
2019-04-01 17:01 ` [PATCH 4.14 041/107] powerpc/pseries: Query hypervisor for count cache flush settings Greg Kroah-Hartman
2019-04-01 17:01 ` [PATCH 4.14 042/107] powerpc/powernv: Query firmware " Greg Kroah-Hartman
2019-04-01 17:01 ` [PATCH 4.14 043/107] powerpc/fsl: Add infrastructure to fixup branch predictor flush Greg Kroah-Hartman
2019-04-01 17:01 ` [PATCH 4.14 044/107] powerpc/fsl: Add macro to flush the branch predictor Greg Kroah-Hartman
2019-04-01 17:02 ` [PATCH 4.14 045/107] powerpc/fsl: Fix spectre_v2 mitigations reporting Greg Kroah-Hartman
2019-04-01 17:02 ` [PATCH 4.14 046/107] powerpc/fsl: Emulate SPRN_BUCSR register Greg Kroah-Hartman
2019-04-01 17:02 ` [PATCH 4.14 047/107] powerpc/fsl: Add nospectre_v2 command line argument Greg Kroah-Hartman
2019-04-01 17:02 ` [PATCH 4.14 048/107] powerpc/fsl: Flush the branch predictor at each kernel entry (64bit) Greg Kroah-Hartman
2019-04-01 17:02 ` [PATCH 4.14 049/107] powerpc/fsl: Flush the branch predictor at each kernel entry (32 bit) Greg Kroah-Hartman
2019-04-01 17:02 ` [PATCH 4.14 050/107] powerpc/fsl: Flush branch predictor when entering KVM Greg Kroah-Hartman
2019-04-01 17:02 ` [PATCH 4.14 051/107] powerpc/fsl: Enable runtime patching if nospectre_v2 boot arg is used Greg Kroah-Hartman
2019-04-01 17:02 ` [PATCH 4.14 052/107] powerpc/fsl: Update Spectre v2 reporting Greg Kroah-Hartman
2019-04-01 17:02 ` [PATCH 4.14 053/107] powerpc/fsl: Fixed warning: orphan section `__btb_flush_fixup Greg Kroah-Hartman
2019-04-01 17:02 ` [PATCH 4.14 054/107] powerpc/fsl: Fix the flush of branch predictor Greg Kroah-Hartman
2019-04-01 17:02 ` [PATCH 4.14 055/107] powerpc/security: Fix spectre_v2 reporting Greg Kroah-Hartman
2019-04-01 17:02 ` Greg Kroah-Hartman [this message]
2019-04-01 17:02 ` [PATCH 4.14 057/107] btrfs: remove WARN_ON in log_dir_items Greg Kroah-Hartman
2019-04-01 17:02 ` [PATCH 4.14 058/107] btrfs: raid56: properly unmap parity page in finish_parity_scrub() Greg Kroah-Hartman
2019-04-01 17:02 ` [PATCH 4.14 059/107] ARM: imx6q: cpuidle: fix bug that CPU might not wake up at expected time Greg Kroah-Hartman
2019-04-01 17:02 ` [PATCH 4.14 060/107] powerpc: bpf: Fix generation of load/store DW instructions Greg Kroah-Hartman
2019-04-01 17:02 ` [PATCH 4.14 061/107] NFSv4.1 dont free interrupted slot on open Greg Kroah-Hartman
2019-04-01 17:02 ` [PATCH 4.14 062/107] net: dsa: qca8k: remove leftover phy accessors Greg Kroah-Hartman
2019-04-01 17:02 ` [PATCH 4.14 063/107] ALSA: rawmidi: Fix potential Spectre v1 vulnerability Greg Kroah-Hartman
2019-04-01 17:02 ` [PATCH 4.14 064/107] ALSA: seq: oss: Fix " Greg Kroah-Hartman
2019-04-01 17:02 ` [PATCH 4.14 065/107] ALSA: pcm: Fix possible OOB access in PCM oss plugins Greg Kroah-Hartman
2019-04-01 17:02 ` [PATCH 4.14 066/107] ALSA: pcm: Dont suspend stream in unrecoverable PCM state Greg Kroah-Hartman
2019-04-01 17:02 ` [PATCH 4.14 067/107] ALSA: hda/realtek - Add support headset mode for DELL WYSE AIO Greg Kroah-Hartman
2019-04-01 17:02 ` [PATCH 4.14 068/107] ALSA: hda/realtek - Add support headset mode for New DELL WYSE NB Greg Kroah-Hartman
2019-04-01 17:02 ` [PATCH 4.14 069/107] kbuild: modversions: Fix relative CRC byte order interpretation Greg Kroah-Hartman
2019-04-01 17:02 ` [PATCH 4.14 070/107] fs/open.c: allow opening only regular files during execve() Greg Kroah-Hartman
2019-04-01 17:02 ` [PATCH 4.14 071/107] ocfs2: fix inode bh swapping mixup in ocfs2_reflink_inodes_lock Greg Kroah-Hartman
2019-04-01 17:02 ` [PATCH 4.14 072/107] scsi: sd: Fix a race between closing an sd device and sd I/O Greg Kroah-Hartman
2019-04-01 17:02 ` [PATCH 4.14 073/107] scsi: sd: Quiesce warning if device does not report optimal I/O size Greg Kroah-Hartman
2019-04-01 17:02 ` [PATCH 4.14 074/107] scsi: zfcp: fix rport unblock if deleted SCSI devices on Scsi_Host Greg Kroah-Hartman
2019-04-01 17:02 ` [PATCH 4.14 075/107] scsi: zfcp: fix scsi_eh host reset with port_forced ERP for non-NPIV FCP devices Greg Kroah-Hartman
2019-04-01 17:02 ` [PATCH 4.14 076/107] tty: atmel_serial: fix a potential NULL pointer dereference Greg Kroah-Hartman
2019-04-01 17:02 ` [PATCH 4.14 077/107] staging: comedi: ni_mio_common: Fix divide-by-zero for DIO cmdtest Greg Kroah-Hartman
2019-04-01 17:02 ` [PATCH 4.14 078/107] staging: vt6655: Remove vif check from vnt_interrupt Greg Kroah-Hartman
2019-04-01 17:02 ` [PATCH 4.14 079/107] staging: vt6655: Fix interrupt race condition on device start up Greg Kroah-Hartman
2019-04-01 17:02 ` [PATCH 4.14 080/107] serial: max310x: Fix to avoid potential NULL pointer dereference Greg Kroah-Hartman
2019-04-01 17:02 ` [PATCH 4.14 081/107] serial: sh-sci: Fix setting SCSCR_TIE while transferring data Greg Kroah-Hartman
2019-04-01 17:02 ` [PATCH 4.14 082/107] USB: serial: cp210x: add new device id Greg Kroah-Hartman
2019-04-01 17:02 ` [PATCH 4.14 083/107] USB: serial: ftdi_sio: add additional NovaTech products Greg Kroah-Hartman
2019-04-01 17:02 ` [PATCH 4.14 084/107] USB: serial: mos7720: fix mos_parport refcount imbalance on error path Greg Kroah-Hartman
2019-04-01 17:02 ` [PATCH 4.14 085/107] USB: serial: option: set driver_info for SIM5218 and compatibles Greg Kroah-Hartman
2019-04-01 17:02 ` [PATCH 4.14 086/107] USB: serial: option: add support for Quectel EM12 Greg Kroah-Hartman
2019-04-01 17:02 ` [PATCH 4.14 087/107] USB: serial: option: add Olicard 600 Greg Kroah-Hartman
2019-04-01 17:02 ` [PATCH 4.14 088/107] Disable kgdboc failed by echo space to /sys/module/kgdboc/parameters/kgdboc Greg Kroah-Hartman
2019-04-01 17:02 ` [PATCH 4.14 089/107] fs/proc/proc_sysctl.c: fix NULL pointer dereference in put_links Greg Kroah-Hartman
2019-04-01 17:02 ` [PATCH 4.14 090/107] drm/vgem: fix use-after-free when drm_gem_handle_create() fails Greg Kroah-Hartman
2019-04-01 17:02 ` [PATCH 4.14 091/107] gpio: exar: add a check for the return value of ida_simple_get fails Greg Kroah-Hartman
2019-04-01 17:02 ` [PATCH 4.14 092/107] gpio: adnp: Fix testing wrong value in adnp_gpio_direction_input Greg Kroah-Hartman
2019-04-01 17:02 ` [PATCH 4.14 093/107] phy: sun4i-usb: Support set_mode to USB_HOST for non-OTG PHYs Greg Kroah-Hartman
2019-04-01 17:02 ` [PATCH 4.14 094/107] usb: mtu3: fix EXTCON dependency Greg Kroah-Hartman
2019-04-01 17:02 ` [PATCH 4.14 095/107] USB: gadget: f_hid: fix deadlock in f_hidg_write() Greg Kroah-Hartman
2019-04-01 17:02 ` [PATCH 4.14 096/107] usb: common: Consider only available nodes for dr_mode Greg Kroah-Hartman
2019-04-01 17:02 ` [PATCH 4.14 097/107] usb: host: xhci-rcar: Add XHCI_TRUST_TX_LENGTH quirk Greg Kroah-Hartman
2019-04-01 17:02 ` [PATCH 4.14 098/107] xhci: Fix port resume done detection for SS ports with LPM enabled Greg Kroah-Hartman
2019-04-01 17:02 ` [PATCH 4.14 099/107] usb: cdc-acm: fix race during wakeup blocking TX traffic Greg Kroah-Hartman
2019-04-01 17:02 ` [PATCH 4.14 100/107] mm/migrate.c: add missing flush_dcache_page for non-mapped page migrate Greg Kroah-Hartman
2019-04-01 17:02 ` [PATCH 4.14 101/107] perf intel-pt: Fix TSC slip Greg Kroah-Hartman
2019-04-01 17:02 ` [PATCH 4.14 102/107] cpu/hotplug: Prevent crash when CPU bringup fails on CONFIG_HOTPLUG_CPU=n Greg Kroah-Hartman
2019-04-01 17:02 ` [PATCH 4.14 103/107] x86/smp: Enforce CONFIG_HOTPLUG_CPU when SMP=y Greg Kroah-Hartman
2019-04-01 17:02 ` [PATCH 4.14 104/107] KVM: Reject device ioctls from processes other than the VMs creator Greg Kroah-Hartman
2019-04-01 17:03 ` [PATCH 4.14 105/107] KVM: x86: Emulate MSR_IA32_ARCH_CAPABILITIES on AMD hosts Greg Kroah-Hartman
2019-04-01 17:03 ` [PATCH 4.14 106/107] Revert "USB: core: only clean up what we allocated" Greg Kroah-Hartman
2019-04-01 17:03 ` [PATCH 4.14 107/107] vfio: ccw: only free cp on final interrupt Greg Kroah-Hartman
2019-04-01 21:03 ` [PATCH 4.14 000/107] 4.14.110-stable review kernelci.org bot
2019-04-02  8:58 ` Naresh Kamboju
2019-04-02  9:03 ` Jon Hunter
2019-04-02 19:05 ` Guenter Roeck
2019-04-02 23:45 ` shuah

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190401170050.862866177@linuxfoundation.org \
    --to=gregkh@linuxfoundation.org \
    --cc=dsterba@suse.com \
    --cc=fdmanana@suse.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=seulbae@gatech.edu \
    --cc=stable@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).