Trim SSDs through ioctl in Java/Scala

Trim Command is defined for SSDs to mask which data are discarded. This low level ATA command can be submitted from the user space by ioctl (see: Manually trim solid-state drivers (SSDs) in Linux).

The c code used to submit trim command is:

#ifndef BLKDISCARD
#define BLKDISCARD _IO(0x12,119)
#endif

#ifndef BLKSECDISCARD
#define BLKSECDISCARD  _IO(0x12,125)
#endif

uint64_t range[2];

range[0] = 0;
range[1] = ULLONG_MAX;

if (secure) {
  if (ioctl(fd, BLKSECDISCARD, &range))
    err(EXIT_FAILURE, _("%s: BLKSECDISCARD ioctl failed"), path);
} else {
  if (ioctl(fd, BLKDISCARD, &range))
    err(EXIT_FAILURE, _("%s: BLKDISCARD ioctl failed"), path);
}

My purpose is to run this function in Scala. Checkout this post to see how to call libc code in Scala. The trim function in scala is:

  def trim(offset:Long, length:Long):Int = { // length = N * page size
    val range = Array(offset, length)
    if ( fd != 0 ) {
      libc.ioctl(fd, 0x1277, range)
    } else -1
  }