zfsonlinux/zfs — pull request #924 by dechamps (2012-09-01 09:04:35+00:00)
Title: TRIM/UNMAP/DISCARD support for vdevs

This adds TRIM (a.k.a UNMAP, DISCARD, hole punching) support for vdevs. The original patch is from Pawel Jakub Dawidek (pjd@FreeBSD.org) who wrote it for FreeBSD. I'm just porting it to ZFS On Linux. Original discussion is in #598.

This code is largely untested and carries a *high risk of data corruption* since it deals with deletion of data. **DO NOT APPLY THIS PATCH IF YOU CARE ABOUT YOUR DATA. USE AT YOUR OWN RISK. EXPECT DATA LOSS.**

@behlendorf: I'm not asking you to merge this. At least not right now. I'm just opening a pull request to give the patch more visibility and get more testing. As usual, as soon as it's ready and received enough testing for your taste, I'll clean it and open another, final pull request.

Note that the new feature is disabled by default (`zfs_notrim=1`). To use it, you have to explicitly load the module with `zfs_notrim=0`. For TRIM to work, you need to have vdevs that support it, e.g. SSDs. If you don't have any, you can still test using a Linux RAM block device (`rbd` module), as it supports discards.

There is a module parameter (`trim_txg_limit`) that specifies after how many TXGs the freed data is actually discarded on the device (default 64). Indeed, the algorithm doesn't discard data immediately so that pool disaster recovery (i.e. `zpool -F`) don't break. I suggest you test with the default and with a low value (e.g. 1) to make the algorithm more aggressive, which might trigger different bugs.

TODO list:
- Extensive testing for stability and data integrity
- Add a "test mode" that writes zeroes or some pattern to the range being discarded to make data corruption obvious and reproducible, and to make the code testable on devices that don't support DISCARD
- Discard whole L2ARC and slog devices on import/add
- Support hole punching on file vdevs using Linux APIs
- Add userspace discard support (i.e. `ioctl(BLKDISCARD)`)
- See if there's room for optimization

Note that I have no idea if I'll have enough time to see this whole thing through, so if you want to take care of some items of the TODO list, be my guest.