For an SSD, I can open the row device and discard a range of pages using ioctl() with BLKDISCARD command.My question is for an open file, how to discard a range of data? and Is that depends on the implementation of the underlying filesystem?
How to discard/TRIM a range in an open file ?
linux;filesystems;ssd;trim
You're looking for fallocate's FALLOC_FL_PUNCH_HOLE. It's then up to the filesystem to discard/trim/unmap those blocks. I believe ext4 will do so.For example, you'd do something like this to discard 2MiB starting 1MiB in:err = fallocate(fd, FALLOC_FL_PUNCH_HOLE|FALLOC_FL_KEEP_SIZE, 1048576, 2097152);if (0 != err) {    /* handle error */}On success, future reads of those bytes should give 0s.[Note: POSIX doesn't have a way to do this. The above fallocate is Linux-specific. But you tagged your question Linux, so hopefully that's OK.] 