<div dir="auto">Had same issue on a similar (well,  quite exactly same setup), on a machine in production.<div dir="auto">But It Is more than 4 tera of data, so in the end I re-dd the image and restarted, sticking to 5.0.y branch never had problem.</div><div dir="auto">I was able to replicate it. SSD Samsung, more recent version.</div><div dir="auto">Not with btrfs but ext4, by the way.</div><div dir="auto">I saw the discard of big initial part of lvm partition. I can't find superblocks Copy in the First half, but torwards the end of logical volume.</div><div dir="auto"><br></div><div dir="auto">Sorry, i can't play with It again, but i have the whole (4 tera) dd image with the bug.</div><div dir="auto"><br></div><div dir="auto"><br></div><div dir="auto">Ciao,</div><div dir="auto">Gelma</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">Il lun 20 mag 2019, 02:38 Michael Laß <<a href="mailto:bevan@bi-co.net">bevan@bi-co.net</a>> ha scritto:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">CC'ing dm-devel, as this seems to be a dm-related issue. Short summary for new readers:<br>
<br>
On Linux 5.1 (tested up to 5.1.3), fstrim may discard too many blocks, leading to data loss. I have the following storage stack:<br>
<br>
btrfs<br>
dm-crypt (LUKS)<br>
LVM logical volume<br>
LVM single physical volume<br>
MBR partition<br>
Samsung 830 SSD<br>
<br>
The mapping between logical volumes and physical segments is a bit mixed up. See below for the output for “pvdisplay -m”. When I issue fstrim on the mounted btrfs volume, I get the following kernel messages:<br>
<br>
attempt to access beyond end of device<br>
sda1: rw=16387, want=252755893, limit=250067632<br>
BTRFS warning (device dm-5): failed to trim 1 device(s), last error -5<br>
<br>
At the same time, other logical volumes on the same physical volume are destroyed. Also the btrfs volume itself may be damaged (this seems to depend on the actual usage).<br>
<br>
I can easily reproduce this issue locally and I’m currently bisecting. So far I could narrow down the range of commits to:<br>
Good: 92fff53b7191cae566be9ca6752069426c7f8241<br>
Bad: 225557446856448039a9e495da37b72c20071ef2<br>
<br>
In this range of commits, there are only dm-related changes.<br>
<br>
So far, I have not reproduced the issue with other file systems or a simplified stack. I first want to continue bisecting but this may take another day.<br>
<br>
<br>
> Am 18.05.2019 um 12:26 schrieb Qu Wenruo <<a href="mailto:quwenruo.btrfs@gmx.com" target="_blank" rel="noreferrer">quwenruo.btrfs@gmx.com</a>>:<br>
> On 2019/5/18 下午5:18, Michael Laß wrote:<br>
>> <br>
>>> Am 18.05.2019 um 06:09 schrieb Chris Murphy <<a href="mailto:lists@colorremedies.com" target="_blank" rel="noreferrer">lists@colorremedies.com</a>>:<br>
>>> <br>
>>> On Fri, May 17, 2019 at 11:37 AM Michael Laß <<a href="mailto:bevan@bi-co.net" target="_blank" rel="noreferrer">bevan@bi-co.net</a>> wrote:<br>
>>>> <br>
>>>> <br>
>>>> I tried to reproduce this issue: I recreated the btrfs file system, set up a minimal system and issued fstrim again. It printed the following error message:<br>
>>>> <br>
>>>> fstrim: /: FITRIM ioctl failed: Input/output error<br>
>>> <br>
>>> Huh. Any kernel message at the same time? I would expect any fstrim<br>
>>> user space error message to also have a kernel message. Any i/o error<br>
>>> suggests some kind of storage stack failure - which could be hardware<br>
>>> or software, you can't know without seeing the kernel messages.<br>
>> <br>
>> I missed that. The kernel messages are:<br>
>> <br>
>> attempt to access beyond end of device<br>
>> sda1: rw=16387, want=252755893, limit=250067632<br>
>> BTRFS warning (device dm-5): failed to trim 1 device(s), last error -5<br>
>> <br>
>> Here are some more information on the partitions and LVM physical segments:<br>
>> <br>
>> fdisk -l /dev/sda:<br>
>> <br>
>> Device     Boot Start       End   Sectors   Size Id Type<br>
>> /dev/sda1  *     2048 250069679 250067632 119.2G 8e Linux LVM<br>
>> <br>
>> pvdisplay -m:<br>
>> <br>
>>  --- Physical volume ---<br>
>>  PV Name               /dev/sda1<br>
>>  VG Name               vg_system<br>
>>  PV Size               119.24 GiB / not usable <22.34 MiB<br>
>>  Allocatable           yes (but full)<br>
>>  PE Size               32.00 MiB<br>
>>  Total PE              3815<br>
>>  Free PE               0<br>
>>  Allocated PE          3815<br>
>>  PV UUID               mqCLFy-iDnt-NfdC-lfSv-Maor-V1Ih-RlG8lP<br>
>> <br>
>>  --- Physical Segments ---<br>
>>  Physical extent 0 to 1248:<br>
>>    Logical volume    /dev/vg_system/btrfs<br>
>>    Logical extents   2231 to 3479<br>
>>  Physical extent 1249 to 1728:<br>
>>    Logical volume    /dev/vg_system/btrfs<br>
>>    Logical extents   640 to 1119<br>
>>  Physical extent 1729 to 1760:<br>
>>    Logical volume    /dev/vg_system/grml-images<br>
>>    Logical extents   0 to 31<br>
>>  Physical extent 1761 to 2016:<br>
>>    Logical volume    /dev/vg_system/swap<br>
>>    Logical extents   0 to 255<br>
>>  Physical extent 2017 to 2047:<br>
>>    Logical volume    /dev/vg_system/btrfs<br>
>>    Logical extents   3480 to 3510<br>
>>  Physical extent 2048 to 2687:<br>
>>    Logical volume    /dev/vg_system/btrfs<br>
>>    Logical extents   0 to 639<br>
>>  Physical extent 2688 to 3007:<br>
>>    Logical volume    /dev/vg_system/btrfs<br>
>>    Logical extents   1911 to 2230<br>
>>  Physical extent 3008 to 3320:<br>
>>    Logical volume    /dev/vg_system/btrfs<br>
>>    Logical extents   1120 to 1432<br>
>>  Physical extent 3321 to 3336:<br>
>>    Logical volume    /dev/vg_system/boot<br>
>>    Logical extents   0 to 15<br>
>>  Physical extent 3337 to 3814:<br>
>>    Logical volume    /dev/vg_system/btrfs<br>
>>    Logical extents   1433 to 1910<br>
>> <br>
>> <br>
>> Would btrfs even be able to accidentally trim parts of other LVs or does this clearly hint towards a LVM/dm issue?<br>
> <br>
> I can't speak sure, but (at least for latest kernel) btrfs has a lot of<br>
> extra mount time self check, including chunk stripe check against<br>
> underlying device, thus the possibility shouldn't be that high for btrfs.<br>
<br>
Indeed, bisecting the issue led me to a range of commits that only contains dm-related and no btrfs-related changes. So I assume this is a bug in dm.<br>
<br>
>> Is there an easy way to somehow trace the trim through the different layers so one can see where it goes wrong?<br>
> <br>
> Sure, you could use dm-log-writes.<br>
> It will record all read/write (including trim) for later replay.<br>
> <br>
> So in your case, you can build the storage stack like:<br>
> <br>
> Btrfs<br>
> <dm-log-writes><br>
> LUKS/dmcrypt<br>
> LVM<br>
> MBR partition<br>
> Samsung SSD<br>
> <br>
> Then replay the log (using src/log-write/replay-log in fstests) with<br>
> verbose output, you can verify every trim operation against the dmcrypt<br>
> device size.<br>
> <br>
> If all trim are fine, then move the dm-log-writes a layer lower, until<br>
> you find which layer is causing the problem.<br>
<br>
That sounds like a plan! However, I first want to continue bisecting as I am afraid to lose my reproducer by changing parts of my storage stack.<br>
<br>
Cheers,<br>
Michael<br>
<br>
> <br>
> Thanks,<br>
> Qu<br>
>> <br>
>> Cheers,<br>
>> Michael<br>
>> <br>
>> PS: Current state of bisection: It looks like the error was introduced somewhere between b5dd0c658c31b469ccff1b637e5124851e7a4a1c and v5.1.<br>
<br>
</blockquote></div>