View Single Post
  #2  
Old 8th April 2012, 05:54 AM
stevea
Guest
 
Posts: n/a
Re: fstrim shows discardable blocks even after using discard option

fstrim makes a single syscall withthe open mountpoint.
This call falls to the kernel filesystem code (ext4, btrfs, whatever) which then walks the file system data structures to find all unused blocks in the block device that have not been discarded since the mount. Since the kernel has no idea which blocks have already been discarded - it discards ALL unused blocks of the filesystem (except maybe ones that if KNOWS have been discadred already since mount. Then it discards all of these.

When you run fstrim a second time on an mounted FS, then for ext4 at least it KNOWS all the blocks have been trim'ed already- so the it reports 0 trimmed.

For btrfs it discards ALL unused blocks on every use of 'fstrim. IOW btrfs isn't smart enough to keep

If you reboot or umount and mount the same FS - then it will report the big number - essentially the number of unused blocks.



Example for a btrfs fs
Quote:
[root@crucibulum /]# mount /dev/sda5 /tmp/mp -o discard,ssd
[root@crucibulum /]# fstrim -v /tmp/mp
/tmp/mp: 2566369280 bytes were trimmed
[root@crucibulum /]# fstrim -v /tmp/mp
/tmp/mp: 2566369280 bytes were trimmed
this is for an ext4 fs
Quote:
[root@crucibulum ~]# mount -t ext4 -o discard /dev/sda5 /tmp/mp
[root@crucibulum ~]# fstrim -v /tmp/mp
/tmp/mp: 31813779456 bytes were trimmed
[root@crucibulum ~]# fstrim -v /tmp/mp
/tmp/mp: 0 bytes were trimmed
[root@crucibulum ~]# umount /tmp/mp
[root@crucibulum ~]# mount -t ext4 -o discard /dev/sda5 /tmp/mp
[root@crucibulum ~]# fstrim -v /tmp/mp
/tmp/mp: 31813779456 bytes were trimmed
So ext4 KEEPs the trim info between fstrim calls, while btrfs does not.
Neither can keep the keep the info between mounts or boots.

Last edited by stevea; 8th April 2012 at 06:03 AM.
Reply With Quote