# To TRIM or not to TRIM, that's the question



## mirco (Aug 22, 2015)

Hi Folks,

recently, me as a noob got one experts opinion on (not) using TRIM.

Since this is the only voice I know recommending not to use TRIM,
I'd like to put it here and hopefully get your valued opinion on this topic.

Also I'd like to know, why TRIM has the potential to corrupt the filesystem.



			
				Matt Dillon said:
			
		

> I do NOT recommend using TRIM
> except for initial erasure of the SSD, and potentially to clean out the swap partition on boot.
> 
> Instead of trying to use TRIM, just leave a couple of gigabytes of unused space on the SSD.
> ...



Thank you and Best Wishes


----------



## wblock@ (Aug 22, 2015)

TRIM is a way to tell the SSD hardware that a block is no longer being used.  It can't tell whether a block is still being used just by writes.  It has to assume that if a block has been written, it is still being used.  That's the reason it is not advised to write images to SSDs with dd(1).  It makes the SSD think every block is in use.

With SSDs, which blocks are in use matters because of wear leveling and garbage collection.  The SSD swaps data to less-used areas in the background.  But it has to know which blocks are really used and which are free to be reused.  When a block is unused, there is no need to move the data to another block, so using trim can maintain performance.

One way to do that is overprovisioning, where a section of the drive is either unpartitioned or partitioned but never formatted.  The SSD knows these blocks are unused because they have never been written.

A filesystem that supports trim uses it to tell the SSD about blocks that are no longer being used, like when a file is deleted.

There have been recent claims about problems with trim in the latest Samsung SSDs.  Blocks of zeros were found in data files.  Some people claimed this was a Samsung firmware problem, later investigation claims it was due to a Linux kernel bug: https://blog.algolia.com/when-solid-state-drives-are-not-that-solid/.  Linux's `fstrim` appears to work by issuing the TRIM command for all unused blocks of a filesystem.  My impression is that UFS does this as individual blocks are deleted.

I've used trim without problems on all the SSDs I've used with UFS on FreeBSD and NTFS on Windows 7 and 8.  On some SSDs, I also left some unused space on the drive.  This is an admittedly small sample, and disregards Linux because I generally don't use Linux.  To date, I have not seen any data corruption.

If I were running Linux, I might be more worried.  The quote above does not specify which operating system or filesystem is being used.  Maybe it was meant for a specific case.


----------



## mirco (Aug 22, 2015)

Thanks for the in detail answer, appreciate that.



wblock@ said:


> The quote above does not specify which operating system or filesystem is being used.  Maybe it was meant for a specific case.


I am sorry, of course, I forgot. This is for DragonflyBSD with the following disk setup:


> a - UFS /boot partition, typically 1GB
> b - Swap partition, depends on use case but usually not less than 1 x main mem
> d - One hammer partition for the remainder


----------

