9

I have installed one SSD disk (Micron C400-MTFDDAC128MAM) in an external USB 3.0 enclosure. Now I want use this disk as a second disk in my laptop with Ubuntu 12.04. The disk is working, but I want to use TRIM support which is not working as expected.

Checking for TRIM support:

user@server:~$ sudo hdparm -I /dev/sdc | grep -i TRIM
       *    Data Set Management TRIM supported (limit 8 blocks)
       *    Deterministic read data after TRIM

Disk was mounted with the following options:

/dev/sdc1 on /media/MICRON type ext4 (rw,nosuid,nodev,uhelper=udisks)

But when I run the TRIM command manually, I get an error:

user@server:~$ sudo fstrim -v /media/MICRON/
fstrim: /media/MICRON/: FITRIM ioctl failed: Operation not permitted

I used this disk before as an internal disk and TRIM was working.

Here are some USB details:

[ 1039.248050] usb 4-1: new SuperSpeed USB device number 4 using xhci_hcd
[ 1039.265597] scsi8 : usb-storage 4-1:1.0
[ 1041.547879] scsi 8:0:0:0: Direct-Access     C400-MTF DDAC128MAM       0509 PQ: 0 ANSI: 5
[ 1041.549134] sd 8:0:0:0: Attached scsi generic sg2 type 0
[ 1041.550511] sd 8:0:0:0: [sdc] 250069680 512-byte logical blocks: (128 GB/119 GiB)
[ 1041.550778] sd 8:0:0:0: [sdc] Write Protect is off
[ 1041.550785] sd 8:0:0:0: [sdc] Mode Sense: 23 00 00 00
[ 1041.552520] sd 8:0:0:0: [sdc] No Caching mode page present
[ 1041.552528] sd 8:0:0:0: [sdc] Assuming drive cache: write through
[ 1041.554029] sd 8:0:0:0: [sdc] No Caching mode page present
[ 1041.554035] sd 8:0:0:0: [sdc] Assuming drive cache: write through
[ 1041.678373]  sdc: sdc1
[ 1041.679982] sd 8:0:0:0: [sdc] No Caching mode page present
[ 1041.679991] sd 8:0:0:0: [sdc] Assuming drive cache: write through
[ 1041.679997] sd 8:0:0:0: [sdc] Attached SCSI disk

How to find out if the mass storage device uses the UASP (USB Attached SCSI Protocol) which should support TRIM?

5 Answers 5

13

This is a software issue, Linux does not seem to currently support TRIM through USB. The problem is that USB storage devices employ the SCSI command set, whereas the SSD drive implements the ATA command set. The USB enclosure has to provide a translator between these command sets. The operation called TRIM in ATA is called UNMAP in SCSI and DISCARD in the Linux kernel. When Linux receives the command to trim a device, it looks up the correct command to be sent to the device. As USB storage devices look like SCSI disks, Linux tries to use UNMAP or a couple of other possible SCSI commands. In principle, the translator in the USB enclosure could often translate UNMAP requests to the corresponding ATA TRIM, although there are probably tricky cases. In practice, the enclosures don't do this, and they indicate instead that the device does not support UNMAP. However, many enclosures implement a SCSI command to issue ATA commands directly to the device. It is called ATA passthrough. There is a standard command to do this, but some enclosures have a proprietary command instead. In fact, hdparm -I uses ATA passthrough to get information from the device. The same passthrough could be used to issue TRIMs directly to the device, but the Linux driver does not currently do that. It would have to detect that a SCSI disk is actually a SCSI-to-ATA translator that supports ATA passthrough and use the passthrough for DISCARDs instead of the native SCSI commands.

3

This question deserves an updated answer now that there are USB enclosures on the market that support trim. For example if your enclosure uses the JMICRON JMS583 chip then it supports trim.

I am going to only include the steps required to get it to work if it is supported, but if you want a more detailed walk-through see the source link below.

  1. Get the vendor and product ids from lsusb
  2. Create /etc/udev/rules.d/50-usb-ssd-trim.rules with the following content, but change the vendor and product ids to match the ones found in step 1:
ACTION=="add|change", ATTRS{idVendor}=="152d", ATTRS{idProduct}=="0583", SUBSYSTEM=="scsi_disk", ATTR{provisioning_mode}="unmap"
  1. Reload the udev configuration sudo service udev force-reload
  2. Make sure you don't have the USB enclosure mounted and then replug the USB enclosure.
  3. You can check if trim is supported with lsblk --discard /dev/sda

See: https://www.glump.net/howto/desktop/enable-trim-on-an-external-ssd-on-linux

1

Your SSD reports to hdparm to support TRIM (hdparm -I = Request identification info directly from the drive).

TRIM however, is controlled by the drive controller.

It is quite likely that the USB3 harddiskcontroller of the external HDD closure doesn't support TRIM (most external controllers don't).

In that case you won't get any TRIM capabilities even if your SSD does support it.

4
  • USB3 has brought a new protocol: UASP (USB Attached SCSI Protocol) for communication with external mass storage devices over USB. The old protocol BOT (Bulk Only Transport) does not support TRIM. UASP supporting TRIM and NCQ.Question is how can I find out that the OS is using UASP with the external drive?
    – Luckyrings
    Mar 8, 2013 at 23:37
  • @Luckyrings Please ask a new question and Link to this question if it helps provide conetxt. Mar 8, 2013 at 23:48
  • @Luckyrings Here are tests of several UASP enclosures for TRIM. Almost none support TRIM except those that use a certain chipset. translate.google.co.in/translate?hl=en&sl=zh-CN&u=http:/…
    – user243057
    Mar 31, 2016 at 5:11
  • I opened a similar but more specific task about the same issue at askubuntu.com/questions/1466416/fstrim-for-usb-ssd-boot-drive . Could you kindly give me your opinione? Many thanks! May 4, 2023 at 3:56
1

If UNMAP is not translated correctly by your enclosure you can at least manually trim the whole drive using hdparm (this uses the ata-passthrough of the SCSI protocol and works fine on my UASP hdd dock). But you have to calculate the sectors manually because hdparm only supports trimming 65535 sectors at a time. I have written a short script to do the math:

#!/usr/bin/env python3

import sys

remaining = int(sys.argv[1])
i = 0

while remaining > 0:
    add = min(65535, remaining)
    print("%d:%d" % (i, add))
    remaining -= add
    i += add

Save it as sectors.py and do chmod +x sectors.py. It yields a list of sector-blocks usable with hdparm --trim-sector-ranges-stdin. Now execute hdparm -I /dev/sdX (as root) and hold out for a line that looks like:

LBA48  user addressable sectors:   62533296

This is the devices sector count (as you could calculate this is a ~32 GB SSD I frequently use for testing).

Copy the number to the following command:

./sectors.py SECTOR_COUNT | sudo hdparm --trim-sector-ranges-stdin --please-destroy-my-drive /dev/sdX

WARNING: This will erase the WHOLE DRIVE!

After it finished, run sync and wait some seconds. Now you can re-read the partition table with hdparm -z /dev/sdX or simply power-cycle the device. Congratulations, you have a "fresh" SSD now.

3
  • There is a better way of doing this. It's implemented as part of drive's security feature, called "Secure Erase". hdparm is used with --security-erase option after setting a password with --security-set-pass. After erase, the whole drive is essentially trimmed and zeroed. It's all done in the drive's flash controller. I've written a detailed blog post detailing the steps.
    – Mansour
    Dec 21, 2015 at 7:34
  • @Mansour does --security-erase wipe your existing data?
    – Krypton
    Jan 10, 2017 at 19:34
  • Never mind, I got the answer. It does wipe all of your data. Be careful!
    – Krypton
    Jan 10, 2017 at 19:49
1

If your device doesn't support trim, maybe it will after a firmware update.

For example ASMedia ASM1051E can it with firmware "140509_A1_82_40.bin"

You get it from there https://www.usbdev.ru/files/asmedia/asmt2115firmware/

Flasher tool from here https://www.usbdev.ru/files/asmedia/asm105mptool/

You need windows for flashing firmware.

After starting flasher click the key symbol on top and enter asmedia as PW. After that screen is unlocked. You can now choose firmware and change a few other settings like serialnumer from HDD. Then press play button in the lower left corner. It showed me failed, but after unplugging and replugging I saw new FW version.

Then after adding a udev rule in /etc/udev/rules.d/50-usb-ssd-trim.rules

ACTION=="add|change", ATTRS{idVendor}=="174c", ATTRS{idProduct}=="55aa", SUBSYSTEM=="scsi_disk", ATTR{provisioning_mode}="unmap"

and rebooting finally trim works.

With old FW:

sudo fstrim -v /
fstrim: /: FITRIM ioctl failed: Ein-/Ausgabefehler der Gegenstelle (remote)

With firmware "140509_A1_82_40.bin"

sudo fstrim -v /
/: 44,8 MiB (46931968 bytes) trimmed

To check if it works (adapted from) https://wiki.ubuntuusers.de/SSD/TRIM/Testen/

yes | sudo dd iflag=fullblock bs=1M count=1 of=trim.test 

Find where the file is exactly on disk

filefrag -s -v trim.test
Filesystem type is: ef53
File size of trim.test is 1048576 (256 blocks of 4096 bytes)
 ext:     logical_offset:        physical_offset: length:   expected: flags:
   0:        0..     255:     125184..    125439:    256:             last,eof
trim.test: 1 extent found

Find out on which disk file is:

Dateisystem            1K-Blöcke Benutzt Verfügbar Verw% Eingehängt auf
/dev/mapper/sda2_crypt 122768060 1294164 116453544    2% /

Read content directly from disk

sudo dd bs=4096 skip=125184 count=256 if=/dev/mapper/sda2_crypt | hexdump -C
00000000  79 0a 79 0a 79 0a 79 0a  79 0a 79 0a 79 0a 79 0a  |y.y.y.y.y.y.y.y.|
*
256+0 Datensätze ein
256+0 Datensätze aus
1048576 bytes (1,0 MB, 1,0 MiB) copied, 0,0683545 s, 15,3 MB/s
00100000

Now delete, sync and trim

sudo rm trim.test
sync 
sudo fstrim -v ./ 

Drop cache before verifying

echo 1 | sudo tee /proc/sys/vm/drop_caches

exec again

sudo dd bs=4096 skip=125184 count=256 if=/dev/mapper/sda2_crypt | hexdump -C

Now you should see a lot of output on an encrypted drive like

000070a0  e3 2d ee 86 3f c3 58 12  7e 89 94 27 ba 7c d8 57  |.-..?.X.~..'.|.W|
000070b0  58 5f 61 2a d1 37 63 77  8e 54 29 c5 a5 55 2c 66  |X_a*.7cw.T)..U,f|
000070c0  54 ab 00 07 c8 2c 64 5a  b1 24 d0 73 23 1c 9e 0d  |T....,dZ.$.s#...|
000070d0  3d 39 3e 43 36 54 d8 55  1d c7 79 bd b4 17 13 93  |=9>C6T.U..y.....|
000070e0  b1 75 a6 34 d4 2a c2 0c  13 c6 ff df ed 78 d7 42  |.u.4.*.......x.B|

On an unencrypted drive you'll see

00000000  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*

If trim doesn't work you'll see again

00000000  79 0a 79 0a 79 0a 79 0a  79 0a 79 0a 79 0a 79 0a  |y.y.y.y.y.y.y.y.|
*

--> Verified that trim is working with USB enclosure

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .