Update on TRIM support

Tim Bisson bissont at mac.com
Sat Jul 16 16:32:24 PDT 2011

Here's another update on Trim:

First, the code:
* Support for slices (UFS/hammer):
** "newfs -E /dev/da0s1b" trims the right sectors
* ability to trim swap
** swapon -e /dev/da0s1b
** adjusted rcorder scripts so swapon starts after savecore
*** allows us to use the same device for savecore and swap since "swapon 
-e" will trim the device only after savecore has run.

* sysctl to control trim
** kern.cam.da.X.trim_enabled
** sysctl appears only if the device supports trim
** sysctl needs to be set in order to trim a new fs and/or enable online 
FS trim

* enable online trim in ufs with mount option "-o trim"

For example, one could use a device in the following manner:
$ sysctl kern.cam.da.0.trim_enabled=1    #enables trim for /dev/da0
$ newfs_hammer -L da0s1a -E /dev/da0s1a  #trim device slice a
$ mount_hammer /dev/da0s1a /mnt/test     #mount hammer slice
$ newfs -E /dev/da0s1b                   #trims the device slice b
$ mount -o trim /dev/da0s1b /mnt/test    #enable online trim for UFS
$ umount /mnt/test
$ mount  /dev/da0s1b /mnt/test           #online trim for UFS off
Feedback is always welcome.

On 6/1/11 11:12 PM, Tim Bisson wrote:
Hi All,

Here's an update on TRIM support based on feedback from Matt, Alex,
Venkatesh, and others. Thanks guys.
First, the code:
If you clone the code and do a:
git difftool upstream/master..HEAD
you can get a diff against the current master (as of today).
* Moved the underlying TRIM code from the IDE driver to the AHCI/SCSI
* Made UFS block frees (ffs_blkfree()) execute TRIM command on blocks
asynchronously instead of synchronously. Once the TRIM command is
completed, we free the actual blocks in the freemap in the callback.
This makes delete performance much faster as we don't have to wait for
the TRIM command to finish before returning from the ffs_blkfree().
* Added an option, -E to newfs_hammer, that will run TRIM on the devices
making up the hammer file system before the hammer file system is created.
* Modified the IOCTL to take in bytes rather than sectors. In the IOCTL,
we  issue a BUF_CMD_FREEBLKS command using a struct buf, which has a
size limit of 2^31 (b_bcount) bytes. Therefore, we potentially have to
break up the IOCTL TRIM command into multiple requests if the original
command is for more than 2^31 bytes in size.
  From "man newfs":
-R      Turn on the TRIM enable flag.  If enabled, and if the underlying
               device supports the BIO_DELETE command, the file system
will send
               a delete request to the underlying device for each freed
               The trim enable flag is typically set when the underlying
               uses flash-memory as the device can use the delete command to
               pre-zero or at least avoid copying blocks that have been
-E      Use TRIM to erase the file system before creating it.  If used
               and the underlying device supports TRIM, the device's LBAs
               be erased using the TRIM command before the file system is
  From "man newfs_hammer"
-E      TRIM Device(s) before FS creation. Should only be called if all
               devices are SSDs that support TRIM.
Any suggestions, ideas, or feedback is welcome.


More information about the Kernel mailing list